Fresco basic use

Keywords: Android network xml Gradle

Edit the build.gradle file:

dependencies {
  // Other dependencies
  compile 'com.facebook.fresco:fresco:0.12.0'
}

network right

<uses-permission android:name="android.permission.INTERNET" />

The following dependencies need to be added as required:

dependencies {
  // When the machine on API < 14 supports WebP, you need to add
  compile 'com.facebook.fresco:animated-base-support:0.12.0'

  // Support GIF dynamic graph, need to add
  compile 'com.facebook.fresco:animated-gif:0.12.0'

  // Support WebP (static graph + dynamic graph), need to add
  compile 'com.facebook.fresco:animated-webp:0.12.0'
  compile 'com.facebook.fresco:webpsupport:0.12.0'

  // Only WebP static graph is supported, need to add
  compile 'com.facebook.fresco:webpsupport:0.12.0'
}

You must initialize the Fresco class before loading the picture. You only need to call Fresco.initialize once to complete the initialization. It is more suitable to do this in Application (such as the following code). Note that multiple calls to initialize are meaningless.

[MyApplication.java]
public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Fresco.initialize(this);
    }
}

After doing the above, you need to specify your Application class in Android manifest.xml. In order to download the network picture, please confirm that you have declared the permission of the network request.

 <manifest
    ...
    >
    <uses-permission android:name="android.permission.INTERNET" />
    <application
      ...
      android:label="@string/app_name"
      android:name=".MyApplication"
      >
      ...
    </application>
    ...
  </manifest>

In the xml layout file, add the namespace:

<!-- Other elements-->
<!--
XML Namespace provides methods to avoid element naming conflicts xmlns:fresco="http://schemas.android.com/apk/res-auto"
-->
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:fresco="http://schemas.android.com/apk/res-auto"

    android:layout_height="match_parent"
    android:layout_width="match_parent">
//Join SimpleDraweeView:


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

Start loading pictures:

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);

For the rest, Fresco will do it for you:

Display the occupation map until the loading is completed;
Download pictures;
Cache pictures;
When the picture is no longer displayed, it is removed from the memory;
Wait, wait, wait.

Posted by vitalyb on Thu, 30 Apr 2020 15:05:32 -0700