如何使用 volley 在 android 中下载和加载 .gif?
how to dowload and load .gif in android using volley?
我正在使用这个 gif 库 https://github.com/koral--/android-gif-drawable
为了在 xml 中显示 gif 和 gifImageView 作为视图,我在 volley 中使用 ImageRequest 从 url 下载 gif。但 gif 在应用程序中显示为静止图像。无法弄清楚发生了什么?请帮助我的代码或请告诉我如何通过 volley 下载 gif。
定义的变量:-
SwipeDeck cardSwipe;
String url = "";
ImageView iv;
RequestQueue que;
RequestQueue que2;
SwipeCardAdapter adapter;
ArrayList<Bitmap> urls = new ArrayList<>();
这是要获取的 stringRequest 功能块 Json
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;
}
这是从 url 下载 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;
}
适配器内部的这个 class 设置 gifImageView:-
GifImageView gifImageView = (GifImageView) convertView.findViewById(R.id.gifIV);
Bitmap thisGif = getItem(position);
gifImageView.setImageBitmap(thisGif);
我不太确定您为什么使用 GifDrawable 库,Glide 库要好得多。 https://github.com/bumptech/glide
Glide.with(mContext).load(image.getMedium())
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(holder.thumbnail);
我正在使用这个 gif 库 https://github.com/koral--/android-gif-drawable 为了在 xml 中显示 gif 和 gifImageView 作为视图,我在 volley 中使用 ImageRequest 从 url 下载 gif。但 gif 在应用程序中显示为静止图像。无法弄清楚发生了什么?请帮助我的代码或请告诉我如何通过 volley 下载 gif。
定义的变量:-
SwipeDeck cardSwipe;
String url = "";
ImageView iv;
RequestQueue que;
RequestQueue que2;
SwipeCardAdapter adapter;
ArrayList<Bitmap> urls = new ArrayList<>();
这是要获取的 stringRequest 功能块 Json
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;
}
这是从 url 下载 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;
}
适配器内部的这个 class 设置 gifImageView:-
GifImageView gifImageView = (GifImageView) convertView.findViewById(R.id.gifIV);
Bitmap thisGif = getItem(position);
gifImageView.setImageBitmap(thisGif);
我不太确定您为什么使用 GifDrawable 库,Glide 库要好得多。 https://github.com/bumptech/glide
Glide.with(mContext).load(image.getMedium())
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(holder.thumbnail);