Matrix 旋转图片,该怎么解决
Matrix 旋转图片
------解决方案--------------------
图片旋转
- Java code
Matrix xMatrix = new Matrix(); xMatrix.set(mBaseMatrix); xMatrix.setRotate(-90); setImageMatrix(xMatrix); float v[] = new float[9]; xMatrix.getValues(v); mBaseMatrix.setValues(v);
------解决方案--------------------
图片旋转
- Java code
public class BeadplateMiddle extends View { private Bitmap mBitmap = null;//声明Bitmap对象 private int x = 300; private int y = 100; //private float angleA = 0.0f;//声明轴心A转动 private float angleB = 0.0f;//声明轴心B转动角度 private Matrix mMatrix = new Matrix();//构建矩阵Matrix对象 public BeadplateMiddle(Context context) { super(context); //装载资源 mBitmap =((BitmapDrawable) getResources().getDrawable(R.drawable.bedplate)).getBitmap(); //开启线程 new Thread(new DrawThread()).start(); } //系统IOC开始绘制 @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Paint mPaint = new Paint(); mPaint.setColor(Color.RED); mPaint.setAntiAlias(true); canvas.drawCircle(x, y, 12, mPaint); mMatrix = getMyMatrix(mMatrix,angleB, x, y); canvas.drawBitmap(mBitmap, mMatrix, null); } /** * 动态构建旋转矩阵Matrix对象 * @param matrix 需要计算的矩阵 * @param canvas 画布 * @param degrees 图片旋转的角度,正值为顺时针,负值为逆时针 * @param pivotX 轴心的X坐标 * @param pivotY 轴心的Y坐标 */ private Matrix getMyMatrix(Matrix matrix ,float degrees,int pivotX , int pivotY ){ //重置Matrix matrix.reset(); float cosValue = (float) Math.cos(Math.PI/(180/degrees)); float sinValue = (float) Math.sin(Math.PI/(180/degrees)); //设置旋转矩阵值 matrix.setValues( new float[]{ cosValue, -sinValue, pivotX, sinValue, cosValue, pivotY, 0, 0, 1}); return matrix; } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_DPAD_LEFT){ x --; }else if(keyCode == KeyEvent.KEYCODE_DPAD_RIGHT){ x ++; }else if(keyCode == KeyEvent.KEYCODE_DPAD_UP){ y --; }else if(keyCode == KeyEvent.KEYCODE_DPAD_DOWN){ y ++; }else if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER){ angleB ++; } return true; } private class DrawThread implements Runnable{ @Override public void run() { while(!Thread.currentThread().isInterrupted()){ try { Thread.sleep(500); } catch (Exception e) { Thread.currentThread().interrupt(); } //使用PostInvalidate可以直接在线程中更新视图 postInvalidate(); } } } }
------解决方案--------------------
#4楼的意思是是像这样
- Java code
Matrix xMatrix = new Matrix(); while(true) { xMatrix.set(mBaseMatrix); xMatrix.setRotate(-90); setImageMatrix(xMatrix); float v[] = new float[9]; xMatrix.getValues(v); mBaseMatrix.setValues(v); }