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