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