Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deep copy of a Drawable

I have an ImageView. In its onClick I get its Drawable:

Drawable dr = ((ImageView) v).getDrawable();

And set it to a dialog's ImageView:

zoomedImage.setImageDrawable(dr);

But when I close the dialog or the activity is resumed. The image at the original position gets stretched and is shown larger than its size, leading to only a portion of the image is visible in the ImageView.

Is this a case of deep copy or there is another problem? If it is, how can do I deep copy the original Drawable so that I could set the copy to zoomed image?

Thanks in advance.

like image 689
Shashank Degloorkar Avatar asked Oct 25 '12 08:10

Shashank Degloorkar


3 Answers

Finally I succeed! I had similar problem, when I used color filter on my drawable it changed the drawable, its very close to the solution of the other people here, but only this worked for me:

Drawable drwNewCopy = dr.getConstantState().newDrawable().mutate();
like image 183
LiranNis Avatar answered Nov 17 '22 01:11

LiranNis


I managed to copy the drawable using following code:

drawable.mutate().getConstantState().newDrawable();

Here mutate() makes the drawable mutable to avoid sharing its state, and getConstantState().newDrawable() creates a new copy.

Thus different ImageViews use different drawables and there's no stretching.

like image 33
ernazm Avatar answered Nov 17 '22 02:11

ernazm


Use BitmapFactory to convert the drawable into bitmap separately make or perform changes on it.

like image 1
Deepak Samuel Rajan Avatar answered Nov 17 '22 02:11

Deepak Samuel Rajan