Android place ImageView on top of Canvas

回眸只為那壹抹淺笑 提交于 2020-01-05 11:39:48

问题


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:


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


来源:https://stackoverflow.com/questions/29307102/android-place-imageview-on-top-of-canvas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!