如何在开发Android应用程序时使用Volley Library制作加载的图像渐变

时间:2014-11-29 07:59:47

标签: android listview android-volley networkimageview

我正在使用Volley Library开发一个Android应用程序。

我想将异步中的图像加载到带有Volley的listView Adapter中,加载后我想使图像渐变。

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

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

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;
}

2 个答案:

答案 0 :(得分:0)

占位符是渐变的,因为代码在加载图像时正确执行...但网络图像加载有点迟,即网络I / O完成后图像属性可能会被凌空库代码更改...所以应用相同的渐变代码volley回调方法尝试ImageRequest类而不是NetworkImageView,因为NetworkImageView没有回调方法ImageRequest就此而言

答案 1 :(得分: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的图像处理