Android handles bitmap so that it does not distort and is displayed on ImageView after scaling cropping

Source: Internet
Author: User

In the process of Android development, we sometimes need to display some pictures dynamically, and the size gap of these pictures is very large, if the need is not required to complete the picture display, but need not distortion, and to the picture in the middle part of the case, we need to do a series of processing, Because at this time the various scale types of imageview are not applicable. Detailed steps are described in the code below, you can also directly copy the past as a tool class use

1 /**2 * Get the correct scaling of crop fit Iamgeview bitmap3      * @paramImageView4      * @paramBitmap5      * @return6      */7      Public StaticBitmap Createfitbitmap (ImageView ImageView, Bitmap Bitmap) {8LOG.I (TAG, "createfitbitmap<---------------------");9         intWidthtarget =imageview.getwidth ();Ten         intHeighttarget =imageview.getheight (); One         intWidthbitmap =bitmap.getwidth (); A         intHeightbitmap =bitmap.getheight (); -LOG.I (TAG, "widthtarget =" +widthtarget); -LOG.I (TAG, "heighttarget =" +heighttarget); theLOG.I (TAG, "widthbitmap =" +widthbitmap); -LOG.I (TAG, "heightbitmap =" +heightbitmap); -Bitmap result =NULL; -         if(Widthbitmap >= widthtarget && heightbitmap >=heighttarget) { +result =Createlargetosmallbitmap (Widthbitmap, Heightbitmap, Widthtarget, Heighttarget, bitmap); -         } +         Else if(Widthbitmap >= widthtarget && Heightbitmap <heighttarget) { ABitmap temp =Createlargewidthtoequalheightbitmap (Widthbitmap, Heightbitmap, Widthtarget, Heighttarget, bitmap); atresult =Createlargetosmallbitmap (Temp.getwidth (), Temp.getheight (), Widthtarget, Heighttarget, temp); -         } -         Else if(Widthbitmap < widthtarget && Heightbitmap >=heighttarget) { -Bitmap temp =Createlargeheighttoequalwidthbitmap (Widthbitmap, Heightbitmap, Widthtarget, Heighttarget, bitmap); -result =Createlargetosmallbitmap (Temp.getwidth (), Temp.getheight (), Widthtarget, Heighttarget, temp); -         } in         Else{ -Bitmap temp =Createsmalltoequalbitmap (Widthbitmap, Heightbitmap, Widthtarget, Heighttarget, bitmap); toresult =Createfitbitmap (ImageView, temp); +         } -LOG.I (TAG, "Createfitbitmap--------------------->"); the         returnresult; *     } $     Panax Notoginseng      -      the     Private StaticBitmap Createlargetosmallbitmap (intWidthbitmap,intHeightbitmap,intWidthtarget,intHeighttarget, Bitmap Bitmap) { +LOG.I (TAG, "createlargetosmallbitmap<---------------------"); ALOG.I (TAG, "widthtarget =" +widthtarget); theLOG.I (TAG, "heighttarget =" +heighttarget); +LOG.I (TAG, "widthbitmap =" +widthbitmap); -LOG.I (TAG, "heightbitmap =" +heightbitmap); $         intx = (widthbitmap-widthtarget)/2; $         inty = (heightbitmap-heighttarget)/2; -LOG.I (TAG, "Createlargetosmallbitmap--------------------->"); -         returnBitmap.createbitmap (Bitmap, x, Y, Widthtarget, heighttarget); the     } -         Wuyi     Private StaticBitmap Createlargewidthtoequalheightbitmap (intWidthbitmap,intHeightbitmap,intWidthtarget,intHeighttarget, Bitmap Bitmap) { the          -LOG.I (TAG, "createlargewidthtoequalheightbitmap<---------------------"); WuLOG.I (TAG, "widthtarget =" +widthtarget); -LOG.I (TAG, "heighttarget =" +heighttarget); AboutLOG.I (TAG, "widthbitmap =" +widthbitmap); $LOG.I (TAG, "heightbitmap =" +heightbitmap); -         DoubleScale = (Heighttarget * 1.0)/Heightbitmap; -LOG.I (TAG, "Createlargewidthtoequalheightbitmap--------------------->"); -         returnBitmap.createscaledbitmap (Bitmap, (int) (Widthbitmap * scale), Heighttarget,false); A     } +      the     Private StaticBitmap Createlargeheighttoequalwidthbitmap (intWidthbitmap,intHeightbitmap,intWidthtarget,intHeighttarget, Bitmap Bitmap) { -          $LOG.I (TAG, "createlargeheighttoequalwidthbitmap<---------------------"); theLOG.I (TAG, "widthtarget =" +widthtarget); theLOG.I (TAG, "heighttarget =" +heighttarget); theLOG.I (TAG, "widthbitmap =" +widthbitmap); theLOG.I (TAG, "heightbitmap =" +heightbitmap); -         DoubleScale = (Widthtarget * 1.0)/Widthbitmap; inLOG.I (TAG, "Createlargeheighttoequalwidthbitmap--------------------->"); the         returnBitmap.createscaledbitmap (Bitmap, Widthtarget, (int) (Heighttarget * scale),false); the     } About      the     Private StaticBitmap Createsmalltoequalbitmap (intWidthbitmap,intHeightbitmap,intWidthtarget,intHeighttarget, Bitmap Bitmap) { the          theLOG.I (TAG, "createsmalltoequalbitmap<---------------------"); +LOG.I (TAG, "widthtarget =" +widthtarget); -LOG.I (TAG, "heighttarget =" +heighttarget); theLOG.I (TAG, "widthbitmap =" +widthbitmap);BayiLOG.I (TAG, "heightbitmap =" +heightbitmap); the         DoubleScaleWidth = (Widthtarget * 1.0)/Widthbitmap; the         DoubleScaleHeight = (Heighttarget * 1.0)/Heightbitmap; -         DoubleScale =math.min (ScaleWidth, scaleheight); -LOG.I (TAG, "Createsmalltoequalbitmap--------------------->"); the         returnBitmap.createscaledbitmap (Bitmap, (int) (Widthbitmap * scale), (int) (Heightbitmap * scale),false); the}

Android handles bitmap so that it does not distort and is displayed on ImageView after scaling cropping

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.