布局文件
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context=".MainActivity" > 11 12 <LinearLayout 13 android:layout_width="match_parent" 14 android:layout_height="wrap_content" > 15 16 <Button 17 android:layout_width="0dp" 18 android:layout_height="wrap_content" 19 android:layout_weight="1" 20 android:onClick="zoomIn" 21 android:text="放大" /> 22 23 <Button 24 android:layout_width="0dp" 25 android:layout_height="wrap_content" 26 android:layout_weight="1" 27 android:onClick="zoomOut" 28 android:text="缩小" /> 29 30 <Button 31 android:layout_width="0dp" 32 android:layout_height="wrap_content" 33 android:layout_weight="1" 34 android:onClick="rotate" 35 android:text="旋转" /> 36 37 <Button 38 android:layout_width="0dp" 39 android:layout_height="wrap_content" 40 android:layout_weight="1" 41 android:onClick="inverse" 42 android:text="镜像" /> 43 </LinearLayout> 44 45 <ImageView 46 android:id="@+id/imageView" 47 android:layout_width="wrap_content" 48 android:layout_height="wrap_content" 49 android:src="@drawable/qq" /> 50 51 </LinearLayout>
代码
1 public class MainActivity extends Activity { 2 3 private ImageView imageView; 4 private Bitmap bitmap; 5 6 @Override 7 protected void onCreate(Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.activity_main); 10 imageView = (ImageView) findViewById(R.id.imageView); 11 bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.qq); 12 } 13 14 public void zoomIn(View v) { 15 Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth() * 2, bitmap.getHeight() * 2, bitmap.getConfig()); // 创建新的Bitmap, 指定大小 16 Canvas canvas = new Canvas(newBitmap); // 创建画布 17 Matrix matrix = new Matrix(); // 创建模型 18 matrix.postScale(2, 2); // 设置缩放比例, x轴和y轴都是2倍大小 19 canvas.drawBitmap(bitmap, matrix, null); // 参照matrix模型, 在画布上按照bitmap画 20 21 imageView.setImageBitmap(newBitmap); // 把新图片设置到ImageView中 22 bitmap = newBitmap; // 记住新图片, 下次从这个图片操作 23 } 24 25 public void zoomOut(View v) { 26 Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getConfig()); 27 Canvas canvas = new Canvas(newBitmap); 28 Matrix matrix = new Matrix(); 29 matrix.postScale(0.5f, 0.5f); 30 canvas.drawBitmap(bitmap, matrix, null); 31 32 imageView.setImageBitmap(newBitmap); 33 bitmap = newBitmap; 34 } 35 36 public void rotate(View v) { 37 Bitmap newBitmap = Bitmap.createBitmap(bitmap.getHeight(), bitmap.getWidth(), bitmap.getConfig()); 38 Canvas canvas = new Canvas(newBitmap); 39 Matrix matrix = new Matrix(); 40 matrix.postRotate(90, bitmap.getWidth() / 2, bitmap.getHeight() / 2); 41 canvas.drawBitmap(bitmap, matrix, null); 42 43 imageView.setImageBitmap(newBitmap); 44 bitmap = newBitmap; 45 } 46 47 public void inverse(View v) { 48 Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight() , bitmap.getConfig()); 49 Canvas canvas = new Canvas(newBitmap); 50 Matrix matrix = new Matrix(); 51 matrix.postScale(-1, 1); // -1 代表x轴做镜像(镜像之后图片会移动到左边) 52 matrix.postTranslate(bitmap.getWidth(), 0); // 向右移动 53 canvas.drawBitmap(bitmap, matrix, null); 54 55 imageView.setImageBitmap(newBitmap); 56 bitmap = newBitmap; 57 } 58 59 }