Shumin Gao Shumin Gao - 10 months ago 65
Android Question

Best practice of showing gif in Android

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.

Do you guys have any great idea that showing gifs smoothly? BTW, I got all gifs from url links.

Answer Source

I would recommend utilising a recently open sourced library, released by Facebook, called 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:

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.


    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;

            vi = inflater.inflate(R.layout.fresco_item, null);
            //Load Uri with URL.
            Uri uri = Uri.parse(getItem(position));

            SimpleDraweeView draweeView = (SimpleDraweeView) vi.findViewById(;

            //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()
            //Set the DraweeView controller, and you should be good to go.


        return vi;


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=""


And then in your onCreate:

    protected void onCreate(Bundle savedInstanceState) {
        //Required by Fresco to be initalized before setContentView.
        ListView lv = (ListView) findViewById(;
        ArrayList<String> urlList = new ArrayList<>();
        //Load neon cats
        for(int i = 0; i < 10; i++) {

        lv.setAdapter(new FrescoGifAdapter(this, urlList));