我想对 ImageView 应用淡入动画,以创建从 url 加载的图像在下载完成时淡入的效果。
我知道如何将图像从 url 下载到 ImageView,如 this answer ,我知道如何将淡入动画应用到 imageView like here .
这次尝试
Drawable d = ImageUtils.fetchDrawable("the url");
imageView.setImageDrawable(d);
imageView.startAnimation(fadeInAnimation);
产生闪烁效果(看、看不到、淡入看)。颠倒最后两行的顺序也会导致闪烁。
我用谷歌搜索并搜索了回调/监听器形式的解决方案 - 如下所示:
imageView.setOnLoadCompleteListener...
在 ImageView 中注册加载完成事件,但我没有找到任何类似的东西。
如果能提供有关如何实现此效果的解决方案的任何指示,我将不胜感激。
最佳答案
可以使用:TransitionDrawable,简单代码如下:
// Transition drawable with a transparent drwabale and the final bitmap
final TransitionDrawable td =
new TransitionDrawable(new Drawable[] {
new ColorDrawable(Color.TRANSPARENT),
new BitmapDrawable(mResources, bitmap)
});
// Set background to loading bitmap
imageView.setBackgroundDrawable(
new BitmapDrawable(mResources, mLoadingBitmap));
imageView.setImageDrawable(td);
td.startTransition(FADE_IN_TIME);
关于android - 如何在从url加载的ImageView中淡入图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10416871/