Matrix矩阵介绍

在Android中,对图片的处理需要使用到Matrix类,Matrix是一个3 x 3的矩阵,内部就是个一维数组,内部有9个元素;可以通过setValues( float[])进行初始化,通过getValues(float[])拿到当前矩阵的值。
其具体坐标对应的属性为:
{MSCALE_X,MSKEW_X,MTRANS_X,
 MSKEW_Y, MSCALE_Y,MTRANS_Y,
 MPERSP_0,MPERSP_1,MPERSP_2}
比如我想知道现在x方向缩放比例:
matrix.getValues(matrixValues);
return matrixValues[Matrix.MSCALE_X];

操作示例
Matrix matrix = new Matrix();
matrix.setValues(new float[] {
        1, 0, 0,
        0, 1, 0,
        0, 0, 1
});
x'=1x+0y+0z(x坐标)
y'=0x+1y+0z(y坐标)
z'=0x+0y+1z(目前没用到)
如果第一行改为(0.5f,0,0),那么图像在x轴上将图像缩小到50%;如果第二行改为(0,2,0),那么图像在y轴上将图像放大到2倍;
如果第一行改为(1,0,100),那么图像在x轴上向右移动100像素;如果第二行改为(0,1,-100),那么图像在y轴上向上移动100像素;

为方便使用,Matrix提供了一些方法来控制图片变换:
  • setTranslate(float dx,float dy):控制Matrix进行平移。X轴,右移为正;Y轴,下移为正
  • setSkew(float kx,float ky):控制Matrix进行倾斜,kx、ky为X、Y方向上的比例
  • setSkew(float kx,float ky,float px,float py):控制Matrix以px、py为轴心进行倾斜(错切),kx、ky为X、Y方向上的倾斜比例
  • setRotate(float degrees):控制Matrix以(0,0)为轴心进行depress角度的旋转,顺时针为正
  • setRotate(float degrees,float px,float py):设置Matrix以(px,py)为轴心旋转
  • setScale(float sx,float sy):设置Matrix进行缩放,sx、sy为X、Y方向上的缩放比例。如果为负,那么会将该图像绘制到坐标系统的负值空间
  • setScale(float sx,float sy,float px,float py):设置Matrix以(px,py)为轴心进行缩放,sx、sy为X、Y方向上的缩放比例
       以上的set方法,均有对应的post和pre方法,Matrix调用一系列set、pre、post方法时,可视为将这些方法插入到一个队列。当然,按照队列中从头至尾的顺序调用执行。
其中pre表示在队头插入一个方法(相当于矩阵中的右乘),post表示在队尾插入一个方法(相当于矩阵中的左乘)。而set表示把当前队列清空,并且总是位于队列的最中间位置。当执行了一次set后,pre方法总是插入到set前部的队列的最前面,post方法总是插入到set后部的队列的最后面。
       错切变换,在数学上又称为Shear mapping(可译为“剪切变换”)或者Transvection(缩并),它是一种比较特殊的线性变换。错切变换的效果就是让所有点的x坐标(或者y坐标)保持不变,而对应的y坐标(或者x坐标)则按比例发生平移,且平移的大小和该点到x轴(或y轴)的垂直距离成正比。错切变换,属于等

PS:在Canvas上绘制图形时既可使用Matrix,也可使用ColorMatrix来改变在Canvas对象上绘制的Paint对象。

演示代码

