musa
musa

Reputation: 1465

Canvas to ImageView problem in Android

I want to display Canvas contents on ImageView in android,

but ImageView displaying blank.

Bitmap imgBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
Canvas canvas = new Canvas();
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

canvas.drawBitmap(imgBitmap, 0, 0, paint);

paint.setColor(Color.BLACK);
paint.setAlpha(100);
canvas.drawRect(0, 0, 100, 100, paint); // transparent black on image

imgView.draw(canvas);

what is the problem? and what should I do?

Upvotes: 0

Views: 6793

Answers (1)

Dan S
Dan S

Reputation: 9189

When ImageView.draw() is called its actually putting the contents of the ImageView into the provided canvas, you have it logically backwards here. Instead use Canvas(Bitmap) constructor instead (so your canvas will draw on to the bitmap) then ImageView.setImageBitmap() with the same bitmap given to the canvas. You can use Bitmap.createBitmap(int, int, Bitmap.Config) to create the size of Bitmap you want. And remember if you have the canvas draw outside the Bitmap's bounds it is clipped.

Upvotes: 5

Related Questions