android Bitmap圍繞一個點進行旋轉

來源:互聯網
上載者:User

在項目中需要使用定位功能,也就是一個點圍繞一個圓心進行旋轉,查看了canvas的函數也就只有一個 canvas.drawBitmap(bitmap, matrix, paint)通過使用Matrix來實現旋轉,這裡實現一個demo,功能就是小原點順時針繪製一個原然後逆時針撤銷這個圓,通過Timer來實現。代碼如下: Surface.java這個是實現繪製圖形的類   

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.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);//設定背景透明           bmp = BitmapFactory.decodeResource(getResources(), R.drawable.camera_r_local);          bmp = Bitmap.createScaledBitmap(bmp, 15, 15, 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);//清除螢幕   //          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", degree+"  : "+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.graphics.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);//設定背景透明bmp = BitmapFactory.decodeResource(getResources(), R.drawable.camera_r_local);  bmp = Bitmap.createScaledBitmap(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);//清除螢幕//         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是主類 
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);}}

 

 如下: 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.