android Bitmap围绕一个点进行旋转
在项目中需要使用定位功能,也就是一个点围绕一个圆心进行旋转,查看了canvas的函数也就只有一个 canvas.drawBitmap(bitmap, matrix, paint)通过使用Matrix来实现旋转,这里实现一个demo,功能就是小原点顺时针绘制一个原然后逆时针撤销这个圆,通过Timer来实现。代码如下:Su易做图ce.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.Su易做图ceHolder; import android.view.Su易做图ceView; import android.view.Su易做图ceHolder.Callback; @SuppressLint("HandlerLeak") class Su易做图ce extends Su易做图ceView implements Callback{ Bitmap bmp; private Paint mPaint; private Su易做图ceHolder mHolder; public Su易做图ce(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 su易做图ceCreated(Su易做图ceHolder 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 su易做图ceChanged(Su易做图ceHolder holder, int format, int width, int height) { } @Override public void su易做图ceDestroyed(Su易做图ceHolder 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.Su易做图ceHolder; import android.view.Su易做图ceView; import android.view.Su易做图ceHolder.Callback; @SuppressLint("HandlerLeak") class Su易做图ce extends Su易做图ceView implements Callback{ Bitmap bmp; private Paint mPaint; private Su易做图ceHolder mHolder; public Su易做图ce(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 su易做图ceCreated(Su易做图ceHolder 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 su易做图ceChanged(Su易做图ceHolder holder, int format, int width, int height) { } @Override public void su易做图ceDestroyed(Su易做图ceHolder 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{ Su易做图ce su易做图ce; RelativeLayout.LayoutParams laParams; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); su易做图ce = new Su易做图ce(this); laParams = new RelativeLayout.LayoutParams(260, 260); setContentView(su易做图ce, laParams); } } package com.example.test; import android.app.Activity; import android.os.Bundle; import android.widget.RelativeLayout; public class Main7 extends Activity{ Su易做图ce su易做图ce; RelativeLayout.LayoutParams laParams; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); su易做图ce = new Su易做图ce(this); laParams = new RelativeLayout.LayoutParams(260, 260); setContentView(su易做图ce, laParams); } }
效果图如下: