依赖:
// 其他依赖
implementation 'com.facebook.fresco:fresco:1.9.0'
// 支持 GIF 动图,需要添加
implementation 'com.facebook.fresco:animated-gif:1.9.0'
// 支持 WebP (静态图+动图),需要添加
implementation 'com.facebook.fresco:animated-webp:1.9.0'
implementation 'com.facebook.fresco:webpsupport:1.9.0'
在加载图片之前,你必须初始化Fresco
类。你只需要调用Fresco.initialize
一次即可完成初始化,在 Application
里面做这件事再适合不过了(如下面的代码),注意多次的调用初始化是无意义的。
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Fresco.initialize(this);
}
}
权限:
<uses-permission android:name="android.permission.INTERNET" />
命名空间:
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
java代码:
public class MainActivity extends AppCompatActivity {
private SimpleDraweeView sdv1;
private SimpleDraweeView sdv2;
private SimpleDraweeView sdv3;
private String url1="http://7xi8d6.com1.z0.glb.clouddn.com/20171219224721_wFH5PL_Screenshot.jpeg";
private String url2="http://7xi8d6.com1.z0.glb.clouddn.com/20180102083655_3t4ytm_Screenshot.jpeg";
private String url3="http://s6.sinaimg.cn/mw690/006rE0eDzy763Db5syN45&690";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sdv1 = findViewById(R.id.sdv1);
sdv2 = findViewById(R.id.sdv2);
sdv3 = findViewById(R.id.sdv3);
Uri uri1 = Uri.parse(url1);
Uri uri2 = Uri.parse(url2);
// Uri uri3 = Uri.parse("res://drawable/"+R.drawable.aa);//本地资源方式
Uri uri3 = Uri.parse(url3);
//渐进式
ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri1)
.setProgressiveRenderingEnabled(true)
.build();
DraweeController controller = Fresco.newDraweeControllerBuilder()
.setImageRequest(request)
.setOldController(sdv1.getController())
.build();
sdv1.setController(controller);
sdv1.setTranslationX(470f);
sdv1.setTranslationY(70f);
sdv1.setRotation(10f);
//设置为圆角或圆圈
RoundingParams roundingParams = RoundingParams.fromCornersRadius(0f);
roundingParams.setBorder(Color.BLACK, 1.0f);//描边线
// roundingParams.setRoundAsCircle(true);//圆形
// roundingParams.setCornersRadius(100f);//总体圆角
roundingParams.setCornersRadii(50f,100f,150f,200f);//各角不同圆角
sdv2.getHierarchy().setRoundingParams(roundingParams);
sdv2.setImageURI(uri2);
//加载动图
DraweeController controller1 = Fresco.newDraweeControllerBuilder()
.setUri(uri3)
.setTapToRetryEnabled(true)
.setAutoPlayAnimations(true)//设置为true将循环播放Gif动画
.setOldController(sdv3.getController())
.setControllerListener(new BaseControllerListener<ImageInfo>(){
@Override
public void onFinalImageSet(String id, @Nullable ImageInfo imageInfo, @Nullable Animatable animatable) {
super.onFinalImageSet(id, imageInfo, animatable);
Toast.makeText(MainActivity.this, "加载成功", Toast.LENGTH_SHORT).show();
}
})
.build();
sdv3.setController(controller1);
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/sdv1"
android:layout_width="300dp"
android:layout_height="300dp"
fresco:roundedCornerRadius="10dp"
fresco:fadeDuration="800"
app:placeholderImage="@mipmap/ic_launcher"/>
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/sdv2"
android:layout_width="300dp"
android:layout_height="300dp"
app:placeholderImage="@mipmap/ic_launcher"/>
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/sdv3"
android:layout_width="300dp"
android:layout_height="wrap_content"
app:viewAspectRatio="1" //设置宽高比
app:placeholderImage="@mipmap/ic_launcher"/>
</LinearLayout>
</LinearLayout>
简单的封装:
public class FrescoUtil {
/**
* 基础加载图片
* @param url 图片路径
* @param simpleDraweeView 控件
*/
public static void setTu(String url,SimpleDraweeView simpleDraweeView){
Uri uri = Uri.parse(url);
simpleDraweeView.setImageURI(uri);
}
/**
* 渐进式加载图片
* @param url 图片路径
* @param simpleDraweeView 控件
*/
public static void setJianJin(String url, SimpleDraweeView simpleDraweeView){
Uri uri = Uri.parse(url);
ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
.setProgressiveRenderingEnabled(true)
.build();
DraweeController controller = Fresco.newDraweeControllerBuilder()
.setImageRequest(request)
.setOldController(simpleDraweeView.getController())
.build();
simpleDraweeView.setController(controller);
}
/**
* 圆角图片
* @param url 图片路径
* @param simpleDraweeView 控件
* @param radius 角度
* @param color 描边线颜色
* @param width 描边线宽度
*/
public static void setYuanJiao(String url,SimpleDraweeView simpleDraweeView,float radius,int color,float width){
Uri uri = Uri.parse(url);
RoundingParams roundingParams = RoundingParams.fromCornersRadius(0f);
if (width > 0f) {
roundingParams.setBorder(color, width);//描边线
}
roundingParams.setCornersRadius(radius);//总体圆角
simpleDraweeView.getHierarchy().setRoundingParams(roundingParams);
simpleDraweeView.setImageURI(uri);
}
/**
* 圆角图片
* 可控四角角度
* @param url 图片路径
* @param simpleDraweeView 控件
* @param topLeft 左上角
* @param topRight 右上角
* @param bottomRight 右下角
* @param bottomLeft 左下角
* @param color 描边线颜色
* @param width 描边线宽度
*/
public static void setYuanJiao(String url,SimpleDraweeView simpleDraweeView,float topLeft, float topRight, float bottomRight, float bottomLeft,int color,float width){
Uri uri = Uri.parse(url);
RoundingParams roundingParams = RoundingParams.fromCornersRadius(0f);
if (width > 0f) {
roundingParams.setBorder(color, width);//描边线
}
roundingParams.setCornersRadii(topLeft,topRight,bottomRight,bottomLeft);//各角不同圆角
simpleDraweeView.getHierarchy().setRoundingParams(roundingParams);
simpleDraweeView.setImageURI(uri);
}
/**
* 圆形图片
* @param url 图片路径
* @param simpleDraweeView 控件
* @param color 描边线颜色
* @param width 描边线宽度
*/
public static void setYuanQuan(String url,SimpleDraweeView simpleDraweeView,int color,float width){
RoundingParams roundingParams = RoundingParams.fromCornersRadius(0f);
if (width > 0f && color != 0) {
roundingParams.setBorder(color, width);//描边线
}
roundingParams.setRoundAsCircle(true);//圆形
simpleDraweeView.getHierarchy().setRoundingParams(roundingParams);
if(url==null){
simpleDraweeView.setImageURI(url);
}else {
Uri uri = Uri.parse(url);
simpleDraweeView.setImageURI(uri);
}
}
/**
* Gif动态图片
* @param url 图片路径
* @param simpleDraweeView 控件
*/
public static void setDongTu(String url,SimpleDraweeView simpleDraweeView){
Uri uri = Uri.parse(url);
DraweeController controller1 = Fresco.newDraweeControllerBuilder()
.setUri(uri)
.setTapToRetryEnabled(true)
.setAutoPlayAnimations(true)//设置为true将循环播放Gif动画
.setOldController(simpleDraweeView.getController())
.build();
simpleDraweeView.setController(controller1);
}
/**
* 通过imageWidth 的宽度,自动适应高度
* 可更改加载方式
* @param simpleDraweeView 控件
* @param imagePath 图片路径
* @param imageWidth 图片宽度
*/
public static void setControllerListener(final SimpleDraweeView simpleDraweeView, String imagePath, final int imageWidth) {
final ViewGroup.LayoutParams layoutParams = simpleDraweeView.getLayoutParams();
ControllerListener controllerListener = new BaseControllerListener<ImageInfo>() {
@Override
public void onFinalImageSet(String id, @Nullable ImageInfo imageInfo, @Nullable Animatable anim) {
if (imageInfo == null) {
return;
}
int height = imageInfo.getHeight();
int width = imageInfo.getWidth();
layoutParams.width = imageWidth;
layoutParams.height = (int) ((float) (imageWidth * height) / (float) width);
simpleDraweeView.setLayoutParams(layoutParams);
}
@Override
public void onIntermediateImageSet(String id, @Nullable ImageInfo imageInfo) {
Log.d("TAG", "Intermediate image received");
}
@Override
public void onFailure(String id, Throwable throwable) {
throwable.printStackTrace();
}
};
//加载方式
DraweeController controller = Fresco.newDraweeControllerBuilder().setControllerListener(controllerListener).setUri(Uri.parse(imagePath)).build();
simpleDraweeView.setController(controller);
}
/**
* 获取屏幕宽度
* @param context
* @return
*/
public static int getScreenWidth(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
return outMetrics.widthPixels;
}
}