android matrix
16lz
2022-07-30
目标:
本文将讲述如何如何在Android中使用Matrix实现图片的缩放和旋转,通过本文学习,你将学会如何通过Matrix操作图像。
代码示例:
直接上代码了,我在代码中附带了详细的解释,代码如下:
package com.eoeandroid.demo.testcode; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.view.ViewGroup.LayoutParams; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ImageView.ScaleType; public class bitmaptest extends Activity { public void onCreate(Bundle icicle) { super.onCreate(icicle); setTitle("eoeAndroid教程: 缩放和旋转图片 -by:IceskYsl"); LinearLayout linLayout = new LinearLayout(this); // 加载需要操作的图片,这里是eoeAndroid的logo图片 Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), R.drawable.eoe_android); //获取这个图片的宽和高 int width = bitmapOrg.getWidth(); int height = bitmapOrg.getHeight(); //定义预转换成的图片的宽度和高度 int newWidth = 200; int newHeight = 200; //计算缩放率,新尺寸除原始尺寸 float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; // 创建操作图片用的matrix对象 Matrix matrix = new Matrix(); // 缩放图片动作 matrix.postScale(scaleWidth, scaleHeight); //旋转图片 动作 matrix.postRotate(45); // 创建新的图片 Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width, height, matrix, true); //将上面创建的Bitmap转换成Drawable对象,使得其可以使用在ImageView, ImageButton中 BitmapDrawable bmd = new BitmapDrawable(resizedBitmap); //创建一个ImageView ImageView imageView = new ImageView(this); // 设置ImageView的图片为上面转换的图片 imageView.setImageDrawable(bmd); //将图片居中显示 imageView.setScaleType(ScaleType.CENTER); //将ImageView添加到布局模板中 linLayout.addView(imageView, new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT ) ); // 设置为本activity的模板 setContentView(linLayout); } }
这里没用定义XML布局模板,而是直接在代码中生成了需要的模板和视图组件,你可以可以定义XML模板,其他原理是一样的。
在游戏开发中,自定义View是一个相当重要的功能,下面先讲一讲在View上绘制所需的四个基本主键:
Bitmap:用于容纳像素点(android.graphics.Bitmap)
Canvas:负责调用绘制方法,是整个过程的入口
要绘制的对象:比如绘制一个Bitmap,矩形或者圆
Paint: 设置绘制图形的颜色和样式
Matrix:它包含一个3x3的矩阵,用于做变换匹配(图像处理中有讲),Matrix没有一个结构体,它必须被初始化,通过实现reset()方法或者set..()方法来实现。
下面来看代码:
package com.eoeandroid.demo.testcode; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.view.ViewGroup.LayoutParams; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ImageView.ScaleType; public class bitmaptest extends Activity { public void onCreate(Bundle icicle) { super.onCreate(icicle); setTitle("eoeAndroid教程: 缩放和旋转图片 -by:IceskYsl"); LinearLayout linLayout = new LinearLayout(this); // 加载需要操作的图片,这里是eoeAndroid的logo图片 Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), R.drawable.eoe_android); //获取这个图片的宽和高 int width = bitmapOrg.getWidth(); int height = bitmapOrg.getHeight(); //定义预转换成的图片的宽度和高度 int newWidth = 200; int newHeight = 200; //计算缩放率,新尺寸除原始尺寸 float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; // 创建操作图片用的matrix对象 Matrix matrix = new Matrix(); // 缩放图片动作 matrix.postScale(scaleWidth, scaleHeight); //旋转图片 动作 matrix.postRotate(45); // 创建新的图片 Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width, height, matrix, true); //将上面创建的Bitmap转换成Drawable对象,使得其可以使用在ImageView, ImageButton中 BitmapDrawable bmd = new BitmapDrawable(resizedBitmap); //创建一个ImageView ImageView imageView = new ImageView(this); // 设置ImageView的图片为上面转换的图片 imageView.setImageDrawable(bmd); //将图片居中显示 imageView.setScaleType(ScaleType.CENTER); //将ImageView添加到布局模板中 linLayout.addView(imageView, new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT ) ); // 设置为本activity的模板 setContentView(linLayout); } }
更多相关文章
- Android(安卓)matrix 控制图片的旋转、缩放、移动
- android拍照与读取相册
- Android(安卓)报错:Caused by: android.os.FileUriExposedExcepti
- android解决坚屏拍照和保存图片旋转90度的问题,并兼容4.0
- [Android]在App中使用相机
- android WebView 图片缩放功能小结
- Android(安卓)主流图片库Picasso Glide Fresco对比分析
- android背景选择器selector用法汇总
- ImageView的scaletype属性