Android Project Notepad (-----) zoom in and out of the picture and add a border to the picture

Source: Internet
Author: User

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, it must be scaled, But in the scaling, how to zoom, long and wide scaling 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, Equal scale 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:

[Java]View Plaincopy
  1. //equal scale picture
  2. Private Bitmap Resize (Bitmap Bitmap,int S) {
  3. int imgwidth = Bitmap.getwidth ();
  4. int imgheight = Bitmap.getheight ();
  5. double partion = imgwidth*1.0/imgheight;
  6. Double sqrtlength = math.sqrt (partion*partion + 1);
  7. //new thumbnail size
  8. Double NEWIMGW = s* (partion/sqrtlength);
  9. Double newimgh = s* (1/sqrtlength);
  10. float Scalew = (float) (newimgw/imgwidth);
  11. float Scaleh = (float) (newimgh/imgheight);
  12. Matrix MX = new Matrix ();
  13. //Zoom in on the original image
  14. Mx.postscale (Scalew, Scaleh);
  15. Bitmap = Bitmap.createbitmap (bitmap, 0, 0, ImgWidth, imgheight, MX, true);
  16. return bitmap;
  17. }

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:

[Java]View Plaincopy
    1. //Add a border to the picture and return the picture after the border
    2. Public Bitmap Getbitmaphuasebiankuang (Bitmap Bitmap) {
    3. float framesize = 0.2f;
    4. Matrix matrix = new Matrix ();
    5. //used to make Basemaps
    6. Bitmap BITMAPBG = Bitmap.createbitmap (Bitmap.getwidth (),
    7. Bitmap.getheight (), Bitmap.Config.ARGB_8888);
    8. //Set Basemap as Canvas
    9. Canvas canvas = new Canvas (BITMAPBG);
    10. Canvas.setdrawfilter (new Paintflagsdrawfilter (0, Paint.anti_alias_flag
    11. | Paint.filter_bitmap_flag));
    12. float scale_x = (bitmap.getwidth ()- 2 * framesize- 2) * 1f
    13. /(Bitmap.getwidth ());
    14. float scale_y = (bitmap.getheight ()- 2 * framesize- 2) * 1f
    15. /(Bitmap.getheight ());
    16. Matrix.reset ();
    17. Matrix.postscale (scale_x, scale_y);
    18. //Photo size processing (minus the size of the border)
    19. Bitmap = Bitmap.createbitmap (bitmap, 0, 0, Bitmap.getwidth (),
    20. Bitmap.getheight (), Matrix, true);
    21. Paint paint = new paint ();
    22. Paint.setcolor (Color.White);
    23. Paint.setstrokewidth (1);
    24. Paint.setstyle (Style.fill);
    25. //Draw a basemap border
    26. Canvas.drawrect (
    27. New Rect (0, 0, Bitmapbg.getwidth (), Bitmapbg.getheight ()),
    28. Paint);
    29. //Draw a gray border
    30. Paint.setcolor (Color.Blue);
    31. Canvas.drawrect (
    32. New Rect ((int) (framesize), (int) (framesize), BITMAPBG
    33. . GetWidth ()-(int) (framesize), Bitmapbg.getheight ()
    34. -(int) (framesize)), paint);
    35. Canvas.drawbitmap (bitmap, framesize + 1, framesize + 1, paint);
    36. return BITMAPBG;

Android Project Notepad (-----) zoom in and out of the picture and add a border to the picture

Related Article

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.