Skip to main content

Fresco Android Tutorial

In this tutorial you will learn how to use fresco android image library.

Fresco is an image library created by Facebook developers that can be used to display image from internet or local storage. Many popular android apps like facebook, twitter, wikipedia, 9gag, etc uses this library. If you want to create an app that loads lot of image from internet then this library is the best choice. It is great alternative of other android image libraries Picasso and Glide.

It manages memory so efficiently that app works faster and causes less crashes. Using this you can display jpg, png, gif and WebPs. Below I have shown usage of this library using one simple example.

Fresco Android Tutorial

Fresco Android Tutorial

Create an android studio project with package name com.frescoandroid.

Add following dependency in app level build.gradle file under dependencies section then sync the project.

compile 'com.facebook.fresco:fresco:1.5.0'

Note: If you want to display gif or WebPs then you need to include some more dependencies. To learn about them visit this link.

Create java class with name MyApplication that extend Application class. Before using Fresco in our project we have to initialize it, this class is used for initialization purpose. In this way we have to initialize it once and then we can use the library throughout the project.

MyApplication.java

package com.frescoandroid;

import android.app.Application;

import com.facebook.drawee.backends.pipeline.Fresco;

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

Put this class entry in AndroidManifest.xml file. For that add android:name=”.MyApplication” attribute inside <application> tag.

As I will load image from internet so add internet access permission in AndroidManifest.xml.

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

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:fresco="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.frescoandroid.MainActivity">\

    <com.facebook.drawee.view.SimpleDraweeView
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:id="@+id/image"
        fresco:placeholderImage="@drawable/loading"
        />

</RelativeLayout>

For displaying the image we use <com.facebook.drawee.view.SimpleDraweeView> element in layout file. Also mention custom namespace in top level element by adding xmlns:fresco=”http://schemas.android.com/apk/res-auto” attribute.

Here I have also used placeholder image, that will be displayed until image is downloaded from internet. Make sure to place a placeholder image in drawable folder.

MainActivity.java

package com.frescoandroid;

import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.facebook.drawee.view.SimpleDraweeView;

public class MainActivity extends AppCompatActivity {
    SimpleDraweeView image;
    String url ="https://upload.wikimedia.org/wikipedia/en/thumb/8/80/Wikipedia-logo-v2.svg/1200px-Wikipedia-logo-v2.svg.png";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        image = (SimpleDraweeView)findViewById(R.id.image);

        //loading image from url
        Uri uri = Uri.parse(url);
        image.setImageURI(uri);
    }
}

To show the image you just have to set the image in SimpleDraweeView ui element using setImageURI() method. Rest of the code is self explanatory. Finally save and run the project.

Screenshot

Fresco Android Tutorial Screenshot

To learn more functionalities of fresco library visit its official website http://frescolib.org/.

Comment below if you have doubts related to above fresco android tutorial.

The post Fresco Android Tutorial appeared first on The Crazy Programmer.



from The Crazy Programmer https://www.thecrazyprogrammer.com/2017/09/fresco-android-tutorial.html

Comments

Popular posts from this blog

dotnet sdk list and dotnet sdk latest

Can someone make .NET Core better with a simple global command? Fanie Reynders did and he did it in a simple and elegant way. I'm envious, in fact, because I spec'ed this exact thing out in a meeting a few months ago but I could have just done it like he did and I would have used fewer keystrokes! Last year when .NET Core was just getting started, there was a "DNVM" helper command that you could use to simplify dealing with multiple versions of the .NET SDK on one machine. Later, rather than 'switching global SDK versions,' switching was simplified to be handled on a folder by folder basis. That meant that if you had a project in a folder with no global.json that pinned the SDK version, your project would use the latest installed version. If you liked, you could create a global.json file and pin your project's folder to a specific version. Great, but I would constantly have to google to remember the format for the global.json file, and I'd constan

R vs Python for Machine Learning

There are so many things to learn before to choose which language is good for Machine Learning. We will discuss each and everything about R as well as Python and the situation or problem in which situation we have to use which language. Let’s start Python and R are the two most Commonly used Programming Languages for Machine Learning and because of the popularity of both the languages Novice or you can say fresher are getting confused, whether they should choose R or Python language to commence their career in the Machine learning domain. Don’t worry guys through this article we will discuss R vs Python for Machine Learning. So, without exaggerating this article let’s get started. We will start it from the very Basics things or definitions. R vs Python for Machine Learning Introduction R is a programming language made by statisticians and data miners for statistical analysis and graphics supported by R foundation for statistical computing. R also provides high-quality graphics and

Top Tips For PCB Design Layout

Are you thinking about designing a printed circuit board? PCBs are quite complicated, and you need to make sure that the layout that you choose is going to operate as well as you want it to. For this reason, we have put together some top tips for PCB design layout. Keep reading if you would like to find out more about this. Leave Enough Space One of the most important design tips for PCB layout is that you need to make sure that you are leaving enough space between the components. While many people might think that packing components closely is the best route to take, this can cause problems further down the line. This is why we suggest leaving extra space for the wires that will spread. This way, you’ll have the perfect PCB design layout. Print Out Your Layout Struggling to find out if your components sizes match? Our next tip is to print out your layout and compare the printed version to your actual components. Datasheets can sometimes come with errors, so it doesn’t hurt to do