步骤
找到build.gradle(Modlue:app)
1 导依赖
implementation ‘com.android.support:support-v4:28.0.0’
implementation ‘com.facebook.fresco:fresco:0.12.0’
implementation ‘com.facebook.fresco:animated-gif:0.12.0’
implementation ‘com.facebook.fresco:animated-webp:0.12.0’
implementation ‘com.facebook.fresco:webpsupport:0.12.0’
2新建一个类继承Application
注意:在清单文件里注册App
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);
}
}
3布局
<?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"如下:
xmlns:fresco=“http://schemas.android.com/apk/res-auto”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:orientation=“vertical”
tools:context=".MainActivity"
>
<com.facebook.drawee.view.SimpleDraweeView
fresco:placeholderImage="@drawable/ic_launcher_background"
android:id="@+id/simpleDraweeView"
android:layout_width=“150dp”
android:layout_height=“150dp”
/>
</LinearLayout》
4MainActivity
public class MainActivity extends AppCompatActivity {
private SimpleDraweeView draweeView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
draweeView = findViewById(R.id.simpleDraweeView);
//设置图给draweeView
Uri parse= Uri.parse(“http://img2.ph.126.net/uzguQM3gqa3NcK2XaOD3FA==/6631646108817614243.gif”);
draweeView.setImageURI(parse);
//设置圆形图片
RoundingParams roundingParams = RoundingParams.fromCornersRadius(50f);
roundingParams.setRoundAsCircle(true);
roundingParams.setBorder(Color.BLUE,1);
roundingParams.setOverlayColor(Color.WHITE);
roundingParams.setCornersRadius(30f);
draweeView.getHierarchy().setRoundingParams(roundingParams);
}
}