Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set TransitionDrawable with ImageView ScaleType

I have got this code to crossfade the background of an ImageView:

private void setWithCrossfade(Bitmap bitmap) {
    //create drawable vector
    Drawable backgrounds[] = new Drawable[2];
    //getting first image
    backgrounds[0] = mImageView.getDrawable();
    backgrounds[1] = new BitmapDrawable(mImageView.getResources(), bitmap);

    TransitionDrawable transitionDrawable = new TransitionDrawable(backgrounds);
    transitionDrawable.setCrossFadeEnabled(true);

    mImageView.setAdjustViewBounds(false);
    mImageView.setImageDrawable(transitionDrawable);
    //it is needed to reset scale type
    mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    //start crossfading
    transitionDrawable.startTransition(250);
}

The ImageView scaleType is set to centerCrop in the XML layout. However, when the crossfade finishes, the new bitmap is set to fitXY. In other thread they say it can be solved by resetting the scaleType, but it is not working either. Resizing the bitmap is not a proper solution in terms of memory. Is there any workaround to implement this? Thank you very much.

P.S.: Please, do not suggest crossfading 2 ImageViews, or using a ViewSwitcher thanks.

like image 852
Quark Avatar asked Oct 19 '22 11:10

Quark


1 Answers

I had the same problem. The problem is that a TransitionDrawable needs two drawables of the same size. If it is not the same size it stretches to the size of the first Image. Picasso already handles this. So I took the PicassoDrawable from Picasso and first set the placeholder and then the bitmap.

PicassoDrawable.setPlaceholder(imageView, placeholderDrawable);
PicassoDrawable.setBitmap(imageView, context, bitmap);

You may need to set the class and those methods to public. Also I removed some methods only relevant for Picasso. Now it should fade correctly with different image sizes.

like image 104
Kevin van Mierlo Avatar answered Nov 15 '22 05:11

Kevin van Mierlo