Image loading in Android
To load an image in android requires you to read the stream into a bitmap and which then can be used to display the image. But this cannot be done in the main UI thread so you will have to use Async task. This requires you to write a lot of code. Besides you have to handle memory and disk caching mechanism. An easy and quick way is to use the Picasso Library.
Picasso is a powerful image downloading and caching library for Android.
Picasso can be included in your project by adding the following line to your project's gradle file:
You can check the available versions at : https://github.com/square/picasso
A simple example to load an image into an imageview is :
It also allows image transformation to better fit into layouts and reduce memory size. An example to do this is :
It allows place holder images which is displayed before the image is downloaded and also allows error images to be displayed if there is problem loading the image. Here is an example for this :
Picasso is a powerful image downloading and caching library for Android.
Picasso can be included in your project by adding the following line to your project's gradle file:
compile 'com.squareup.picasso:picasso:(latest version)'
You can check the available versions at : https://github.com/square/picasso
A simple example to load an image into an imageview is :
Picasso.with(context) .load("http://i.imgur.com/DvpvklR.png") .into(imageView);
It also allows image transformation to better fit into layouts and reduce memory size. An example to do this is :
Picasso.with(context) .load("http://i.imgur.com/DvpvklR.png") .resize(50, 50) .centerCrop() .into(imageView);
It allows place holder images which is displayed before the image is downloaded and also allows error images to be displayed if there is problem loading the image. Here is an example for this :
Picasso also supports resource loading. Some examples of resource loading are:Picasso.with(context) .load("http://i.imgur.com/DvpvklR.png") .placeholder(R.drawable.user_placeholder) .error(R.drawable.user_placeholder_error) .into(imageView);
Picasso.with(context) .load(R.drawable.landing_screen) .into(imageView1); Picasso.with(context) .load("file:///android_asset/DvpvklR.png") .into(imageView2); Picasso.with(context) .load(new File(...)). into(imageView3);
Comments
Post a Comment