5

我将 ImageView 设置为片段的背景(imageview 在宽度和高度上都设置为 match_parent),并且 scaletype 设置为 centerCrop。我下载一个图像以异步方式放入其中,并在完成下载后使用 TransitionDrawable 进行淡入淡出。

图像第一次以正确的 scaletype 出现,但是如果我导航到另一个片段,然后以 imageview 作为背景返回到这个片段,图像会出现扭曲/拉伸(看起来 imageView 设置为 fitXY)

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

是否有保持正确纵横比的 TransitionDrawable 等价物?或者我需要在 TransitionDrawable 上设置什么来防止这种调整大小?

我真正想做的就是在图像之间有一个很好的过渡。我正在考虑在完成动画后将图像设置为 TransitionDrawable 的第二层可绘制对象,但无法监控动画何时完成......

编辑:我想我会提到图像的尺寸不同,不确定这是否会有所不同

编辑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);
4

1 回答 1

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);
于 2014-01-20T15:39:34.383 回答