Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
Fresco is a powerful library for displaying images in Android applications. It was developed by Facebook and is particularly optimized for displaying large images or many images in a scrolling list or grid without running out of memory.
Here are some of the main features of Fresco:
Below is a simple example of how to use Fresco in an Android app:
First, add the necessary dependencies to your build.gradle
:
implementation 'com.facebook.fresco:fresco:2.5.0' // This version may change over time, always refer to the latest version.
In your Application
class (create one if you haven't), initialize Fresco:
public class MyApp extends Application { @Override public void onCreate() { super.onCreate(); Fresco.initialize(this); } }
Make sure to declare your Application class in the manifest:
<application android:name=".MyApp" ...> ... </application>
SimpleDraweeView
in your XML layout:<com.facebook.drawee.view.SimpleDraweeView android:id="@+id/my_image_view" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
In your activity or fragment:
SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.my_image_view); Uri uri = Uri.parse("https://www.example.com/image.jpg"); draweeView.setImageURI(uri);
Fresco allows for easy customization like setting placeholders, error images, and retry images. For instance:
GenericDraweeHierarchy hierarchy = new GenericDraweeHierarchyBuilder(getResources()) .setPlaceholderImage(R.drawable.placeholder) .setRetryImage(R.drawable.retry) .setErrorImage(R.drawable.error) .build(); draweeView.setHierarchy(hierarchy); draweeView.setImageURI(uri);
With these simple steps, you have Fresco integrated into your Android application. The library offers a lot more advanced features such as image transformations, listeners, customizing caching, and more. Always refer to the official documentation and resources to explore these advanced features.
Integrating Fresco library in Android app example code:
implementation 'com.facebook.fresco:fresco:2.5.0'
Displaying images with Fresco in Android:
SimpleDraweeView
in XML layouts or DraweeView
programmatically.<com.facebook.drawee.view.SimpleDraweeView android:id="@+id/myDraweeView" android:layout_width="wrap_content" android:layout_height="wrap_content" fresco:actualImageUri="https://example.com/image.jpg"/>
Configuring Fresco for efficient image loading:
Fresco.initialize(context, ImagePipelineConfig.newBuilder(context) .setBitmapMemoryCacheParamsSupplier(myBitmapMemoryCacheParamsSupplier) .setMainDiskCacheConfig(myMainDiskCacheConfig) .build());
Loading images from different sources with Fresco:
Uri uri = Uri.parse("https://example.com/image.jpg"); DraweeController controller = Fresco.newDraweeControllerBuilder() .setUri(uri) .build(); myDraweeView.setController(controller);
Setting up placeholder images with Fresco library:
<com.facebook.drawee.view.SimpleDraweeView android:id="@+id/myDraweeView" android:layout_width="wrap_content" android:layout_height="wrap_content" fresco:placeholderImage="@drawable/placeholder_image" fresco:actualImageUri="https://example.com/image.jpg"/>