Android place ImageView on top of Canvas

2019-08-06 13:14发布

how can I display a programmatically created ImageView on top of a previously created View Canvas? I tried using bringToFront(), but without success.

Code:

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();

The Image is being displayed, but underneath the canvas, which I don't want.

1条回答
做自己的国王
2楼-- · 2019-08-06 14:07

You have to redraw your imageview on 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);
查看更多
登录 后发表回答