Android ImageView绘图对象

时间:2013-04-14 11:04:49

标签: android android-imageview

我有这个Android程序,我想在父布局的Rectangle内显示ImageView。组件在此层次结构上设置,并遵循下面列出的布局。显示矩形的代码也如下所示:

  • LinearLayout [垂直]
    • 微调
    • ImageView的
<LinearLayout 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:orientation="vertical">    
    <Spinner
        android:id="@+id/spinMap"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:listitem="@android:layout/simple_spinner_item" />    
    <ImageView
        android:id="@+id/imageMap"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/black" />
</LinearLayout>

CODE:

public class MainActivty extends Activity {

    private Display     display;
    private Spinner     spinMap;
    private ImageView   imageMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initailize();
    }
    private void initailize() {
        display = getWindowManager().getDefaultDisplay();
        spinMap = (Spinner) findViewById(R.id.spinMap);
        imageMap = (ImageView) findViewById(R.id.imageMap);
        imageMap.draw(drawMap());
    }
    private Canvas drawMap() {
        Canvas canvas = new Canvas(Bitmap.createBitmap(display.getWidth(), display.getHeight(), Bitmap.Config.RGB_565));
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        canvas.drawRect(10.0f, 10.0f, 100.0f, 100.0f, paint);
        return canvas;
    }
}

问题是当我运行应用程序时,Rectangle无法显示。如何显示Rectangle

1 个答案:

答案 0 :(得分:2)

我认为这可以帮助你Android - Canvas drawLine inside ImageView

您应该使用矩形创建位图,然后将其提供给ImageView