【问题标题】:Best practice of showing gif in AndroidAndroid中显示gif的最佳实践
【发布时间】:2023-03-03 05:44:17
【问题描述】:

我尝试加载 gif 列表并在列表视图中显示它们。目前,我将每个 gif 显示到 webview 中,但它很慢而且看起来很丑。我今天做了一些研究,但没有任何帮助。

你们有什么好主意可以流畅地显示 GIF 吗?顺便说一句,我从 url 链接中获得了所有 gif。

【问题讨论】:

标签: android performance view gif animated-gif


【解决方案1】:

我建议使用 Facebook 发布的最近开源的库,名为 fresco https://github.com/facebook/fresco。根据我的经验,它通过在 VM 堆之外使用一些奇特的缓存方法来很好地处理 GIF,以避免 GC 对位图/图像的惊人影响。用户指南:http://frescolib.org/docs/getting-started.html#_

以下只是实现到列表视图中的基本适配器:

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 属性,因为它会破坏内容。

<?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 中:

    @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));

    }

【讨论】:

  • 我正在使用 Fresco 加载 gif。但是当我滚动列表时,加载 gif 比 jpeg 慢得多,我不知道如何推广它
【解决方案2】:

webview 用于 gifimage 加载,无需任何库

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 2015-06-20
    • 1970-01-01
    • 1970-01-01
    • 2012-02-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多