引入Fresco
编辑 build.gradle 文件:
compile 'com.facebook.fresco:fresco:0.12.0'
// 在 API < 14 上的机器支持 WebP 时,需要添加
compile 'com.facebook.fresco:animated-base-support:0.12.0'
// 支持 GIF 动图,需要添加
compile 'com.facebook.fresco:animated-gif:0.12.0'
// 支持 WebP (静态图+动图),需要添加
compile 'com.facebook.fresco:animated-webp:0.12.0'
compile 'com.facebook.fresco:webpsupport:0.12.0'
// 仅支持 WebP 静态图,需要添加
compile 'com.facebook.fresco:webpsupport:0.12.0'
在加载图片之前,你必须初始化Fresco类。你只需要调用Fresco.initialize一次即可完成初始化,在 Application 里面初始化
package com.zhoujian.fresco;
import android.app.Application;
import com.facebook.drawee.backends.pipeline.Fresco;
/**
* Created by zhoujian on 2017/1/7.
*/
public class AppApplication extends Application
{
@Override
public void onCreate()
{
super.onCreate();
Fresco.initialize(this);
}
}
做完上面的工作后,你需要在 AndroidManifest.xml 中指定你的 Application 类。并设置网络权限
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zhoujian.fresco">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:name=".AppApplication"
android:theme="@style/AppTheme">
<activity android:name=".activity.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
在xml布局文件中, 加入命名空间:
xmlns:fresco="http://schemas.android.com/apk/res-auto"
在布局中使用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/fresco_imgeview"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
fresco:viewAspectRatio="1.33"
fresco:placeholderImage="@mipmap/image_default"
fresco:fadeDuration="300"
fresco:actualImageScaleType="focusCrop"
fresco:placeholderImageScaleType="fitCenter"
fresco:failureImage="@mipmap/image_default"
fresco:failureImageScaleType="centerInside"
fresco:retryImage="@mipmap/image_default"
fresco:retryImageScaleType="centerCrop"
fresco:progressBarImage="@mipmap/image_default"
fresco:progressBarImageScaleType="centerInside"
fresco:progressBarAutoRotateInterval="1000"
fresco:backgroundImage="@color/colorAccent"
fresco:roundAsCircle="false"
fresco:roundedCornerRadius="20dp"
fresco:roundTopLeft="true"
fresco:roundTopRight="true"
fresco:roundBottomLeft="true"
fresco:roundBottomRight="true"
fresco:roundWithOverlayColor="#eeeeee"
fresco:roundingBorderWidth="1dp"
fresco:pressedStateOverlayImage="@android:color/transparent"/>
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/fresco_imgeview1"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
fresco:viewAspectRatio="1.33"
fresco:placeholderImage="@mipmap/image_default"/>
</LinearLayout>
在代码中加载
package com.zhoujian.fresco.activity;
import android.app.Activity;
import android.os.Bundle;
import com.facebook.drawee.view.SimpleDraweeView;
import com.zhoujian.fresco.R;
import butterknife.BindView;
import butterknife.ButterKnife;
public class MainActivity extends Activity
{
@BindView(R.id.fresco_imgeview)
SimpleDraweeView mFrescoImgeview;
@BindView(R.id.fresco_imgeview1)
SimpleDraweeView mFrescoImgeview1;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(MainActivity.this);
setImage();
}
private void setImage()
{
mFrescoImgeview.setImageURI("http://image.tianjimedia.com/uploadImages/2014/339/38/M7K1817T9KU1.jpg");
mFrescoImgeview1.setImageURI("http://img5.imgtn.bdimg.com/it/u=3432186896,3159823243&fm=21&gp=0.jpg");
}
}
运行效果截图:
源码下载