导入依赖
implementation 'com.facebook.fresco:fresco:0.13.0'//fresco依赖
implementation 'com.facebook.fresco:animated-gif:0.13.0'//gif动图加载
初始化
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
Fresco.initialize(this);//初始化Fresco
}
}
写入图片和动图的路径
//图片路径
Uri uri = Uri.parse("http://h.hiphotos.baidu.com/image/pic/item/ca1349540923dd54e1964cb2dc09b3de9d824807.jpg");
//动图路径
String URL = "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3064674694,445703165&fm=26&gp=0.jpg";
布局控件
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/DraweeView"
android:layout_width="300dp"
android:layout_height="wrap_content"
/>
设置图片的比例
DraweeView = findViewById(R.id.DraweeView);
DraweeView.setAspectRatio(1f);//设置默认比例
DraweeView.setImageURI(uri);//必须的属性
现在开始步入正题:
圆角
/**
* 圆角20f
* @param view
*/
public void yuan(View view) {
RoundingParams roundingParams = RoundingParams.fromCornersRadius(20f);
DraweeView.setAspectRatio(1f);//设置默认比例
DraweeView.getHierarchy().setRoundingParams(roundingParams);//设置圆角框架
DraweeView.setImageURI(uri);//设置图片
}
圆形
/**
* 圆形
* @param view
*/
public void yuan2(View view) {
RoundingParams roundingParams = RoundingParams.fromCornersRadius(20f);
roundingParams.setRoundAsCircle(true);
DraweeView.setAspectRatio(1f);//设置默认比例
DraweeView.getHierarchy().setRoundingParams(roundingParams);//设置圆角框架
DraweeView.setImageURI(uri);//设置图片
}
比例
/**
* 比例
* @param view
*/
public void bili(View view) {
DraweeView.setAspectRatio(2f);//设置比例为默认的二倍
}
渐进
/**
* 渐进
* @param view
*/
public void jianjin(View view) {
AbstractDraweeController controller = Fresco.newDraweeControllerBuilder()
.setUri(uri)
.build();
DraweeView.setController(controller);
}
加载动图
/**
* 加载动图
* @param view
*/
public void gif(View view) {
AbstractDraweeController controller = Fresco.newDraweeControllerBuilder()
.setUri(URL)
.setAutoPlayAnimations(true)
.build();
DraweeView.setController(controller);
}