Android Load Image Using Fresco

Jul 10, 2019

Android Image Loading Library

  • Fresco by Facebook, last release in July 4, 2019
  • Glide by Google, last release in Feb 15, 2019
  • Picasso - last release in May 8, 2018

Dependencies

dependencies {
  // your app's other dependencies
  implementation 'com.facebook.fresco:fresco:2.0.0'
}

NOTE: Optional packages for animated gif, webp, etc.

Initialize

class LuaApp: Application() {    override fun onCreate() {        super.onCreate()        Fresco.initialize(this)    }}

SimpleDraweeView as ImageView

<FrameLayout
    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"
    >

    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/draweeView"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_gravity="center"
        fresco:placeholderImage="@drawable/img_placeholder_300dp"
        />
</FrameLayout>

NOTE: Full list of SimpleDraweeView XML attributes.

Load Image

Resouce ID

val uri = UriUtil.getUriForResourceId(R.drawable.sample_image)draweeView.setImageURI(uri)

NOTE: This does not work for vector image. Refer to Android Fresco Load Vector Drawable From Uri.

By URL

val uri = Uri.parse("https://raw.githubusercontent.com/facebook/fresco/master/docs/static/logo.png")draweeView.setImageURI(uri)

WebP support

WebP support is enabled by default on Android 4.0 (API level 14), while lossless and transparent WebP images are supported in Android 4.3 (API level 18), same as Android WebP Support.

To use WebP on older version, include the following.

dependencies {
  implementation 'com.facebook.fresco:webpsupport:2.0.0'
}

Proguard

Since Fresco 1.9.0 a ProGuard configuration file is included in Fresco itself which is automatically applied if you enable ProGuard for your app.

References:

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.