public class MainActivity extends Activity {
    private ImageView iv_new;
    private Bitmap srcBmp;//原图
    private Bitmap alterBitmap;//创建的可修改的图
    private Canvas canvas;//画板
    private Paint paint;//画笔
    private Matrix matrix;//矩阵
    private float[] matrixValues = new float[3 * 3];//矩阵中的数组,默认[1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]
    //可以设置的参数
    private EditText et_width, et_height, et_scale_x, et_scale_y, et_rotate, et_skew_x, et_skew_y;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_matrix);
        iv_new = (ImageView) findViewById(R.id.iv_new);
        et_width = (EditText) findViewById(R.id.et_width);
        et_height = (EditText) findViewById(R.id.et_height);
        et_scale_x = (EditText) findViewById(R.id.et_scale_x);
        et_scale_y = (EditText) findViewById(R.id.et_scale_y);
        et_rotate = (EditText) findViewById(R.id.et_rotate);
        et_skew_x = (EditText) findViewById(R.id.et_skew_x);
        et_skew_y = (EditText) findViewById(R.id.et_skew_y);
        srcBmp = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
        paint = new Paint();
        matrix = new Matrix();
    }

    private void init() {
        int newWidth = (int) (srcBmp.getWidth() * Float.parseFloat(et_width.getText().toString()));
        int newHeight = (int) (srcBmp.getHeight() * Float.parseFloat(et_height.getText().toString()));
        //创建原图的一个副本。 此副本是可修改的。创建的是一个空白的图形。
        alterBitmap = Bitmap.createBitmap(newWidth, newHeight, srcBmp.getConfig());//Config.ARGB_8888,32位;Config.RGB_565,24位,忽略透明度
        canvas = new Canvas(alterBitmap);//画板的大小就是我们创建的副本的大小
        matrix.reset();//重置矩阵
    }
   
    //复制
    public void copy(View view) {
        init();
        matrix.getValues(matrixValues);
        //MSCALE_X,  MSKEW_X,  MTRANS_X,             MSKEW_Y,  MSCALE_Y,  MTRANS_Y,             MPERSP_0,  MPERSP_1,  MPERSP_2
        Log.i("bqt", Arrays.toString(matrixValues));//[1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]
        canvas.drawBitmap(srcBmp, matrix, paint);
        iv_new.setImageBitmap(alterBitmap);
    }
    //缩放
    public void zoom(View view) {
        init();
        matrix.setScale(Float.parseFloat(et_scale_x.getText().toString()), 1.0f * Float.parseFloat(et_scale_y.getText().toString()));
        matrix.getValues(matrixValues);
        Log.i("bqt", Arrays.toString(matrixValues));//[0.5, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]
        Log.i("bqt", matrixValues[Matrix.MSCALE_X] + "--" + matrixValues[Matrix.MSCALE_Y]);//0.5--1.0
        canvas.drawBitmap(srcBmp, matrix, paint);
        iv_new.setImageBitmap(alterBitmap);
    }
    //旋转。这里有一个bug:当进入界面后第一次点击的是"旋转"时,图片并没有旋转;若第一次点击的是其他按钮,则之后再点击就正常了,不知道为什么!
    public void rotate(View view) {
        init();
        //注意,getLeft()、getTop()等获取到的是控件【左上角】相对于【父控件】相应部位的值,setRotate中的参数为相对图片【自身】【左上角】的距离
        matrix.setRotate(Float.parseFloat(et_rotate.getText().toString()), iv_new.getWidth() / 2, iv_new.getHeight() / 2);
        matrix.getValues(matrixValues);
        Log.i("bqt", Arrays.toString(matrixValues));//[0.0, -1.0, 144.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0]
        Log.i("bqt", matrixValues[Matrix.MSKEW_X] + "--" + matrixValues[Matrix.MSKEW_Y]);//-1.0--1.0
        Log.i("bqt", matrixValues[Matrix.MTRANS_X] + "--" + matrixValues[Matrix.MTRANS_Y]);//144.0--0.0
        canvas.drawBitmap(srcBmp, matrix, paint);
        iv_new.setImageBitmap(alterBitmap);
    }
    //错切
    public void skew(View view) {
        init();
        matrix.setSkew(Float.parseFloat(et_skew_x.getText().toString()), Float.parseFloat(et_skew_y.getText().toString()));//kx、ky为X、Y方向上的比例
        matrix.getValues(matrixValues);
        Log.i("bqt", Arrays.toString(matrixValues));//[1.0, 0.5, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]
        Log.i("bqt", matrixValues[Matrix.MSKEW_X] + "--" + matrixValues[Matrix.MSKEW_Y]);//0.5--0.0
        canvas.drawBitmap(srcBmp, matrix, paint);
        iv_new.setImageBitmap(alterBitmap);
    }
    //镜面(移动+缩放)
    public void mirror(View view) {
        init();
        matrix.setScale(-1.0f, 1.0f);//y不变,x变成负的(此时已经是镜面效果了,但是图片不是在画布范围,所以无法显示)
        matrix.postTranslate(srcBmp.getWidth(), 0);//向右移动一个图片高度的距离
        matrix.getValues(matrixValues);
        Log.i("bqt", Arrays.toString(matrixValues));//[-1.0, 0.0, 144.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]
        Log.i("bqt", matrixValues[Matrix.MSCALE_X] + "--" + matrixValues[Matrix.MSCALE_Y]);//-1.0--1.0
        Log.i("bqt", matrixValues[Matrix.MTRANS_X] + "--" + matrixValues[Matrix.MTRANS_Y]);//144.0--0.0
        canvas.drawBitmap(srcBmp, matrix, paint);
        iv_new.setImageBitmap(alterBitmap);
    }
}

