1

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

  • 线性布局 [垂直]
    • 微调器
    • 图像视图
<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>

代码:

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

4

1 回答 1

2

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

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

于 2013-04-14T11:14:24.997 回答