Android Fresco的使用

一、添加依赖

//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";
@Override
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>() {
@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)
//第二种设置图片地址
.setUri(uri)
//设置可以重试 (重试4次)
.setTapToRetryEnabled( true)
//设置自动播放
.setAutoPlayAnimations( true)
//监听图片加载
.setControllerListener(baseControllerListener)
.setOldController(simple_drawee_view.getController())
.build();
simple_drawee_view.setController(controller);
}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值