如何在Android中使用volley下载和加载.gif?

时间:2016-07-09 21:05:33

标签: android android-volley gif android-json

我正在使用这个gif库https://github.com/koral--/android-gif-drawable 要在xml和gifImageView中显示gif作为视图,我在排球中使用ImageRequest从url下载gif。但是gif在app中显示为静止图像。无法弄清楚发生了什么?请帮我解决我的代码或请告诉我如何通过排球下载gif。

定义的变量: -

SwipeDeck cardSwipe;
String url = "";
ImageView iv;
RequestQueue que;
RequestQueue que2;
SwipeCardAdapter adapter;
ArrayList<Bitmap> urls = new ArrayList<>();

这是获取Json的stringRequest函数块

public StringRequest strReq(){

    StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONObject jsonObject = new JSONObject(response);
                JSONArray dataObject = jsonObject.getJSONArray("data");

                for(int i = 0 ; i < dataObject.length() ; i++){
                    JSONObject thisObject = dataObject.getJSONObject(i);
                    JSONObject image = thisObject.getJSONObject("images");
                    JSONObject imageType = image.getJSONObject("original");
                    String imageUrl = imageType.getString("url");
                    Log.d("TAG", imageUrl);
                    URL url = new URL(imageUrl);
                    que2.add(imageReq(imageUrl));

                }

            } catch (JSONException e) {
                e.printStackTrace();
                Log.d("TAG","error occured in try and catch");

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

            if(error.getCause() != null){
                error.getCause().printStackTrace();
            }
            if(error.networkResponse != null){
                Toast.makeText(MainActivity.this, "Network Response is " + error.networkResponse , Toast.LENGTH_SHORT).show();
            }

        }
    });


            return stringRequest;
}

这是从网址下载gif的imageRequest功能块: -

public ImageRequest imageReq(String url){

        ImageRequest imageRequest = new ImageRequest(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {

                urls.add(response);

                adapter.notifyDataSetChanged();



            }
        }, 0, 0, null, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                if(error.getCause() != null){
                    error.getCause().printStackTrace();
                }
                if(error.networkResponse != null){
                    Toast.makeText(MainActivity.this, "Network Response is " + error.networkResponse , Toast.LENGTH_SHORT).show();
                }

            }
        });
        return imageRequest;
    }

并在适配器类中设置gifImageView: -

GifImageView gifImageView = (GifImageView) convertView.findViewById(R.id.gifIV);


            Bitmap thisGif = getItem(position);

            gifImageView.setImageBitmap(thisGif);

1 个答案:

答案 0 :(得分:0)

我不确定为什么你使用GifDrawable库,Glide Library要好得多。 https://github.com/bumptech/glide

Glide.with(mContext).load(image.getMedium())
                .thumbnail(0.5f)
                .crossFade()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(holder.thumbnail);