1.初始化:
private TestMatrixImageView mTestMatrixImageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mTestMatrixImageView=new TestMatrixImageView(MainActivity.this); mTestMatrixImageView.setScaleType(ImageView.ScaleType.MATRIX);//?? mTestMatrixImageView.setOnTouchListener(new TouchListenerImpl()); setContentView(mTestMatrixImageView); }
a.平移:
private void testTranslate(){ Matrix matrix=new Matrix(); int width=mTestMatrixImageView.getBitmap().getWidth(); int height=mTestMatrixImageView.getBitmap().getHeight(); matrix.postTranslate(width, height); mTestMatrixImageView.setImageMatrix(matrix); showMatrixEveryValue(matrix); }
b.围绕图片中心点旋转:
private void testRotate(){ Matrix matrix=new Matrix(); int width=mTestMatrixImageView.getBitmap().getWidth(); int height=mTestMatrixImageView.getBitmap().getHeight(); matrix.postRotate(45f, width/2, height/2); matrix.postTranslate(width, height); mTestMatrixImageView.setImageMatrix(matrix); showMatrixEveryValue(matrix); }
c.缩放:
private void testScale() { Matrix matrix = new Matrix(); matrix.setScale(0.5f, 0.5f); mTestMatrixImageView.setImageMatrix(matrix); showMatrixEveryValue(matrix); }
d.水平倾斜:
private void testSkewX() { Matrix matrix = new Matrix(); matrix.setSkew(0.5f, 0); mTestMatrixImageView.setImageMatrix(matrix); showMatrixEveryValue(matrix); }
e.垂直倾斜:
private void testSkewY() { Matrix matrix = new Matrix(); matrix.setSkew(0, 0.5f); mTestMatrixImageView.setImageMatrix(matrix); showMatrixEveryValue(matrix); }
f. 水平且垂直倾斜:
private void testSkewXY() { Matrix matrix = new Matrix(); matrix.setSkew(0.5f, 0.5f); mTestMatrixImageView.setImageMatrix(matrix); showMatrixEveryValue(matrix); }
g.水平对称--图片关于X轴对称
private void testSymmetryX() { Matrix matrix = new Matrix(); int height = mTestMatrixImageView.getBitmap().getHeight(); float matrixValues[] = { 1f, 0f, 0f, 0f, -1f, 0f, 0f, 0f, 1f }; matrix.setValues(matrixValues); //若是matrix.postTranslate(0, height); //表示将图片上下倒置 matrix.postTranslate(0, height*2); mTestMatrixImageView.setImageMatrix(matrix); showMatrixEveryValue(matrix); }
h.垂直对称--图片关于Y轴对称:
private void testSymmetryY() { Matrix matrix = new Matrix(); int width=mTestMatrixImageView.getBitmap().getWidth(); float matrixValues[] = {-1f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 1f}; matrix.setValues(matrixValues); //若是matrix.postTranslate(width,0); //表示将图片左右倒置 matrix.postTranslate(width*2, 0); mTestMatrixImageView.setImageMatrix(matrix); showMatrixEveryValue(matrix); }
j.关于X=Y对称--图片关于X=Y轴对称:
private void testSymmetryXY() { Matrix matrix = new Matrix(); int width = mTestMatrixImageView.getBitmap().getWidth(); int height = mTestMatrixImageView.getBitmap().getHeight(); float matrixValues[] = { 0f, -1f, 0f, -1f, 0f, 0f, 0f, 0f, 1f }; matrix.setValues(matrixValues); matrix.postTranslate(width+height, width+height); mTestMatrixImageView.setImageMatrix(matrix); showMatrixEveryValue(matrix); }
testMatrixImageView.java:
public class TestMatrixImageView extends ImageView{ private Matrix mMatrix; private Bitmap mBitmap; public TestMatrixImageView(Context context) { super(context); mMatrix=new Matrix(); mBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.icon); } @Override protected void onDraw(Canvas canvas) { System.out.println("---> onDraw"); //画原图 canvas.drawBitmap(mBitmap, 0, 0, null); //画经过Matrix变化后的图 canvas.drawBitmap(mBitmap, mMatrix, null); super.onDraw(canvas); } @Override public void setImageMatrix(Matrix matrix) { System.out.println("---> setImageMatrix"); this.mMatrix.set(matrix); super.setImageMatrix(matrix); } public Bitmap getBitmap(){ System.out.println("---> getBitmap"); return mBitmap; } }