1.安装依赖

  1. //build.gradle
  2. dependencies {
  3. implementation 'com.facebook.fresco:fresco:2.0.0'
  4. ...
  5. }

2.添加权限配置

  1. //AndroidManifest.xml
  2. <uses-permission android:name="android.permission.INTERNET" /> //允许进行网络请求

3.MainActivity

一定要配置在setContentView前面

  1. public class MainActivity extends AppCompatActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. //alt+enter自动导入包
  6. Fresco.initialize(this); //添加这一行会报错点击alt+enter,
  7. setContentView(R.layout.activity_main);
  8. }
  9. }

4.在xml布局文件中, 加入命名空间:

  1. <LinearLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android" //这两行
  3. xmlns:fresco="http://schemas.android.com/apk/res-auto"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. >

5.加入SimpleDraweeView:

替换掉ImageView模块

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

6.开始加载图片

  1. Uri uri = Uri.parse("https://raw.githubusercontent.com/facebook/fresco/gh- pages/static/logo.png");
  2. SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.my_image_view);
  3. draweeView.setImageURI(uri);
  1. // MainActivity
  2. public class MainActivity extends AppCompatActivity {
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. ...
  6. //导入以下几行
  7. Uri uri = Uri.parse("https://raw.githubusercontent.com/facebook/fresco/gh-pages/static/logo.png");
  8. SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.my_image_view);
  9. draweeView.setImageURI(uri);
  10. }
  11. }