Android Volley - 如何动画图像加载?

Android Volley - how to animate image loading?(Android Volley - 如何动画图像加载?)

本文介绍了Android Volley - 如何动画图像加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

知道如何在图像加载时播放淡入淡出的动画吗? 现在它只是闪烁到位.我正在使用 Volley 工具包中的 NetworkImageView.

any idea how to play a fade in animation when image loads? Now it just blinks into place. I am using NetworkImageView from the Volley toolkit.

另外,有没有办法在不使用 ImageLoader.get(..) 的情况下在网络图像视图上设置加载和错误位图?

Also, is there a way to set loading and error bitmaps on the network image view without using the ImageLoader.get( .. ) ?

谢谢!

//EDIT:好的,谢谢大家,但是如果我们想成为完美主义者,我们应该只在从磁盘缓存加载时制作动画,覆盖 setImageBitmap 会导致动画消失即使从 memcache中拉出

//EDIT: Okay, thanks to you all, but if we want to be perfectionists, we should only animate if loading from disk cache, overriding setImageBitmap would case animation to go off even if pulled from memcache

你想要做的是像这样向 ImageListener.onResponse 添加一个布尔值 shouldAnimate

what you want to do is add a boolean shouldAnimate to ImageListener.onResponse like this

public static ImageListener getImageListener(final ImageView view, final int defaultImageResId,
        final int errorImageResId) {
    return new ImageListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            if (errorImageResId != 0) {
                view.setImageResource(errorImageResId);
            }
        }

        @Override
        public void onResponse(ImageContainer response, boolean isImmediate, boolean shouldAnimate) {
            if (response.getBitmap() != null) {
                if (shouldAnimate) {
                    // ADDED
                    view.setAlpha(0f);
                    view.setImageBitmap(response.getBitmap());
                    view.animate().alpha(1f).setDuration(1000);
                    // END ADDED
                } else {
                    view.setImageBitmap(response.getBitmap());
                }
            } else if (defaultImageResId != 0) {
                view.setImageResource(defaultImageResId);
            }
        }
    };
}

这是一个设置位图的方法,不管它来自哪里,所以你需要将它设置为 false 到 ImageLoader 中的每个用法,除了

this is a method that sets the bitmap, no matter where it is from, so you need to set it to false to every usage in ImageLoader except for

class BatchedImageRequest {

   private void batchResponse(String cacheKey, BatchedImageRequest request,
        final VolleyError error) {
      ...
      container.mListener.onResponse(container, false, true);
      ...
   }
}

我已经为 Copy &粘贴用法 - https://gist.github.com/ursusursus/5732521

推荐答案

CommonsWare 答案的示例实现可以在这里找到:https://gist.github.com/benvd/5683818.

Example implementation of CommonsWare's answer can be found here: https://gist.github.com/benvd/5683818.

使用 TransitionDrawable 确实增加了一层额外的过度绘制.如果您想避免这种情况,也许使用 ViewPropertyAnimator 可能会有所帮助.

Using a TransitionDrawable does add an extra layer of overdraw. If you want to avoid that, perhaps using a ViewPropertyAnimator might help.

它的要点基本上是在您的 setImageBitmap() 中有以下代码段:

The gist of it is basically to have the following snippet in your setImageBitmap():

TransitionDrawable td = new TransitionDrawable(new Drawable[]{
        new ColorDrawable(android.R.color.transparent),
        new BitmapDrawable(getContext().getResources(), bm)
});

setImageDrawable(td);
td.startTransition(FADE_IN_TIME_MS);

这篇关于Android Volley - 如何动画图像加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Android Volley - 如何动画图像加载?

基础教程推荐