图片的放大缩小
16lz
2021-01-23
布局文件
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 <LinearLayout13 android:layout_width="match_parent"14 android:layout_height="wrap_content" >15 16 <Button17 android:layout_width="0dp"18 android:layout_height="wrap_content"19 android:layout_weight="1"20 android:onClick="zoomIn"21 android:text="放大" />22 23 <Button24 android:layout_width="0dp"25 android:layout_height="wrap_content"26 android:layout_weight="1"27 android:onClick="zoomOut"28 android:text="缩小" />29 30 <Button31 android:layout_width="0dp"32 android:layout_height="wrap_content"33 android:layout_weight="1"34 android:onClick="rotate"35 android:text="旋转" />36 37 <Button38 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 <ImageView46 android:id="@+id/imageView"47 android:layout_width="wrap_content"48 android:layout_height="wrap_content"49 android:src="@drawable/qq" />50 51 </LinearLayout>View Code
代码
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 }
更多相关文章
- 关于android 调用系统图片浏览器并返回图片路径问题
- Android保存图片到本地
- 【Android自学笔记】Android获取手机和存储卡上的图片
- android中如何显示图片的一部分
- android > layout > background 背景图片重复
- 如何在EditText中设置固定图片——Android移动开发
- android 快速滚动条设置(像新浪微博,滚动的时候会出现悬浮的滚动
- picasso-强大的Android图片下载缓存库