Android study note 11: Image translation, rotation, and scaling

Source: Internet
Author: User

In Android, res \ drawable in the project directory is used to place image resources of the project.

Android provides the Bitmap class to obtain image file information, perform image translation, rotation, scaling, and other operations, and save image files in a specified format.

1. Image Rendering

Before creating an image, you must obtain the required image resources from res \ drawable in the project directory. We can use the resource index to obtain the Bitmap of the image object. The specific method is as follows (put an image named fuwa.png in res \ drawableunder the project directory ):

MBitmap = (BitmapDrawable) getResources (). getDrawable (R. drawable. fuwa). getBitmap ():

The getResources () method is used to obtain the resource object. The getDrawable () method is used to obtain the Drawable object in the resource. The parameter is the resource index id. getBitmap () the method is used to obtain the Bitmap object.

After obtaining image resources, you can use the drawBitmap () method to display the image to the (x, y) Coordinate Position on the screen. The specific method is as follows:

Canvas. drawBitmap (mBitmap, x, y, null );

In addition, to obtain the image information, you can use the mBitmap. getHight () method to obtain the image height, and use the mBitmap. getWidth () f method to obtain the image width.

2. Image Translation

By drawing the image, we know that the Canvas. drawBitmap (mBitmap, x, y, null) method can be used to draw the image to the coordinates of the screen (x, y.

Therefore, to achieve image translation, you only need to change the coordinates of the image drawn to the screen (x, y.

3. Image Rotation

In Android, Matrix can be used for image rotation. Matrix is a 3*3 Matrix used for image transformation and matching. Matrix has no struct and must be initialized. It can be implemented through the reset () or set () method, as shown below:

MMatrix. reset ();

After initialization, you can use the setRotate () method to set the desired rotation angle, as shown below:

MMatrix. setRotate ();

After setting the rotation angle, you can use creatBitmap () to create a Bitmap object after rotation. The method is as follows:

MBitmapRotate = Bitmap. creatBitmap (mBitmap, 0, 0, mBitmapWidth, mBitmapHight, mMatrix, true );

Finally, the Bitmap object is drawn to the screen to rotate the image.

4. Image Scaling

In Android, you can also use Matrix to scale images. Use the postScale () method of Matrix to set the image zoom factor, as shown below:

MMatrix. postScale ();

After you set the scaling factor, you also need to use the creatBitmap () method to create a Bitmap object that has been scaled. Finally, the Bitmap object is drawn to the screen to achieve image scaling.

5. Use the thread update Interface

To achieve real-time image rotation, scaling, and other effects on the interface, you can use a thread. Add the postInvalidate () method to the thread processing. As follows:

Thread processing
1 public void run (){
2 while (! Thread. currentThread (). isInterrupted ()){
3 try {
4 Thread. sleep (100 );
5}
6 catch (InterruptedException e ){
7 Thread. currentThread (). interrupt ();
8}
9 postInvalidate (); // you can use postInvalidate to directly update the interface in the thread.
10}
11}
6. Instance

In this example, four variables are defined: mBitmapToLeft, mBitmapToTop, mAngle, and mScale. The variable mBitmapToLeft indicates the distance from the image to the left boundary of the screen, mBitmapToTop indicates the distance from the image to the top of the screen, mAngle indicates the image rotation angle, and mScale indicates the image scaling factor.

You can move up, down, left, right, left, right, and right of the image in parallel by pressing the menu and back buttons to control the Rotation Angle of the image. You can also press the volume plus or minus button to control the magnification of the image.

These operations are processed in the thread and the postInvalidate () method has been called to refresh the interface in real time.

The instance source code is as follows:

MyView. java source code
1 package com. example. android_imagedraw;
2
3 import android. content. Context;
4 import android. graphics. Bitmap;
5 import android. graphics. Canvas;
6 import android. graphics. Color;
7 import android. graphics. Matrix;
8 import android. graphics. drawable. BitmapDrawable;
9 import android. view. KeyEvent;
10 import android. view. View;
11
12 public class MyView extends View implements Runnable {
13
14 Bitmap mBitmap = null; // image object
15 Matrix mMatrix = new Matrix (); // Matrix object
16
17 int mBitmapToLeft = 0; // The distance from the image to the left boundary of the screen
18 int mBitmapToTop = 0; // The distance from the image to the top of the screen
19
20 float mAngle = 0.0f; // Rotation Angle
21 float mScale = 1.0f; // Scaling Factor
22
23 public MyView (Context context ){
24 super (context );
25
26 mBitmap = (BitmapDrawable) getResources (). getDrawable
27 (R. drawable. fuwa). getBitmap (); // load the image from the resource file
28
29 new Thread (this). start (); // enable the Thread
30}
31
32 public void onDraw (Canvas canvas ){
33 super. onDraw (canvas );
34
35 canvas. drawColor (Color. GRAY); // set the canvas background Color to GRAY.
36
37 mMatrix. reset (); // resets the Matrix
38 mMatrix. setRotate (mAngle); // you can specify the rotation angle.
39 mMatrix. postScale (mScale, mScale); // you can specify a scaling factor.
40
41 // construct a new processed Bitmap
42 Bitmap mBitmapRotate = Bitmap. createBitmap (mBitmap, 0, 0,
43 mBitmap. getWidth (), mBitmap. getHeight (), mMatrix, true );
44
45 MyView. drawImage (canvas, mBitmapRotate, mBitmapToLeft, mBitmapToTop );
46}
47
48 // press the button
49 public boolean onKeyDown (int keyCode, KeyEvent event ){
50 switch (keyCode) {// process Translation
51 case KeyEvent. KEYCODE_DPAD_UP: // move up
52 if (mBitmapToTop> 0 ){
53 mBitmapToTop --;
54}
55 break;
56 case KeyEvent. KEYCODE_DPAD_DOWN: // downward arrow key: Move down
57 if (mBitmapToTop + mBitmap. getHeight () <800 ){
58 mBitmapToTop ++;
59}
60 break;
61 case KeyEvent. KEYCODE_DPAD_LEFT: // left direction key: Left Shift
62 if (mBitmapToLeft> 0 ){
63 mBitmapToLeft --;
64}
65 break;
66 case KeyEvent. KEYCODE_DPAD_RIGHT: // right direction key: Right Shift
67 if (mBitmapToLeft + mBitmap. getWidth () <480 ){
68 mBitmapToLeft ++;
69}
70 break;
71}
72
73 switch (keyCode) {// handle rotation events
74 case KeyEvent. KEYCODE_MENU: // MENU key: clockwise rotation
75 mAngle --;
76 break;
77 case KeyEvent. KEYCODE_BACK: // BACK key: clockwise rotation
78 mAngle ++;
79 break;
80}
81
82 switch (keyCode) {// process scaling events
83 case KeyEvent. KEYCODE_VOLUME_DOWN: // volume reduction key: Zoom out
84 if (mscaling> 0.3 ){
85 mScale-= 0.1;
86}
87 break;
88 case KeyEvent. KEYCODE_VOLUME_UP: // volume plus key: Zoom in
89 if (mScale <1.5 ){
90 mScale + = 0.1;
91}
92 break;
93}
94 return true;
95}
96
97 // press the button to bring up the event
98 public boolean onKeyUp (int keyCode, KeyEvent event ){
99 return false;
100}
101
102 // thread processing
103 public void run (){
104 while (! Thread. currentThread (). isInterrupted ()){
105 try {
106 Thread. sleep (100 );
107}
108 catch (InterruptedException e ){
109 Thread. currentThread (). interrupt ();
110}
111 postInvalidate (); // use postInvalidate to directly update the interface in the thread
112}
113}
114
115 // draw a Bitmap
116 public static void drawImage (Canvas canvas, Bitmap bitmap, int x, int y ){
117 canvas. drawBitmap (bitmap, x, y, null );
118}
119}
The original image effect of this instance is as follows:

 

Figure 1 Original Image Effect

The following figure shows the effect of 2 after translation rotation and Scaling:

 

Figure 2 effect after translation rotation and Scaling

 

 

 

 

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.