0

我开始学习 Android 中的位图和简单图形。我对此有点陌生,所以我仍在努力掌握这些概念。我下面的代码来自一个教程。它适用于位图和可绘制。我让它显示一个三角形的图像。但我有几个问题: 首先:三角形的图像显示 - 但它的底部延伸到屏幕底部。我该如何解决?第二:如何在屏幕中心显示三角形?

我的代码(取自 vogella.com):

可绘制类:

public class MyRoundCornerDrawable extends Drawable {

private Paint paint;

public MyRoundCornerDrawable(Bitmap bitmap){
    BitmapShader shader;
    shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP,
            Shader.TileMode.CLAMP);
    paint = new Paint();
    paint.setAntiAlias(true);
    paint.setShader(shader);
}

@Override
public void draw(Canvas canvas) {
    int height = getBounds().height();
    int width = getBounds().width();
     RectF rect = new RectF(0.0f, 0.0f, width, height);
        canvas.drawRoundRect(rect, 15, 15, paint);

}

@Override
public int getOpacity() {

    return 255;
}

@Override
public void setAlpha(int alpha) {


}

@Override
public void setColorFilter(ColorFilter arg0) {


}

}

主要活动

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImageView button = (ImageView) findViewById(R.id.image);
    InputStream resource = getResources().openRawResource(R.drawable.triangle);
    Bitmap bitmap = BitmapFactory.decodeStream(resource);
    button.setBackground(new MyRoundCornerDrawable(bitmap));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

和 XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

 <ImageView
     android:id="@+id/image"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:contentDescription="TODO" />

4

1 回答 1

0
  1. scaleType您的ImageView. fitXY或者center应该做的伎俩
  2. 添加layout_centerInParent="true"到您的ImageView

    <ImageView
        android:scaleType="center"
        android:id="@+id/image"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        layout_centerInParent="true" 
        android:contentDescription="TODO" />
    
于 2013-05-15T15:20:28.910 回答