摘抄自:http://www.cnblogs.com/limingblogs/archive/2011/10/04/2199186.html
package com.example.matrixtest1; import android.os.Bundle; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; public class MatrixTest1 extends Activity { private EditText scaleEt; private EditText rotateEt; private EditText translateEt1; private EditText translateEt2; private ImageView imageView; private Matrix matrix; private Bitmap bitMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_matrix_test1); scaleEt = (EditText) findViewById(R.id.et_scale); rotateEt = (EditText) findViewById(R.id.et_rotate); translateEt1 = (EditText) findViewById(R.id.et_translateX); translateEt2 = (EditText) findViewById(R.id.et_translateY); imageView = (ImageView) findViewById(R.id.iv_matrix); bitMap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); imageView.setImageBitmap(bitMap); matrix = new Matrix(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_matrix_test1, menu); return true; } public void scaleBitmap(View view) { matrix.postScale(getValues(scaleEt), getValues(scaleEt)); imageView.setImageMatrix(matrix); } public void rotateBitmap(View view) { matrix.postRotate(getValues(rotateEt)); imageView.setImageMatrix(matrix); } public void translateBitmap(View view) { matrix.postTranslate(getValues(translateEt1), getValues(translateEt2)); imageView.setImageMatrix(matrix); } public void clearMatrix(View view) { matrix.reset(); imageView.setImageMatrix(matrix); } private float getValues(EditText et) { return Float.parseFloat(et.getText().toString()); } }
xml代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_marginTop="10dip" android:layout_width="fill_parent" android:layout_height="50dip" android:orientation="horizontal"> <EditText android:id="@+id/et_scale" android:layout_weight="1.0" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="缩放比例" android:text="0.25"/> <EditText android:id="@+id/et_rotate" android:layout_weight="1.0" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="30" android:hint="旋转角度" android:onClick="rotateBitmap"/> <EditText android:id="@+id/et_translateX" android:layout_weight="1.0" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="10" android:hint="X偏移"/> <EditText android:id="@+id/et_translateY" android:layout_weight="1.0" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="10" android:hint="y偏移"/> </LinearLayout> <LinearLayout android:layout_marginTop="10dip" android:layout_width="fill_parent" android:layout_height="50dip" android:orientation="horizontal"> <Button android:layout_weight="1.0" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="缩放" android:onClick="scaleBitmap"/> <Button android:layout_weight="1.0" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="旋转" android:onClick="rotateBitmap"/> <Button android:layout_weight="1.0" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="移动" android:onClick="translateBitmap"/> <Button android:layout_weight="1.0" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="还原" android:onClick="clearMatrix"/> </LinearLayout> <ImageView android:layout_weight="1.0" android:id="@+id/iv_matrix" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaleType="matrix"/> </LinearLayout>