5

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);
AndroidNoob
  • 2,653
  • 2
  • 37
  • 51
  • where do you use TD? as a background Drawable or as a image Drawable? – pskink Oct 15 '13 at 13:27
  • I create the TransitionDrawable with the placeholder drawable that exists in the ImageView already (using ImageView.getDrawable() ), and the newly downloaded Bitmap (as a BitmapDrawable) then use ImageView.setImageDrawable() to set the transition drawable. – AndroidNoob Oct 15 '13 at 14:52
  • have you used setBackgroundDrawable? if so, dont do that as it strecthes a Drawable – pskink Oct 15 '13 at 14:58
  • No it was definitely ImageView.setImageDrawable(Drawable). It works if I don't use TransitionDrawable (i.e. just set my downloaded bitmap straight into the ImageView) the scaletype appears to stay as it should, and even using a TransitionDrawable the first time works, it's only if I navigate to another fragment then back that the scaletype seems to go awry if the ImageView holds a TransitionDrawable – AndroidNoob Oct 15 '13 at 15:37
  • post the code how you do that – pskink Oct 15 '13 at 15:48
  • posted code above for when i set the TransitionDrawable – AndroidNoob Oct 15 '13 at 15:58

1 Answers1

7

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);
Saket
  • 2,527
  • 1
  • 25
  • 31