演示布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="vertical" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="copy"
            android:text="复制" />
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="zoom"
            android:text="缩放" />
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="rotate"
            android:text="旋转" />
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="skew"
            android:text="错切" />
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="mirror"
            android:text="镜面" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="画布横向缩放倍数"
            android:textSize="13sp" />
        <EditText
            android:id="@+id/et_width"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:minEms="3"
            android:text="1"
            android:textSize="13sp" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="画布纵向缩放倍数"
            android:textSize="13sp" />
        <EditText
            android:id="@+id/et_height"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:minEms="3"
            android:text="1"
            android:textSize="13sp" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="图片横向缩放倍数"
            android:textSize="13sp" />
        <EditText
            android:id="@+id/et_scale_x"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:minEms="3"
            android:text="0.5"
            android:textSize="13sp" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="图片纵向缩放倍数"
            android:textSize="13sp" />
        <EditText
            android:id="@+id/et_scale_y"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:minEms="3"
            android:text="1"
            android:textSize="13sp" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="中心旋转角度"
            android:textSize="13sp" />
        <EditText
            android:id="@+id/et_rotate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:minEms="3"
            android:text="90"
            android:textSize="13sp" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="错切相对值"
            android:textSize="13sp" />
        <EditText
            android:id="@+id/et_skew_x"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:minEms="3"
            android:text="0.5"
            android:textSize="13sp" />
        <EditText
            android:id="@+id/et_skew_y"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:minEms="3"
            android:text="0"
            android:textSize="13sp" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >
        <ImageView
            android:id="@+id/iv_old"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#0000ff"
            android:src="@drawable/icon" />
        <ImageView
            android:id="@+id/iv_new"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="1dp"
            android:background="#00ffff"
    />
    </LinearLayout>
</LinearLayout>

