Android TransitionDrawable:图片缩放类型在片段恢复时发生变化

And*_*oob 4 animation android android-fragments transitiondrawable

我有一个ImageView设置为片段的背景(imageview在宽度和高度上都设置为match_parent),scaletype设置为centerCrop.我下载了一个异步放入的图像,并在完成下载后使用TransitionDrawable进行交叉淡入淡出.

第一次使用正确的缩放类型显示图像,但是如果我导航到另一个片段然后以imageview作为背景返回到此片段,则图像显示为变形/拉伸(看起来像imageView设置为fitXY)

如果我不使用TransitionDrawable,图像会保持原样.

是否有一个可保持正确宽高比的TransitionDrawable等效项?或者我是否需要在TransitionDrawable上设置以防止此大小调整?

我真正想做的就是在图像之间进行很好的转换.我想只要将图像设置为TransitionDrawable的第二层drawable一旦完成动画,但是没有办法监视动画何时完成...

编辑:我以为我会提到图像尺寸不一样,不确定这是否有所不同

编辑2:这是设置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);
Run Code Online (Sandbox Code Playgroud)

Sak*_*ket 7

您使用的是不同尺寸的图像吗?这可能导致了这个问题.尝试使用相同尺寸的图像.

编辑:找到解决方案:使用该setImageDrawable命令后,需要再次设置ImageView的scaletype .

    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);
Run Code Online (Sandbox Code Playgroud)