开源库

Android-Fresco图片库的简单使用

2022-07-11  本文已影响0人  阿博聊编程
图片来源网络,入侵必删
在日常的项目维护中,我们会发现除了使用Glide开源库来加载图片之外,还会使用Fresco开源库来加载图片。这篇博客分享一下Fresco开源库的相关知识,希望对看文章的小伙伴有所启发。

Frescon依赖导入

dependencies {
  implementation 'com.facebook.fresco:fresco:2.6.0'
}

在我写博客的时候,最新的版本是2.6.0。未来可能会有新的版本更新,小伙伴们可以通过Fresco开源地址查看最近版本

Fresco扩展依赖

支持Gif动图,有需要可以添加:

implementation 'com.facebook.fresco:animated-gif:2.6.0'

支持Webp的静态图+动图,有需要可以添加:

implementation 'com.facebook.fresco:animated-webp:2.6.0'
implementation 'com.facebook.fresco:webpsupport:2.6.0'

Fresco初始化

需要在项目的Application当中进行初始化:

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Fresco.initialize(this);
    }
}

初始化一次就够,如果多进程App需要自己做一下处理。

加载图片

Fresco需要使用SimpleDraweeView控件来加载图片:

<com.facebook.drawee.view.SimpleDraweeView
    android:id="@+id/my_image_view"
    android:layout_width="130dp"
    android:layout_height="130dp"
    fresco:placeholderImage="@drawable/my_drawable"/>

代码:

Uri uri = Uri.parse("https://raw.githubusercontent.com/facebook/fresco/gh-pages/static/logo.png");
SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.my_image_view);
draweeView.setImageURI(uri);

这个例子来源官网,简单加载图片之后,Fresco会替你完成:

看到这些介绍,感觉一个非常棒的框架。

上一篇 下一篇

猜你喜欢

热点阅读