Android 将 ImageView 放在 Canvas 之上

Android place ImageView on top of Canvas

如何在先前创建的视图 Canvas 之上显示以编程方式创建的 ImageView?我尝试使用 bringToFront(),但没有成功。

代码:

RelativeLayout root = findViewById(R.id.layout);
ImageView img = new ImageView(GameGuessActivity.context);
img.setImageResource(R.drawable.image);

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, height);
params.leftMargin = x;
params.topMargin = y;
root.addView(img, params);
img.bringToFront();

图像正在显示,但在 canvas 下方,我不想要它。

您必须在 canvas 上重绘图像视图:

  pcanvas = new Canvas();
        pcanvas.setBitmap(bitmap);    // drawXY will result on that Bitmap
        pcanvas.drawBitmap(bitmap, 0, 0, null); 
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
        image.draw(pcanvas);

        img.bringToFront();
        img.invalidate();
        img.draw(pcanvas);