先加入依赖:
implementation 'com.facebook.fresco:fresco:1.9.0' implementation 'com.facebook.fresco:animated-gif:1.9.0' implementation 'com.facebook.fresco:animated-webp:1.9.0' implementation 'com.facebook.fresco:webpsupport:1.9.0'
初始化 fresco:
public class MyApp extends Application { @Override public void onCreate() { super.onCreate ( ); Fresco.initialize ( this ); } }
Manifest 里配置:
android:name=".MyApp"
在main里进行加载获取图片:
//只是显示图片 Uri uri = Uri.parse ( "https://img04.sogoucdn.com/net/a/04/link?url=http%3A%2F%2Fimg01.sogoucdn.com%2Fapp%2Fa%2F100520024%2F0315e6ac636c4b0a554729b6ccfe648b&appid=122" ); simple1.setImageURI ( uri ); /* 或者 Uri uri = Uri.parse ( "https://img04.sogoucdn.com/net/a/04/link?url=http%3A%2F%2Fimg01.sogoucdn.com%2Fapp%2Fa%2F100520024%2F0315e6ac636c4b0a554729b6ccfe648b&appid=122" ); simple1.setImageURI ( uri ); DraweeController controller = Fresco.newDraweeControllerBuilder ( ) .setUri ( uri ) .setAutoPlayAnimations ( true ) .build ( ); simple1.setController ( controller );*/ /* //显示圆角图片 Uri uri = Uri.parse ( "https://img04.sogoucdn.com/net/a/04/link?url=http%3A%2F%2Fimg01.sogoucdn.com%2Fapp%2Fa%2F100520024%2F0315e6ac636c4b0a554729b6ccfe648b&appid=122" ); RoundingParams roundingParams = RoundingParams.fromCornersRadius ( 10f ); roundingParams.setBorder ( R.color.colorAccent,2 ); roundingParams.setRoundAsCircle ( true ); simple1.getHierarchy ( ).setRoundingParams ( roundingParams ); controller = Fresco.newDraweeControllerBuilder ( ) .setUri ( uri ) .setAutoPlayAnimations ( true ) .build ( ); simple1.setController ( controller );*/ /* //动态图片 Uri uri = Uri.parse ( "https://upfile2.asqql.com/upfile/2009pasdfasdfic2009s305985-ts/gif_spic/2018-5/201851420193967995.gif" ); DraweeController controller = Fresco.newDraweeControllerBuilder().setUri(uri).setAutoPlayAnimations(true).build(); simple1.setController(controller);*/ /* //圆角动图 Uri uri = Uri.parse ( "https://upfile2.asqql.com/upfile/2009pasdfasdfic2009s305985-ts/gif_spic/2018-5/201851420193967995.gif" ); simple1.setImageURI ( uri ); DraweeController controller = Fresco.newDraweeControllerBuilder ( ) .setUri ( uri ) .setAutoPlayAnimations ( true ) .build ( ); simple1.setController ( controller ); //圆角 RoundingParams roundingParams = RoundingParams.fromCornersRadius (20f ); roundingParams.setRoundAsCircle ( true ); //圆角边框 roundingParams.setBorder ( Color.BLUE, 3 ); //只是将圆角图以外的地方设置成了黑色 roundingParams.setOverlayColor ( Color.BLACK ); simple1.getHierarchy ( ).setRoundingParams ( roundingParams );*/
android:name=".MyApp"
在布局里写上控件
<com.facebook.drawee.view.SimpleDraweeView android:id="@+id/simple1" android:layout_width="fill_parent" android:layout_height="165dp" />
在Main里进行加载获取
//只是显示图片
Uri uri = Uri.parse ( "https://img04.sogoucdn.com/net/a/04/link?url=http%3A%2F%2Fimg01.sogoucdn.com%2Fapp%2Fa%2F100520024%2F0315e6ac636c4b0a554729b6ccfe648b&appid=122" ); simple1.setImageURI ( uri ); /* 或者 Uri uri = Uri.parse ( "https://img04.sogoucdn.com/net/a/04/link?url=http%3A%2F%2Fimg01.sogoucdn.com%2Fapp%2Fa%2F100520024%2F0315e6ac636c4b0a554729b6ccfe648b&appid=122" ); simple1.setImageURI ( uri ); DraweeController controller = Fresco.newDraweeControllerBuilder ( ) .setUri ( uri ) .setAutoPlayAnimations ( true ) .build ( ); simple1.setController ( controller );*/ /* //显示圆角图片 Uri uri = Uri.parse ( "https://img04.sogoucdn.com/net/a/04/link?url=http%3A%2F%2Fimg01.sogoucdn.com%2Fapp%2Fa%2F100520024%2F0315e6ac636c4b0a554729b6ccfe648b&appid=122" ); RoundingParams roundingParams = RoundingParams.fromCornersRadius ( 10f ); roundingParams.setBorder ( R.color.colorAccent,2 ); roundingParams.setRoundAsCircle ( true ); simple1.getHierarchy ( ).setRoundingParams ( roundingParams ); controller = Fresco.newDraweeControllerBuilder ( ) .setUri ( uri ) .setAutoPlayAnimations ( true ) .build ( ); simple1.setController ( controller );*/ /* //动态图片 Uri uri = Uri.parse ( "https://upfile2.asqql.com/upfile/2009pasdfasdfic2009s305985-ts/gif_spic/2018-5/201851420193967995.gif" ); DraweeController controller = Fresco.newDraweeControllerBuilder().setUri(uri).setAutoPlayAnimations(true).build(); simple1.setController(controller);*/ /* //圆角动图 Uri uri = Uri.parse ( "https://upfile2.asqql.com/upfile/2009pasdfasdfic2009s305985-ts/gif_spic/2018-5/201851420193967995.gif" ); simple1.setImageURI ( uri ); DraweeController controller = Fresco.newDraweeControllerBuilder ( ) .setUri ( uri ) .setAutoPlayAnimations ( true ) .build ( ); simple1.setController ( controller ); //圆角 RoundingParams roundingParams = RoundingParams.fromCornersRadius (20f ); roundingParams.setRoundAsCircle ( true ); //圆角边框 roundingParams.setBorder ( Color.BLUE, 3 ); //只是将圆角图以外的地方设置成了黑色 roundingParams.setOverlayColor ( Color.BLACK ); simple1.getHierarchy ( ).setRoundingParams ( roundingParams );*/
当然了,除此之外还有很多的用法:
在写控件的时候,可以直接对其进行操作的属性:
fresco:fadeDuration=
"300"
// 淡入淡出的持续时间
fresco:actualImageScaleType=
"focusCrop"
// 实际图像的缩放类型
fresco:placeholderImage=
"@color/wait_color"
//占位图
fresco:placeholderImageScaleType=
"fitCenter"
//占位图的缩放类型
fresco:failureImage=
"@drawable/error"
//下载失败显示的图片
fresco:failureImageScaleType=
"centerInside"
//失败图的缩放类型
fresco:retryImage=
"@drawable/retrying"
//图片加载失败时显示,提示用户点击重新加载,重复加载4次还是没有加载出来的时候才会显示failureImage的图片
fresco:retryImageScaleType=
"centerCrop"
//重新加载的图片缩放类型
fresco:progressBarImage=
"@drawable/progress_bar"
//进度条图片
fresco:progressBarImageScaleType=
"centerInside"
fresco:progressBarAutoRotateInterval=
"1000"
//进度图自动旋转间隔时间(单位:毫秒ms)
fresco:backgroundImage=
"@color/blue"
//背景图片,这里的背景图片首先被绘制
fresco:overlayImage=
"@drawable/watermark"
// 设置叠加图,在xml中只能设置一张叠加图片,如果需要多张图片的话,需要在java代码中设置哦
fresco:pressedStateOverlayImage=
"@color/red"
// 设置点击状态下的叠加图
fresco:roundAsCircle=
"false"
//设置为圆形图
fresco:roundedCornerRadius=
"1dp"
// 圆角半径
fresco:roundTopLeft=
"true"
// 左上角是否为圆角
fresco:roundTopRight=
"false"
fresco:roundBottomLeft=
"false"
fresco:roundBottomRight=
"true"
fresco:roundWithOverlayColor=
"@color/corner_color"
fresco:roundingBorderWidth=
"2dp"
//边框的宽度
fresco:roundingBorderColor=
"@color/border_color"
//边框颜色