显示Android的GIF的最佳实践 [英] Best practice of showing gif in Android
问题描述
我试图加载GIF格式的列表,并显示在列表视图。目前,我显示的每个GIF成的WebView但它的速度很慢,看起来难看。我做了一些研究,但今天没有什么能够帮助我。
I tried to load a list of gifs and displays them on listview. Currently, I display each gif into a webview but it's very slow and looks ugly. I did some researches today but nothing could help me.
难道你们有什么伟大的想法,呈现平稳的GIF?顺便说一句,我把所有的GIF从URL链接。
Do you guys have any great idea that showing gifs smoothly? BTW, I got all gifs from url links.
推荐答案
我会建议利用近期开源库,被Facebook发布的,被称为壁画的 https://github.com/facebook/fresco 。从我的经验,它通过使用一些花哨的高速缓存方法VM堆之外,以避免GC对位图/图像效果惊人的GIF处理相当不错。用户指南: http://frescolib.org/docs/getting -started.html#_
I would recommend utilising a recently open sourced library, released by Facebook, called fresco https://github.com/facebook/fresco. From my experience, it handles GIFs quite nicely by using some fancy caching methods outside of VM heap to avoid staggering effects of GC on bitmaps/images. User guide: http://frescolib.org/docs/getting-started.html#_
下面就是落实到一个列表视图基本适配器:
Below is just a basic adapter to implement into a listview:
public class FrescoGifAdapter extends BaseAdapter {
ArrayList<String> urlList;
private static LayoutInflater inflater=null;
public FrescoGifAdapter(Activity activity, ArrayList<String> urlList) {
if(urlList==null) {
this.urlList = new ArrayList<>();
}
else {
this.urlList = urlList;
}
inflater = (LayoutInflater)activity.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return urlList.size();
}
public String getItem(int position) {
return urlList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if(convertView==null){
vi = inflater.inflate(R.layout.fresco_item, null);
//Load Uri with URL.
Uri uri = Uri.parse(getItem(position));
SimpleDraweeView draweeView = (SimpleDraweeView) vi.findViewById(R.id.my_image_view);
//Controller is required for controller the GIF animation, here I have just set it to autoplay as per the fresco guide.
DraweeController controller = Fresco.newDraweeControllerBuilder()
.setUri(uri)
.setAutoPlayAnimations(true)
.build();
//Set the DraweeView controller, and you should be good to go.
draweeView.setController(controller);
}
return vi;
}
}
fresco_item.xml (你可以改变尺寸以适应,我只是默认,指导尺寸)。需要注意的是比例已经由壁画进行处理。事实上,他们建议不要设置任何自定义的的ImageView 的属性,因为它打破的东西。
fresco_item.xml (You can change dimensions to suit, I just defaulted to guide dimensions). Caveat is scaling has to be handled by fresco. In fact, they advise against setting any custom ImageView attributes as it breaks stuff.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/my_image_view"
android:layout_width="130dp"
android:layout_height="130dp"
fresco:placeholderImage="@drawable/ic_launcher"
/>
</LinearLayout>
然后在你的onCreate:
And then in your onCreate:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Required by Fresco to be initalized before setContentView.
Fresco.initialize(this);
setContentView(R.layout.activity_main);
ListView lv = (ListView) findViewById(R.id.listview);
ArrayList<String> urlList = new ArrayList<>();
//Load neon cats
for(int i = 0; i < 10; i++) {
urlList.add("https://media3.giphy.com/media/JLQUx1mbgv2hO/200.gif");
}
lv.setAdapter(new FrescoGifAdapter(this, urlList));
}
这篇关于显示Android的GIF的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!