Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android TransitionDrawable: image scaletype changes upon fragment resuming

I have an ImageView set as the background for a fragment (the imageview is set to match_parent on both width and height) and the scaletype is set to centerCrop. I download an image to put in there asynchronously and use a TransitionDrawable to crossfade between once it has finished downloading.

The image appears with the correct scaletype the first time, however if I navigate to another fragment then back to this fragment with the imageview as the background, the image appears warped/stretched (it looks like the imageView is set to fitXY instead)

If I don't use a TransitionDrawable the images stay as they should look.

Is there a TransitionDrawable equivalent that maintains the correct aspect ratio? Or is there something I need to set on TransitionDrawable to prevent this resizing?

All I really want to do is have a nice transition between images. I was thinking of just setting the image to the second layer drawable of the TransitionDrawable once it has finished animating but there is no way to monitor when the animation has finished...

edit: I thought I'd mention the images are not the same dimensions, not sure if that makes a difference

Edit 2: Here is the code to set the drawable:

  //code above to download image
 BitmapDrawable bitmapDrawable = new BitmapDrawable(imageview.getResources(), bitmap);
        TransitionDrawable transitionDrawable = new TransitionDrawable(new Drawable[]{imageview.getDrawable(), bitmapDrawable});
        imageview.setImageDrawable(transitionDrawable);
        transitionDrawable.startTransition(300);
like image 429
AndroidNoob Avatar asked Oct 15 '13 13:10

AndroidNoob


1 Answers

Are you using images of different dimensions? That might be causing the issue. Try using images of same dimensions.

Edit: Found the solution: You need to set the ImageView's scaletype again after using the setImageDrawable command.

    BitmapDrawable bitmapDrawable = new BitmapDrawable(imageview.getResources(), bitmap);
    TransitionDrawable transitionDrawable = new TransitionDrawable(new Drawable[]{imageview.getDrawable(), bitmapDrawable});
    imageview.setImageDrawable(transitionDrawable);
    imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
    transitionDrawable.startTransition(300);
like image 159
Saket Avatar answered Oct 07 '22 06:10

Saket