Copy Bitmap content of one ImageView to anoher

It puzzled me. I need to copy Bitmap from one ImageView to another. I don’t want to just copy one ImageView to another, because I need to make some changes to the bitmap along the way.

Here is the code that doesn't work.

ImageView ivSrc = (ImageView) findViewById(R.id.photo); ivSrc.setDrawingCacheEnabled(true); Bitmap bmSrc1 = ivSrc.getDrawingCache(); // will cause nullPointerException Bitmap bmSrc2 = Bitmap.createBitmap(ivSrc.getDrawingCache());//bmSrc2 will be null View vSrc = (View) ivSrc.getParent(); vSrc.setDrawingCacheEnabled(true); Bitmap bmSrc3 = Bitmap.createBitmap(vSrc.getDrawingCache()); //black bitmap 

// To check bitmaps:

  ImageView ivDest = (ImageView) findViewById(R.id.photo2); ivDest.setImageBitmap(bmSrc1); //bmSrc1, 2, 3 results shown above 

I need to do it wrong, because making a copy is so simple. TIA

+7
android imageview
source share
1 answer

The drawing cache is not used, but is it necessary to call buildDrawingCache ()?

How will I do it:

 Bitmap bmSrc1 = ((BitmapDrawable)ivSrc.getDrawable()).getBitmap(); Bitmap bmSrc2 = bmSrc1.copy(bmSrc1.getConfig(), true); 

Note that bmSrc2 is mutable, i.e. you can embed it in the canvas and do whatever you like with it before you draw it anywhere.

+20
source share

All Articles