Android project notepad (12) ----- proportional scaling of images and adding borders to images

Source: Internet
Author: User

Android project notepad (12) ----- proportional scaling of images and adding borders to images
This article is my learning notes, welcome to reprint, but please note the Source: http://blog.csdn.net/jesson20121020

In Android UI development, image scaling is often encountered. For example, in notepad, the current images are relatively large. If the source image is directly placed on the screen without scaling, the entire screen will be fully occupied, and sometimes the image is bigger than the screen, and the whole image cannot be displayed completely. Therefore, you must scale the image. But how can you scale the image, to maintain the aspect ratio of the source image, the best way is to restrict the proportional scaling, that is, proportional scaling, I believe everyone has used the zoom function in PS. One option is to constrain the zoom ratio, that is, to maintain the aspect ratio, that is, proportional scaling.

Zoom before scaling
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + forward/ruty88rWlo6y + zcrH1NrL9bfFtcS5/forward + forward = "brush: java;"> // proportional scaling image 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 * (partion/sqrtLength); double 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, imgHeight, mx, true); return bitmap ;}S is the maximum pixel to which the length and width are scaled.

Today I will try to add a border effect to the image. Of course, here I simply add a line border, or I can add other nice borders to the image. first look at it:

In fact, the idea of adding a border to an image is to draw a rectangle around the image. The Code is as follows:

// Add a border to the image and return the public Bitmap getBitmapHuaSeBianKuang (Bitmap bitmap) {float frameSize = 0.2f; Matrix matrix = new Matrix (); // used for base map Bitmap bitmapbg = Bitmap. createBitmap (bitmap. getWidth (), bitmap. getHeight (), Bitmap. config. ARGB_8888); // sets the basemap to Canvas canvas 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); // process the 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); // draw the 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 ;}


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.