android - 使用 ImageView ScaleType 设置 TransitionDrawable

标签 android android-animation android-drawable android-bitmap transitiondrawable

我有这段代码可以淡入淡出 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);
}

ImageView scaleType 在 XML 布局中设置为 centerCrop。但是,当交叉淡入淡出完成时,新位图将设置为 fitXY。在其他线程中,他们说可以通过重置 scaleType 来解决,但它也不起作用。就内存而言,调整位图大小不是合适的解决方案。有没有解决方法来实现这个?非常感谢。

P.S.:请不要建议交叉淡入淡出 2 个 ImageView,或使用 ViewSwitcher,谢谢。

最佳答案

我遇到了同样的问题。问题是 TransitionDrawable 需要两个相同大小的可绘制对象。如果它的大小不同,它会拉伸(stretch)到第一个图像的大小。毕加索已经处理了这个问题。所以我拿了PicassoDrawable来自 Picasso,首先设置占位符,然后设置位图。

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

您可能需要将类和那些方法设置为公共(public)。我还删除了一些只与 Picasso 相关的方法。现在它应该可以正确淡出不同的图像尺寸。

关于android - 使用 ImageView ScaleType 设置 TransitionDrawable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30270997/

相关文章:

android - float Action 按钮启动动画

android - 延迟整个重复的 xml 动画

android - android中的高级滑动菜单

java - 安卓异常说明

android - 使用 MVVM 模式显示和隐藏进度条

android - Android 的位置选取器

android - LayerList 或 LevelList XML 项中非位图可绘制对象的内联定义

Android - 使用 xml 绘制复选图标

android - Google map 中的方向箭头标记

android - AdMob。插屏广告播放时能否保持屏幕常亮?