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

Fresco—图片加载框架的简单使用

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

先在Application文件中注册

public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        //Fresco的初始化

        //磁盘缓存的配置
        DiskCacheConfig diskCacheConfig = DiskCacheConfig.newBuilder(this)
                .setBaseDirectoryPath(getCacheDir())
                .setMaxCacheSize(8*1024*1024)
                .build();
        //把磁盘缓存的设置,设置到三级缓存中
        ImagePipelineConfig pipelineConfig = ImagePipelineConfig.newBuilder(this)
                .setMainDiskCacheConfig(diskCacheConfig)
                .build();
        Fresco.initialize(this,pipelineConfig);
    }
}

MainActivity

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.sdv_icon)
    SimpleDraweeView sdvIcon;
    @BindView(R.id.sdv_gifIcon)
    SimpleDraweeView sdvGifIcon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        //加载Fresco图片
        Uri imageUri = Uri.parse("https://ws1.sinaimg.cn/large/0065oQSqgy1fze94uew3jj30qo10cdka.jpg");
        //圆角效果
        RoundingParams roundingParams = RoundingParams.fromCornersRadius(20f);
        sdvIcon.getHierarchy().setRoundingParams(roundingParams);
        //加载显示时的进度条
        sdvIcon.getHierarchy().setProgressBarImage(new ProgressBarDrawable());
        sdvIcon.setImageURI(imageUri);
        //加载Fresco动图
        Uri gifUri = Uri.parse("https://ww1.sinaimg.cn/large/0073sXn7ly1fze96rdfhmg308w0ft7wh");
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setUri(gifUri)
                .setAutoPlayAnimations(true)
                .build();
        sdvGifIcon.setController(controller);

    }
}

activity_main

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/sdv_icon"
        android:layout_width="100dp"
        android:layout_height="100dp"
        alex:placeholderImage="@mipmap/ic_launcher"/>

    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/sdv_gifIcon"
        android:layout_width="200dp"
        android:layout_height="200dp" />