欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

fresco图片框架的应用

程序员文章站 2024-03-17 09:27:04
...

1.添加依赖
implementation ‘com.facebook.fresco:fresco:1.11.0’
// 支持 GIF 动图,需要添加
implementation ‘com.facebook.fresco:animated-gif:1.10.0’
// 支持 WebP (动图),需要添加
implementation ‘com.facebook.fresco:animated-webp:1.10.0’
// 仅支持 WebP 静态图,需要添加
implementation ‘com.facebook.fresco:webpsupport:1.11.0’

2.在加载图片之前,你必须初始化Fresco类,你只需要调用Fresco.initialize一次即可完成初始化,在 Application 里面做这件事再适合不过了

public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Fresco.initialize(this);
}
}
3.指定你的 Application 类
android:name=".MyApplication"
4.不要忘记加网络权限

main页面
public class MainActivity extends AppCompatActivity {

private SimpleDraweeView sim;
private String url = "https://img3.duitang.com/uploads/item/201605/14/20160514165650_RHr3n.gif";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();
    /*Uri uri = Uri.parse("https://img01.sogoucdn.com/net/a/04/link?url=http%3A%2F%2Fimg04.sogoucdn.com%2Fapp%2Fa%2F100520024%2F8a5cac4b4c8819b2b7b3014f952409be&appid=122");*/
    /*sim.setImageURI(uri);*/

    //创建builder对象  --  进度条
    /*GenericDraweeHierarchyBuilder builder = new GenericDraweeHierarchyBuilder(getResources());
    GenericDraweeHierarchy hierarchy = builder.setProgressBarImage(new ProgressBarDrawable()).build();
    sim.setHierarchy(hierarchy);
    sim.setImageURI(uri);*/


    //渐进
   /* AbstractDraweeController request = Fresco.newDraweeControllerBuilder()
            .setUri(url)
            .setAutoPlayAnimations(true)
            .setTapToRetryEnabled(true)
            .build();

    sim.setController(request);*/
}

private void initView() {
    sim = (SimpleDraweeView) findViewById(R.id.sim);
}

}

xml页面
SimpleDraweeView 显示(view)图片
<com.facebook.drawee.view.SimpleDraweeView
android:layout_width=“100dp”
android:layout_height=“100dp”
android:id="@+id/sim"
app:failureImage="@mipmap/ic_launcher"
app:retryImage="@mipmap/ic_launcher_round"
app:placeholderImage="@drawable/ic_launcher_background"
app:roundAsCircle=“true”
app:fadeDuration="5000 />

渐进式JPEG网址
https://blog.csdn.net/Jonly_W/article/details/80244458
使用Fresco网址
https://www.fresco-cn.org/docs/getting-started.html