【问题标题】:Android TransitionDrawable: image scaletype changes upon fragment resumingAndroid TransitionDrawable:片段恢复时图像比例类型发生变化
【发布时间】:2013-10-15 13:02:16
【问题描述】:

我将 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);

【问题讨论】:

  • 您在哪里使用 TD?作为背景 Drawable 还是作为图像 Drawable?
  • 我使用 ImageView 中已经存在的占位符可绘制对象创建了 TransitionDrawable(使用 ImageView.getDrawable() ),然后新下载的位图(作为 BitmapDrawable)使用 ImageView.setImageDrawable() 设置过渡drawable。
  • 你用过setBackgroundDrawable吗?如果是这样,请不要这样做,因为它会拉伸 Drawable
  • 不,肯定是 ImageView.setImageDrawable(Drawable)。如果我不使用TransitionDrawable(即只是将我下载的位图直接设置到ImageView中)它可以工作,scaletype似乎保持原样,即使第一次使用TransitionDrawable也有效,只有当我导航到另一个片段然后返回如果 ImageView 持有一个 TransitionDrawable,那么 scaletype 似乎会出错
  • 贴出你的代码

标签: android animation android-fragments transitiondrawable


【解决方案1】:

您是否使用不同尺寸的图像?这可能是导致问题的原因。尝试使用相同尺寸的图片。

编辑:找到解决方案:使用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);

【讨论】:

  • 不工作。图片缩小到上一张图片的大小。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多