Scaling Bitmap bitmap of Android learning notes

Source: Internet
Author: User

Bitmap scaling can also be achieved through matrix or canvas.

Use the postscale (0.5f, 0.3f) method to set the rotation angle, use the createbitmap method to create a bitmap object after scaling, and use the drawbitmap method to draw the object to the screen, then the scaling operation is implemented.

In the following example, both the original bitmap and the scaled bitmap are drawn to the screen for comparison.

package xiaosi.bitmap;import android.app.Activity;import android.os.Bundle;public class mianActivity extends Activity{private BitmapView bitmapView = null;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);bitmapView = new BitmapView(this);setContentView(bitmapView);}}

 

Bitmapview. Java

Package xiaosi. bitmap; import android. content. context; import android. content. res. resources; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android. graphics. canvas; import android. graphics. color; import android. graphics. matrix; import android. view. view; public class bitmapview extends view {public bitmapview (context) {super (context);} // rewrite the ondraw method public void ondraw (canvas) {// obtain the resource file reference resresources res = getresources (); // obtain the image resource file bitmap BMP = bitmapfactory. decoderesource (Res, R. drawable. h); // set the canvas background to a white canvas. drawcolor (color. black); // draw a bitmap before scaling on the canvas for comparison. // The coordinates of the position on the screen are 0 and 0 canvas. drawbitmap (BMP, 0, 0, null); // defines the matrix object matrix = new matrix (); // scales the original matrix. postscale (0.5f, 0.3f); // BMP. getwidth (), BMP. getheight () indicates the scaled bitmap width and height. Bitmap dstbmp = bitmap. createbitmap (BMP, 0, 0, BMP. getwidth (), BMP. getheight (), matrix, true); // draw the rotated bitmap on the canvas // place it in the position where the coordinate is 60,460 canvas. drawbitmap (dstbmp, 60,460, null );}}

 

 

Source code download: Click the open link

 

 

 

 

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.