一、添加依赖
//Fresco implementation 'com.facebook.fresco:fresco:+' //支持动图 implementation 'com.facebook.fresco:animated-gif:+'
二、XML布局
<RelativeLayout xmlns:fresco="http://schemas.android.com/apk/res-auto" 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" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.fresco.MainActivity"> <!--宽高要固定格式--> <!--fresco:failureImage=""加载失败的时候图片 fresco:progressBarImage="@drawable/icon_progress_bar" 加载过程中的图片 fresco:roundedCornerRadius="20dp" 边角圆形弧度 fresco:roundAsCircle="true" 圆形图片 fresco:roundingBorderWidth="3dp" 图片表框宽度 fresco:roundingBorderColor="#FFEE00" 图片边框颜色 --> <com.facebook.drawee.view.SimpleDraweeView android:layout_centerInParent="true" android:id="@+id/simple_drawee_view" android:layout_width="300dp" android:layout_height="wrap_content" fresco:viewAspectRatio="1" fresco:failureImage="@drawable/icon_failure" fresco:progressBarImage="@drawable/icon_progress_bar" fresco:retryImageScaleType="centerInside" fresco:roundedCornerRadius="20dp" fresco:roundTopLeft="false" fresco:roundAsCircle="true" fresco:roundingBorderWidth="3dp" fresco:roundingBorderColor="#FFEE00" /> </RelativeLayout> 三、MainActivity
import android.graphics.drawable.Animatable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.controller.AbstractDraweeController;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.image.ImageInfo;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
public
class MainActivity extends AppCompatActivity {
private SimpleDraweeView simple_drawee_view;
public
static
final String mImageUrl =
"http://pic39.nipic.com/20140226/18071023_164300608000_2.jpg";
public
static
final String mErrorUrl =
"http://pic39.nipic.com/20140226/18071023_1643006080dddd00_2.jpg";
public
static
final String mGifUrl =
"http://img.zcool.cn/community/0139505792e5fc0000018c1bbb7271.gif";
public
static
final String mJpegUrl =
"http://attach.foyuan.net/portal/201308/03/09/2013080309223742492.jpg";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//初始化Fresco
Fresco.initialize(
this);
setContentView(R.layout.activity_main);
Uri uri = Uri.parse(mGifUrl);
simple_drawee_view = (SimpleDraweeView) findViewById(R.id.simple_drawee_view);
//第一种设置图片路径的地址
//simple_drawee_view.setImageURI(uri);
BaseControllerListener<ImageInfo> baseControllerListener =
new BaseControllerListener<ImageInfo>() {
public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) {
animatable.start();
}
public void onFailure(String id, Throwable throwable) {
Toast.makeText(MainActivity.
this,
"加载失败", Toast.LENGTH_SHORT).show();
}
};
//也可以控制图片请求的一些特性
ImageRequest imageRequest = ImageRequestBuilder.newBuilderWithSource(uri)
//设置支持jpeg渐进式展示(从模糊到清晰)
.setProgressiveRenderingEnabled(
true)
.build();
//控制图片加载的一些特性
AbstractDraweeController controller = Fresco.newDraweeControllerBuilder()
.setImageRequest(imageRequest)
//第二种设置图片地址
.setUri(uri)
//设置可以重试 (重试4次)
.setTapToRetryEnabled(
true)
//设置自动播放
.setAutoPlayAnimations(
true)
//监听图片加载
.setControllerListener(baseControllerListener)
.setOldController(simple_drawee_view.getController())
.build();
simple_drawee_view.setController(controller);
}
}