0

我正在尝试在具有其他元素的 RelativeLayout 顶部的动态位置绘制图像。我尝试创建一个扩展 View 的类并将此视图添加到 RelativeLayout 但图像只是屏幕上显示的东西。RelativeLayout 中的其他元素不可见。

这是视图类

@Override
protected void onDraw (Canvas canvas) {

    super.onDraw(canvas);
    Bitmap myBitmap = BitmapFactory.decodeResource(getResources(),    R.drawable.ic_launcher);
    canvas.drawColor(Color.BLACK);
    canvas.drawBitmap(myBitmap, 50,50, null);   
}

这是在 Activity 的 onCreate

    mainLayout = (RelativeLayout)findViewById(R.id.main_tutorial_layout);
    mainLayout.addView( new DrawAnimationTutotial(getApplicationContext()));
    mainLayout.invalidate();
4

1 回答 1

1

为什么不使用 ImageView?

mainLayout = (RelativeLayout) findViewById(R.id.main_tutorial_layout);
ImageView image = new ImageView(this);
image.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(50, 50);
layoutParams.leftMargin = xValue; 
layoutParams.topMargin = yValue;
image.setLayoutParams(layoutParams);
mainLayout.addView(image);
于 2012-07-15T04:32:55.690 回答