Android Bitmap rotates around a point

Source: Internet
Author: User

You need to use the positioning function in the project, that is, to rotate a point around the center of a circle. The function that views the canvas has only one canvas. drawBitmap (bitmap, matrix, paint) is rotated by using Matrix. Here, a demo is implemented. The function is to draw an original source clockwise and undo this circle counterclockwise, which is implemented by Timer. The Code is as follows: Surface. java is a class for drawing graphics.

Package com. example. test; import java. util. timer; import java. util. timerTask; import android. annotation. suppressLint; import android. content. context; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. graphics. canvas; import android. graphics. color; import android. graphics. matrix; import android. graphics. paint; import android. graphics. pixelFormat; import android. gr Aphics. porterDuff. mode; import android. OS. handler; import android. OS. message; import android. util. log; import android. view. surfaceHolder; import android. view. surfaceView; import android. view. surfaceHolder. callback; @ SuppressLint ("HandlerLeak") class Surface extends SurfaceView implements Callback {Bitmap bmp; private Paint mPaint; private SurfaceHolder mHolder; public Surface (Context context) {su Per (context); mHolder = getHolder (); mHolder. addCallback (this); mPaint = new Paint (); mPaint. setColor (Color. GREEN); mPaint. setAntiAlias (true); // setFocusable (true); setFocusableInTouchMode (true); // setZOrderOnTop (true); // mHolder. setFormat (PixelFormat. TRANSPARENT); // set the background to TRANSPARENT bmp = BitmapFactory. decodeResource (getResources (), R. drawable. camera_r_local); bmp = Bitmap. createScaledBitmap (bmp, 15, 1 5, false);} public Handler handler = new Handler () {@ Override public void handleMessage (Message msg) {switch (msg. what) {case 0: canvas_ OK (); break;} super. handleMessage (msg) ;}}; public void canvas_ OK () {Canvas canvas = null; try {canvas = mHolder. lockCanvas (); canvas. drawColor (Color. TRANSPARENT, Mode. CLEAR); // CLEAR the screen // canvas. save (); Matrix matrix = new Matrix (); matrix. postTranslate (130, 0); Matrix. postRotate (degree, 130,130); canvas. drawBitmap (bmp, matrix, mPaint); // canvas. restore ();} catch (Exception e) {} finally {if (canvas! = Null) {mHolder. unlockCanvasAndPost (canvas) ;}} int degree = 0; Timer timer = new Timer (); boolean flag = true; @ Override public void surfaceCreated (SurfaceHolder holder) {canvas_ OK (); timer. schedule (new TimerTask () {@ Override public void run () {if (flag) {degree ++; if (degree = 360) {flag = false ;}} else {degree --; if (degree =-360 | degree = 0) {flag = true ;}} Log. e ("flag", degr Ee + ":" + flag); handler. sendEmptyMessage (0) ;}}, 50, 50) ;}@ Override public void surfaceChanged (SurfaceHolder holder, int format, int width, int height) {}@ Override public void surfaceDestroyed (SurfaceHolder holder) {if (timer! = Null) {timer. cancel (); timer = null ;}} package com. example. test; import java. util. timer; import java. util. timerTask; import android. annotation. suppressLint; import android. content. context; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. graphics. canvas; import android. graphics. color; import android. graphics. matrix; import android. graphics. paint; import android. grap Hics. pixelFormat; import android. graphics. porterDuff. mode; import android. OS. handler; import android. OS. message; import android. util. log; import android. view. surfaceHolder; import android. view. surfaceView; import android. view. surfaceHolder. callback; @ SuppressLint ("HandlerLeak") class Surface extends SurfaceView implements Callback {Bitmap bmp; private Paint mPaint; private SurfaceHolder mHolder; public Surface (Context context) {super (context); mHolder = getHolder (); mHolder. addCallback (this); mPaint = new Paint (); mPaint. setColor (Color. GREEN); mPaint. setAntiAlias (true); // setFocusable (true); setFocusableInTouchMode (true); // setZOrderOnTop (true); // mHolder. setFormat (PixelFormat. TRANSPARENT); // set the background to TRANSPARENT bmp = BitmapFactory. decodeResource (getResources (), R. drawable. camera_r_local); bmp = Bitmap. createScaledB Itmap (bmp, 15, 15, false);} public Handler handler = new Handler () {@ Overridepublic void handleMessage (Message msg) {switch (msg. what) {case 0: canvas_ OK (); break;} super. handleMessage (msg) ;}}; public void canvas_ OK () {Canvas canvas = null; try {canvas = mHolder. lockCanvas (); canvas. drawColor (Color. TRANSPARENT, Mode. CLEAR); // CLEAR the screen // canvas. save (); Matrix matrix = new Matrix (); matrix. postTranslate (130, 0 ); Matrix. postRotate (degree, 130,130); canvas. drawBitmap (bmp, matrix, mPaint); // canvas. restore ();} catch (Exception e) {} finally {if (canvas! = Null) {mHolder. unlockCanvasAndPost (canvas) ;}} int degree = 0; Timer timer = new Timer (); boolean flag = true; @ Overridepublic void surfaceCreated (SurfaceHolder holder) {canvas_ OK (); timer. schedule (new TimerTask () {@ Overridepublic void run () {if (flag) {degree ++; if (degree = 360) {flag = false ;}} else {degree --; if (degree =-360 | degree = 0) {flag = true ;}} Log. e ("flag", degree + ":" + flag); handler. SendEmptyMessage (0) ;}}, 50, 50) ;}@ Overridepublic void surfaceChanged (SurfaceHolder holder, int format, int width, int height) {}@ Overridepublic void surfaceDestroyed (SurfaceHolder holder) {if (timer! = Null) {timer. cancel (); timer = null ;}}}

 

Main7.java is the main class
package com.example.test;    import android.app.Activity;  import android.os.Bundle;  import android.widget.RelativeLayout;    public class Main7 extends Activity{      Surface surface;      RelativeLayout.LayoutParams laParams;      @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          surface = new Surface(this);          laParams = new RelativeLayout.LayoutParams(260,  260);          setContentView(surface, laParams);      }    }  package com.example.test;import android.app.Activity;import android.os.Bundle;import android.widget.RelativeLayout;public class Main7 extends Activity{Surface surface;RelativeLayout.LayoutParams laParams;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);surface = new Surface(this);laParams = new RelativeLayout.LayoutParams(260,  260);setContentView(surface, laParams);}}

 

As follows:

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.