This article is your own study notes, welcome reprint, but please indicate the source:http://blog.csdn.net/jesson20121020
in the Android UI development often encounter the image of the zoom, such as Notepad, now the picture is relatively large, if the original image is placed directly on the screen without zooming, it will occupy the entire screen, and sometimes the picture will be larger than the screen, then can not fully display the entire picture, so, you have to zoom , but in the scaling, how to zoom, long and wide scale is set to how much appropriate, in order to maintain the image of the aspect ratio, so the best way is to constrain the scale, that is, proportional scaling, I believe we have used PS in the zoom image function, there is an option is to constrain the scale, that is, to maintain the aspect ratio, That is, proportional scaling.
After zooming before zooming
Scaling the idea is simple, is to maintain the aspect ratio in the process of scaling, the code is as follows:
Equal scale picture Private Bitmap resize (Bitmap bitmap,int S) {int imgwidth = bitmap.getwidth (); int imgheight = Bitmap.getheight (); Double partion = imgwidth*1.0/imgheight;double sqrtlength = math.sqrt (partion*partion + 1);//new thumbnail size double NEWIMGW = S* (P artion/sqrtlength);d ouble newimgh = s* (1/sqrtlength); float Scalew = (float) (newimgw/imgwidth); float Scaleh = (float) (Newimgh/imgheight); Matrix MX = new Matrix ();//Scale the original image Mx.postscale (Scalew, Scaleh); bitmap = Bitmap.createbitmap (bitmap, 0, 0, ImgWidth, IMGH Eight, MX, true); return bitmap;}
where S is the maximum pixel length and width to zoom to.
Again today to achieve a picture to add a border effect, of course, here I simply added a line border, you can also use other good-looking border to add to the picture, first look:
In fact, the idea of adding a border to a picture is to draw a rectangle in the four-week picture, with the following code:
//Add border to picture and return the picture after frame public Bitmap Getbitmaphuasebiankuang (Bitmap Bitmap) {float Framesiz e = 0.2f; Matrix matrix = new Matrix (); Used to make basemaps Bitmap BITMAPBG = Bitmap.createbitmap (Bitmap.getwidth (), Bitmap.getheight (), Bitmap.config. argb_8888); Set the basemap for canvas canvases canvas = new canvas (BITMAPBG); Canvas.setdrawfilter (New Paintflagsdrawfilter (0, Paint.anti_alias_flag | Paint.filter_bitmap_flag)); float scale_x = (bitmap.getwidth ()-2 * frameSize-2) * 1f/(Bitmap.getwidth ()); float scale_y = (bitmap.getheight ()-2 * frameSize-2) * 1f/(Bitmap.getheight ()); Matrix.reset (); Matrix.postscale (scale_x, scale_y); Processing of photo size (minus the size of the border) bitmap = Bitmap.createbitmap (bitmap, 0, 0, bitmap.getwidth (), Bitmap.getheight () , Matrix, True); Paint paint = new paint (); Paint.setcolor (Color.White); Paint.setstrokewidth (1); Paint.setstyle (Style.fill); Draws a basemap border Canvas.drawrect (new Rect (0, 0, bitmapbg.getwidth (), Bitmapbg.getheight ()), Paint); Draw a gray border Paint.setcolor (Color.Blue); Canvas.drawrect (new Rect (int) (framesize), (int) (framesize), BITMAPBG. GetWidth () -(int) (framesize), Bitmapbg.getheight ()-(int) (framesize)), paint); Canvas.drawbitmap (Bitmap, framesize + 1, framesize + 1, paint); return BITMAPBG;}
Android Project Notepad (-----) zoom in and out of the picture and add a border to the picture