图片预览旋转

package com.dunianhao.matrixtext;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.View;

@SuppressLint("NewApi")
public class MatrixView extends View {
 private int viewHeight=10,viewWight=10;//控件宽高
 private float x,y;//控件所在位置
 private Matrix mMatrix = new Matrix();//定义矩阵
 private Bitmap cacheBitmap;//缓冲bitmap
 private float rotate = 0f;//  旋转角度
 private Context mContex;
 public MatrixView(Context context, AttributeSet attrs) {
  super(context, attrs);
  this.mContex = context;
  cacheBitmap = Bitmap.createBitmap(viewWight, viewHeight, Config.ARGB_8888);
  viewHeight = cacheBitmap.getHeight();
  viewWight = cacheBitmap.getWidth();
  x = getX();
  y = getY();
  this.setFocusable(true);
 }
 
 public void setBitmapDrawable(Bitmap mBitmapDrawable){
  cacheBitmap = mBitmapDrawable;
  viewHeight = cacheBitmap.getHeight();
  viewWight = cacheBitmap.getWidth();
   MatrixReset();
  postInvalidate();
 }
 
 public void setBitmapDrawableResources(int id){
  cacheBitmap = ((BitmapDrawable)mContex.getResources().getDrawable(id)).getBitmap();
  viewHeight = cacheBitmap.getHeight();
  viewWight = cacheBitmap.getWidth();
   MatrixReset();
  postInvalidate();
 }
 @Override
 protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);
  if(rotate>360f){
   rotate -=360f;
  }else if(rotate<-360f){
   rotate+=360f;
  }
  x = getX();
  y = getY();
  mMatrix.setRotate(rotate,x+viewHeight/2,y+ viewWight/2);
  canvas.drawBitmap(cacheBitmap,mMatrix,null);
  
 }
 public void MatrixLeft(float left){
  //设置旋转zh
  rotate +=left;
  postInvalidate();
 }
 public void MatrixRight(float right){
  rotate -=right;
  postInvalidate();
 }
 public void MatrixReset(){
  rotate =0f;
  postInvalidate();
 }
}


你可能感兴趣的:(Matrix)