首先需要加载依赖
implementation 'com.facebook.fresco:fresco:1.9.0'
// 支持 GIF 动图,需要添加
implementation 'com.facebook.fresco:animated-gif:1.9.0'
穿件App继承Application,一定不要忘记在清单文件里注册一下
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
//初始化
Fresco.initialize(this);
}
}
主页面的相关操作
package jiaoyibo.bawei.com.frescolianxi;
import android.graphics.ImageDecoder;
import android.graphics.drawable.Animatable;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
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 mJpegUrl = "http://attach.foyuan.net/portal/201308/03/09/2013080309223742492.jpg";
/**
* 1、SimpleDraweeView宽高必须设置为固定值
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化Fresco
Uri uri = Uri.parse(mJpegUrl);
simple_drawee_view = findViewById(R.id.simple_drawee_view);
//设置监听图片
BaseControllerListener<ImageInfo> baseControllerListener = new BaseControllerListener<ImageInfo>() {
@Override
public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) {
// animatable.start();
}
@Override
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)
//监听图片加载
.setControllerListener(baseControllerListener)
//可节省不必要的内存分配
.setOldController(simple_drawee_view.getController())
.build();
//展示
simple_drawee_view.setController(controller);
}
public void play(View view) {
Animatable animatable = simple_drawee_view.getController().getAnimatable();
animatable.start();
}
}
主页面的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">
<com.facebook.drawee.view.SimpleDraweeView
//将控件置于父控件的中心位置
android:layout_centerInParent="true"
android:id="@+id/simple_drawee_view"
android:layout_width="150dp"
android:layout_height="wrap_content"
//控件纵横比
app:viewAspectRatio="1"
//失败图
app:failureImage="@drawable/ic_launcher_background"
//进度图
app:progressBarImage="@mipmap/ic_launcher"
//进度图自动旋转间隔时间(单位:毫秒ms)
app:progressBarAutoRotateInterval="1000"
//重试图
app:retryImage="@drawable/ic_launcher_background"
重试图的缩放类型
app:retryImageScaleType="centerInside"
//圆角半径
app:roundedCornerRadius="20dp"
//左上角是否圆角
app:roundTopLeft="false"
//设置为原型图
app:roundAsCircle="true"
//圆角边框的宽度
app:roundingBorderWidth="2dp"
//圆角边框的颜色
app:roundingBorderColor="#eeeeee"/>
<TextView
android:onClick="play"
android:text="播放gif"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>