图片变换 矩阵 Bitmap Matrix的更多相关文章

  1. 图片变换【Matrix】矩阵 简介

    Matrix矩阵介绍 官方文档地址:https://developer.android.com/reference/android/graphics/Matrix.html 在Android中,对图片 ...

  2. android图片缩小和放大Matrix

    /**Bitmap放大的方法*/ private static Bitmap big(Bitmap bitmap) { Matrix matrix = new Matrix(); matrix.pos ...

  3. Android图片加载框架最全解析(五),Glide强大的图片变换功能

    大家好,又到了学习Glide的时间了.前段时间由于项目开发紧张,再加上后来又生病了,所以停更了一个月,不过现在终于又可以恢复正常更新了.今天是这个系列的第五篇文章,在前面四篇文章的当中,我们已经学习了 ...

  4. OpenGL投影矩阵(Projection Matrix)构造方法

    (翻译,图片也来自原文) 一.概述 绝大部分计算机的显示器是二维的(a 2D surface).在OpenGL中一个3D场景需要被投影到屏幕上成为一个2D图像(image).这称为投影变换(参见这或这 ...

  5. Android图片缓存之Bitmap详解

    前言: 最近准备研究一下图片缓存框架,基于这个想法觉得还是先了解有关图片缓存的基础知识,今天重点学习一下Bitmap.BitmapFactory这两个类. 图片缓存相关博客地址: Android图片缓 ...

  6. Android 图片旋转(使用Matrix.setRotate方法)

    imageView2 = (ImageView) findViewById(R.id.img2); Bitmap bitmap = BitmapFactory.decodeResource(getRe ...

  7. 【Math for ML】矩阵分解(Matrix Decompositions) (下)

    [Math for ML]矩阵分解(Matrix Decompositions) (上) I. 奇异值分解(Singular Value Decomposition) 1. 定义 Singular V ...

  8. 【Math for ML】矩阵分解(Matrix Decompositions) (上)

    I. 行列式(Determinants)和迹(Trace) 1. 行列式(Determinants) 为避免和绝对值符号混淆,本文一般使用\(det(A)\)来表示矩阵\(A\)的行列式.另外这里的\ ...

  9. Android图片缓存之Bitmap详解(一)

    前言: 最近准备研究一下图片缓存框架,基于这个想法觉得还是先了解有关图片缓存的基础知识,今天重点学习一下Bitmap.BitmapFactory这两个类. Bitmap: Bitmap是Android ...

随机推荐

  1. oracle遍历游标

    1,while循环 declare v_tname ); cursor c_tname is select a.TABLE_NAME from user_tables a where a.TABLES ...

  2. JavaWeb学习笔记之Servlet(二)

    1.  GenericServlet 抽象类: 1). 是一个 Serlvet. 是 Servlet 接口和 ServletConfig 接口的实现类. 但是一个抽象类. 其中的 service 方法 ...

  3. [转]C++ list 类学习笔记

    双向循环链表list list是双向循环链表,,每一个元素都知道前面一个元素和后面一个元素.在STL中,list和vector一样,是两个常被使用的容器.和vector不一样的是,list不支持对元素 ...

  4. Laravel5中集成Jasig cas统一认证系统

    CAS : CAS(Central Authentication Service)是一款不错的针对 Web 应用的单点登录框架,这里介绍下我刚在laravel5上搭建成功的cas.提前准备工作:可运行 ...

  5. 深入mysql慢查询设置的详解

    set long_query_time=1; #设置慢查询时间为1 秒; set global slow_query_log=on; #开启慢查询日志; show global status like ...

  6. ubuntu 下telnet 操纵memcache 实现

    memcache作为一款优秀的进程外缓存,常常被运用于高并发系统架构中.这里主要谈谈怎么通过telnet工具,查看memcache运行状况并对其key进行管理维护.假设memcache安装目录:/us ...

  7. Entity Framework with MySQL 学习笔记一(复杂类型 Complex Types)

    有时候我们希望在sql一个表里面的column, 一部分被分化成另一个class 典型的例子是 Address 直接看代码: [Table("member")] public cl ...

  8. Summation of primes

    是我算法不对,还是笔记本CPU太差? 我优化了两次,还是花了三四个小时来得到结果. 在输出上加1就是最终结果. The sum of the primes below 10 is 2 + 3 + 5 ...

  9. BZOJ3564 信号增幅仪

    http://www.lydsy.com/JudgeOnline/problem.php?id=3564 思路:先旋转坐标系,再缩进x坐标,把椭圆变成圆,然后做最小圆覆盖. 还有,为什么用srand( ...

  10. [LeetCode 119] - 杨辉三角形II(Pascal's Triangle II)

    问题 给出一个索引k,返回杨辉三角形的第k行. 例如,给出k = 3,返回[1, 3, 3, 1] 注意: 你可以优化你的算法使之只使用O(k)的额外空间吗? 初始思路 首先来复习复习杨辉三角形的性质 ...