Picasso Loading Pictures _ Reprint

Source: Internet
Author: User
Tags int size

Reprint please indicate the source: http://blog.csdn.net/xiejinquan_/article/details/50565152 Current mainstream picture loading library has universal image loader,picasso,glide , Fresro
Picasso is an open-source Android graphics cache for square, which enables image downloading and caching. Official address: Http://square.github.io/picasso/github Address: Https://github.com/square/picasso
Run effect

First, introduce Picasso new Android Studio project, add in Build.grale of module

dependencies {
Compile Filetree (include: [' *.jar '], dir: ' Libs ')
Testcompile ' junit:junit:4.12 '
Compile ' com.android.support:appcompat-v7:23.1.1 '
Compile ' com.squareup.picasso:picasso:2.5.2 '//introduction of Picasso
}
Second, Picasso not only realize the function of picture asynchronous loading, but also solve some common problems that need to be solved when loading pictures in Android:

1. In the adapter need to cancel the field of vision has not been the scope of the ImageView picture resource loading, otherwise it will result in image dislocation, Picasso has solved the problem.

2. Use of complex image compression conversion to minimize memory consumption

3. Self-with memory and hard disk level two cache function three, one line of code can load pictures asynchronously

    private void Initpicasso () {
Load picture
Picasso.with (this). Load (URL). into (MIV);
The MIv2 control cannot be set to Wrap_content, that is, it must have a size, fit () to make the picture's height equal to the width of the mIv2, and to set it () and no longer call resize ()
Picasso.with (this). Load (URL). Fit (). into (MIV2);
Assign the loaded picture to the width display
Picasso.with (this). Load (URL). Resize. Centercrop () (MIV3);
Intercepts the loaded picture for display
Picasso.with (this). Load (URL). Transform (New Cropsquaretransformation ()). into (MIV3);
Default display placeholder, load error display Ic_launcher
Picasso.with (this). Load (URL). Placeholder (R.mipmap.placeholder)-Error (R.mipmap.ic_launcher). into (MIV4);
Default display placeholder, load error display Ic_launcher, specify placeholder size display
Picasso.with (this). Load (URL). Placeholder (R.mipmap.placeholder)-Fit (). Error (R.mipmap.ic_launcher). into (MIV4);
}
Custom cropping class
public class Cropsquaretransformation implements transformation {

Intercept the width and height of the bitmap from the smallest width and height
@Override
Public Bitmap transform (Bitmap source) {
int Size=math.min (Source.getwidth (), Source.getheight ());
int x= (Source.getwidth ()-size)/2;
int y= (Source.getheight ()-size)/2;
Bitmap Result=bitmap.createbitmap (source,x,y,size,size);
if (Result!=source) {
Source.recycle ()/release bitmap
}
return result;
}

@Override
Public String key () {
Return "square ()";
}
}
Note: In addition to downloading pictures over the network, Picasso can also load local image resources
Picasso.with. Load (R.mipmap.ic_launcher). into (ImageView);
Picasso.with. Load (URL). into (ImageView);
Picasso.wiht (context). Load (new File (...)). Into (ImageView);
Four, combined Recyclerview use Picasso, will not Recyclerview Click here: Recyclerview Adapter Universal Adapter Package
Mrecyclerview.setadapter (madapter = new Jesseadapter<pic> (this, r.layout.item_data, Mdatas) {
@Override
public void Onbindview (jesseholder holder, int position) {
ImageView Iv_data = Holder.getview (R.id.iv_data);
Get the "image URL for the" current position.
String url = mdatas.get (position). Getimg ();
Trigger the download of the URL asynchronously into the image view.
Picasso.with (Mcontext)
. Load (URL)
. Placeholder (R.mipmap.placeholder)//default display
. Error (R.mipmap.ic_launcher)//Load Errors display
. Fit ()//placeholder width as the height of the display
. Tag (Mtag)//Set tag to solve the dislocation
. into (Iv_data);

TextView tv_name = Holder.getview (r.id.tv_name);
Tv_name.settext (Mdatas.get (position). GetName ());
}
});
Five, optimized 1,recyclerview sliding does not load, sliding stop loading
Mrecyclerview.addonscrolllistener (New Recyclerview.onscrolllistener () {
@Override
public void onscrollstatechanged (Recyclerview recyclerview, int newstate) {
if (newstate = = Recyclerview.scroll_state_idle) {//sliding stop loading picture
Picasso.with (Mcontext). Resumetag (Mtag);
else {//when sliding does not load pictures
Picasso.with (Mcontext). Pausetag (Mtag);
}
}
});
2, set the picture for opaque pictures can use rgb_565 to optimize memory.
Picasso.with. Load (URL). config (Bitmap.Config.RGB_565). into (ImageView);
By default, Android uses argb_8888
Alpha_8 represents a 8-bit alpha bitmap
argb_4444 represents a 16-bit ARGB bitmap
argb_8888 represents a 32-bit ARGB bitmap
RGB_565 represents 8-bit RGB bitmap source download

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.