1.安装依赖
//build.gradle
dependencies {
implementation 'com.facebook.fresco:fresco:2.0.0'
...
}
2.添加权限配置
//AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" /> //允许进行网络请求
3.MainActivity
一定要配置在setContentView前面
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//alt+enter自动导入包
Fresco.initialize(this); //添加这一行会报错点击alt+enter,
setContentView(R.layout.activity_main);
}
}
4.在xml布局文件中, 加入命名空间:
<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:layout_height="match_parent"
>
5.加入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"
/>
6.开始加载图片
Uri uri = Uri.parse("https://raw.githubusercontent.com/facebook/fresco/gh- pages/static/logo.png");
SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.my_image_view);
draweeView.setImageURI(uri);
// MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
...
//导入以下几行
Uri uri = Uri.parse("https://raw.githubusercontent.com/facebook/fresco/gh-pages/static/logo.png");
SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.my_image_view);
draweeView.setImageURI(uri);
}
}