Fresco的使用<一>

引入Fresco

dependencies {
  // 添加依赖
  compile 'com.facebook.fresco:fresco:0.13.0'
}

开始使用 Fresco

1.进行全局初始化,添加网络权限

// 需要在 AndroidManifest.xml 中指定你的 Application 类
// android:name=".MyApplication"
public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        // onCreate()进行初始化
        Fresco.initialize(this);
    }
}

2.布局

<!-- 在xml布局文件中, 加入命名空间:-->
xmlns:fresco="http://schemas.android.com/apk/res-auto"

2.1.使用控件SimpleDraweeView(不使用ImageView)

<com.facebook.drawee.view.SimpleDraweeView
    android:id="@+id/my_image_view"
    android:layout_width="130dp"
    android:layout_height="130dp"
    fresco:placeholderImage="@drawable/my_drawable"
  />

2.2.注意:Drawees 不支持 wrap_content 属性,只能使用固定宽高(xx dp,match_parent),如果想使用wrap_content需要配合viewAspectRatio属性来使用。

<!-- placeholderImage:占位图
     placeholderImageScaleType:占位图缩放类型
     viewAspectRatio:固定宽高比例
     如果希望图片以特定的宽高比例显示,例如 1:1,
     fresco:viewAspectRatio="1"
     如果是 4:3 则改为1.33
     也可以在代码中指定显示比例:
     mSimpleDraweeView.setAspectRatio(1.33f);  -->
<com.facebook.drawee.view.SimpleDraweeView
    android:id="@+id/my_image_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    fresco:placeholderImage="@drawable/my_drawable"
    fresco:placeholderImageScaleType="fitXY"
    fresco:viewAspectRatio="1"
  />

3.开始加载图片:

Uri uri = Uri.parse("https://www.baidu.com/img/bd_logo1.png");
SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.my_image_view);
draweeView.setImageURI(uri);

4.剩下的,Fresco会替你完成:

  • 显示占位图直到加载完成;
  • 下载图片;
  • 缓存图片;
  • 图片不再显示时,从内存中移除;
  • 等等等等。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值