安卓开发-图像处理-使用Matrix操作图片
在Android中,通过Matrix来对图片进行缩放、旋平移等。
相关API:
Matrix.postScale(float sx,float sy) //缩放
Matrix.postRotate(float degrees)//旋转
Matrix.postTranslate(float dx,float dy)//平移
Matrix.reset()//重置
ImageView.setImageMatrix(Matrix matrix)// ImageView空间设置Matrix 对象并显示
1.缩放图片
ImageView的scaleType属性要设置为“matrix”
<ImageView
android:src="@drawable/hhh"
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="matrix"/>
使用matrix.postScale(scale_X, scale_Y)将图片进行缩放,以左上角为中心,X轴以scale_X,Y轴以scale_Y比例进行缩放。
使用imageView.setImageMatrix(matrix)重新显示缩放后的图片。
matrix.postScale(scale_X, scale_Y);
imageView.setImageMatrix(matrix);
结果展示
2.旋转图片
matrix.postRotate(degree)有两个方法,第一个是默认以左上角为中心进行图片旋转,第二个是指定像素点,并以它为中心点进行旋转
结果展示