Android uses Picasso to load scale scales like network pictures

Source: Internet
Author: User

When doing the Android image loading, because the mobile phone screen is limited, many large images loaded over the time, we require such proportional scaling, such as in accordance with the fixed width, such as proportional scaling height, so that the size of the picture scale is scaled accordingly, but the picture is not deformed. Obviously according to Android:scaletype can not be implemented, because there will be a lot of restrictions, so you have to write the algorithm.

Zoom by Picasso
In fact, Picasso provides such a method. Specifically, the transform method that displays transformation.
(1) Get the width and height of the network or local image first
(2) Get the desired target width
(3) proportional to the height of the target
(4) Create a new diagram according to the target's width and height

Transformation transformation =Newtransformation () {@Override PublicBitmap Transform (Bitmap source) {intTargetwidth =mimg.getwidth (); LOGCAT.I ("source.getheight () ="+source.getheight ()); LOGCAT.I ("source.getwidth () ="+source.getwidth ()); LOGCAT.I ("targetwidth="+targetwidth); if(source.getwidth () = =0) {returnsource; }//If the picture is less than the width of the setting, return the original            if(Source.getwidth () <targetwidth) {returnsource; }Else{//If the picture size is greater than or equal to the width of the setting, the scale is scaled by the width of the setting                DoubleAspectratio = (Double) Source.getheight ()/(Double) source.getwidth (); intTargetheight = (int) (Targetwidth *aspectratio); if(Targetheight! =0&& Targetwidth! =0) {Bitmap result= Bitmap.createscaledbitmap (source, Targetwidth, Targetheight,false); if(Result! =source) {//same bitmap is returned if sizes is the samesource.recycle (); }returnresult; } Else{returnsource; } }} @Override PublicString Key () {return "Transformation"+"Desiredwidth"; }    };

After setting the transform in Picasso

Picasso.with (mcontext).       load (IMAGEURL).       placeholder (R.mipmap.zhanwei)       . Error (R.mipmap.zhanwei)       . Transform (transformation)       . into (Viewholder.mimageview);

Transformation This is a very powerful feature of Picasso, which allows you to make a series of transformations to the picture in the middle of the load picture, into ImageView. For example, you want to do the picture Gaussian blur, add rounded corners, do gray processing, round pictures, etc. can be done by transformation.
Reference article: Https://stackoverflow.com/questions/21889735/resize-image-to-full-width-and-variable-height-with-picasso

Android uses Picasso to load scale scales like network pictures

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.