0

我正在开发一个带有 Volley 库的 android 应用程序。

我想用 Volley 将图像异步加载到 listView 适配器中,加载后我想使图像渐变。

我的代码如下。它不好用。占位符图像变为渐变,但加载的图像不会。

你能告诉我如何解决这个问题吗?

public View getView(int position, View convertView, ViewGroup parent) {

    if (null == convertView) 
        convertView = mInflater.inflate(R.layout.main_list_detail, null);
    if (imageLoader == null)
        imageLoader = AppController.getInstance().getImageLoader();

    String mUrl = "http://....";

    NetworkImageView imageView = (NetworkImageView)convertView.findViewById(R.id.image);

    imageView.setDefaultImageResId(R.drawable.placeholder);
    imageView.setErrorImageResId(R.drawable.placeholder);
    imageView.setImageUrl(mURL, imageLoader);

    GradientDrawable gd = new GradientDrawable(
        GradientDrawable.Orientation.TOP_BOTTOM,
        new int[] {Color.parseColor("#81a001"), Color.parseColor("#455600")});
        gd.setStroke(1, Color.parseColor("#455600"));

    imageView.setImageDrawable(gd);

    return convertView;
}
4

2 回答 2

0

占位符是渐变的,因为加载图像时代码会正确执行......但是网络图像加载有点晚,即当网络 I/O 完成并且图像属性可能会被 volley 库代码更改......所以应用相同的渐变代码截击回调方法尝试使用 ImageRequest 类而不是 NetworkImageView,因为 NetworkImageView 没有回调方法 ImageRequest 是否适用

于 2014-11-29T08:48:31.220 回答
0
    public class imgloaderList implements ImageListener{

            @Override
            public void onErrorResponse(VolleyError error) {
                //error handling code
            }

            @Override
            public void onResponse(ImageContainer response, boolean isImmediate) {
              // code when your network I/O is completed
             // coding style in not properly followed i hope you can fix that 
                GradientDrawable gd = new GradientDrawable(
                        GradientDrawable.Orientation.TOP_BOTTOM,
                        new int[] {Color.parseColor("#81a001"), Color.parseColor("#455600")});
                        gd.setStroke(1, Color.parseColor("#455600"));
                imageView.setImageBitmap(response.getBitmap());
                imageView.setImageDrawable(gd);
            }

        }
        public View getView(int position, View convertView, ViewGroup parent) {

            if (null == convertView) 
                convertView = mInflater.inflate(R.layout.main_list_detail, null);
            ImageLoader imageLoader;
            if (imageLoader == null)
                imageLoader = AppController.getInstance().getImageLoader();

            String mUrl = "http://....";

            NetworkImageView imageView = (NetworkImageView)convertView.findViewById(R.id.image);

            imageView.setDefaultImageResId(R.drawable.placeholder);
            imageView.setErrorImageResId(R.drawable.placeholder);

//provide your own custom ImageLoader.ImageListener as you can see onResponse method is //overridden apply gradient in onResponse method, plz handle the bitmap caching
            imageView.setImageUrl(mURL, new imgloaderList());

            GradientDrawable gd = new GradientDrawable(
                GradientDrawable.Orientation.TOP_BOTTOM,
                new int[] {Color.parseColor("#81a001"), Color.parseColor("#455600")});
                gd.setStroke(1, Color.parseColor("#455600"));

            imageView.setImageDrawable(gd);

            return convertView;
        }

提供自定义 ImageLoader.ImageListener;在覆盖 onResponse 方法,您可以在其中进行后期网络 I/O 的图像处理

于 2014-12-01T07:28:22.137 回答