android.graphics.Matrix
Matrix类包含了一个3x3的矩阵用来改变坐标,它没有一个构造器来初始化它里边的内容,所以创建实例后需要调用reset()方法生成一个标准matrix,或者调用set..一类的函数,比如setTranslate, setRotate,,该函数将会决定matrix如何来改变坐标。SDK里边没有讲述Matrix的3x3矩阵是如何改变点的坐标值的,但是我在代码里边通过打印那9个点的值时,大致可以得到如下结论,9个值[a,b,c,d,e,f,g,h,i],坐标[x,y],当g=0,h=0,i=1,的时候,坐标是这样变换的,x'=a*x+b*y+c;y'=d*x+e*y+f;当调用setTranslate(10,20)之后,matrix的值就变成[1,0,10,0,1,20,0,0,1];这其实还是没有脱离矩阵乘法,只是不知道后三位是如何应用的。了解这个对后边的连接矩阵的理解有好处,连接矩阵其实就是两个矩阵相乘后得到的新矩阵。
public Matrix()
创建一个标准矩阵,应用后点的坐标不会有任何改变
public Matrix(Matrix src)
创建一个src的深度复制,改变规则与src一致
public boolean equals(Object obj)
如果obj为Matrix并且它的值与当前Matrix对象相等的会将会返回true
public void getValues(float[] values)
获取matrix的那9个值
public boolean invert(Matrix inverse)
将当前矩阵反转,并且反转后的值存入inverse中,如果当前矩阵不能反转,那么inverse不变,返回false,反转规则应该是满足 当前矩阵*inverse=标准矩阵,标准矩阵为[1,0,0,0,1,0,0,0,1];不过其实也不用想得那么复杂,比如当前matrix是setTranslate(10,20),那么反转后的matrix就是setTranslate(-10,-20);
public boolean isIdentity()
判断当前矩阵是否为标准矩阵,这个函数比if (getType() == 0)运行的可能更快一些
public void mapPoints(float[] dst, int dstIndex, float[] src, int srcIndex, int pointCount)
用当前矩阵改变src中的点的坐标,然后将改变后的值对应的存入dst数组中,其中pointCount表示点的数目,(x,y)=(src[2*k],src[2*k+1])表示一个点的坐标,k取整数值,安卓中用数组存储点的坐标值的时候都是按如此法则存储的。
public void mapPoints(float[] pts)
用当前矩阵改变pts中的值,然后存储在pts中,同上,pts也是存储点的坐标的数组
public void mapPoints(float[] dst, float[] src)
用当前矩阵改变src的值,并且存储到数组dst中
public float mapRadius(float radius)
将一个半径为radius的圆的所有点坐标用matrix进行变换后,计算出该圆的半径并且返回该值。注意:要得到正确的值该圆默认是有中心的,个人注:说实话不明白这个函数有什么用
public boolean mapRect(RectF dst,RectF src)
用matrix改变src的4个顶点的坐标,并将改变后的坐标调整后存储到dst中,(RectF只能存储改变后的左上角和右下角坐标,所以需要调整),返回的值跟rectStaysRect()一样,从字面意思可以认为src改变后仍然是RectF,那么就返回true
public boolean mapRect(RectF rect)
用matrix改变rect的4个顶点的坐标,并将改变后的坐标调整后存储到rect当中
public void mapVectors(float[] dst, float[] src)
用matrix改变dst中的向量值并且存储到src当中,注意:setTranslate(x,y)这样的matrix调用了这个函数后不会有任何反应,这样的matrix应该调用mapPoints
public void mapVectors(float[] vecs)
用matrix改变vecs中的值并且存储到vecs当中,同上,注意:setTranslate(x,y)这样的matrix调用了这个函数后不会有任何反应,这样的matrix应该调用mapPoints
public void mapVectors(float[] dst, int dstIndex, float[] src, int srcIndex, int vectorCount)
同上,只不过vectorCount表示向量的数目,dstIndex,srcIndex分别表示各自的起始位置
public boolean postConcat(Matrix other)
将当前的matrix连接到other之后,并且将连接后的值写入当前matrix。 M‘=other*M,连接后变换的效果,相当于先变换M,然后在other变换
public boolean postRotate(float degrees)
相当于这样:Matrix other=newMatrix();other.setRotate(degrees);postConcat(other);
先创建设置一个以0,0为原点旋转degrees度的矩阵other,然后将当前的matrix连接到other之后,并且将连接后的值写入当前matrix。
public boolean postRotate(float degrees, float px, float py)
同上,不过改成这样Matrix other=newMatrix();other.setRotate(degrees,px,py);postConcat(other);
public boolean postScale(float sx, float sy)
同上,无非是改成other.setScale(sx,sy);
public boolean postScale(float sx, float sy, float px, float py)
同上
public boolean postSkew(float kx, float ky)
public boolean postSkew(float kx, float ky, float px, float py)
public boolean postTranslate(float dx, float dy)
都是一样的,不过是创建的other有所不一样而已
public boolean preConcat(Matrix other)
public boolean preRotate(float degrees)
public boolean preRotate(float degrees, float px, float py)
public boolean preScale(float sx, float sy)
public boolean preScale(float sx, float sy, float px, float py)
public boolean preSkew(float kx, float ky)
public boolean preSkew(float kx, float ky, float px, float py)
public boolean preTranslate(float dx, float dy)
同上边对应的函数功能类似,无非是other被连接在当前matrix之后,然后将连接后的值写入当前matrix当中
public boolean rectStaysRect()
如果该matrix可以将rectF变换成rectF,那么该函数返回true,在标准变换,伸缩变换,平移变换,和多个90度的旋转变换时,该函数是返回true的
public void reset()
将当前matrix设置为标准矩阵
public void set(Matrix src)
将src的内容深度复制给当前矩阵,如果src为null,那么当前矩阵变为标准矩阵
public boolean setConcat(Matrix a,Matrix b)
将当前matrix的值变为a和b的乘积
public boolean setPolyToPoly(float[] src, int srcIndex, float[] dst, int dstIndex, int pointCount)
将当前matrix的值设置为这样的值,对src变换后可以得到dst的数据,pointCount表示点的数目,只能是0-4。设置成功返回true
public boolean setRectToRect(RectF src,RectF dst, Matrix.ScaleToFit stf)
将当前matrix的值设置为这样的值,对src进行变换后可以得到dst,因两者都是RectF,所以该matrix的值只能是伸缩和平移的组合,设置成功了返回true,stf为伸缩参数,这个Matrix.ScaleToFit伸缩参数有什么名堂呢,它有四个常量,每个常量应用后会导致matrix有什么结果呢,根据那4个常量的文字说明可知,CENTER,END,START表示得到的伸缩矩阵m,m对src进行变换后得到dst1,dst1跟src有同样的宽高比例,dst1在dst的内部,不同的地方是CENTER的状态是这样的:dst1.left-dst.left=dst.right-dst1.right,dst1.top-dst.top=dst.bottom-dst1.bottom;END的状态是这样的:dst1.right=dst.right,dst1.bottom=dst.bottom.START的状态是这样的:dst1.left=dst.left,dst1.top=dst.top;至于FILL表示得到的伸缩矩阵m,通过它对src变换后得到的Rect就是dst,完全重合。结论通过RectF(0,0,10,10), RectF(0,0,20,30)这两个矩阵得到了验证。
public void setRotate(float degrees)
设置当前matrix,使作用于点坐标时使点坐标以点(0,0)为原点旋转degrees度。
public void setRotate(float degrees, float px, float py)
设置当前matrix,使作用于点坐标时使点坐标以点(px,py)为原点旋转degrees度。在转换过程中,该原点不可改变
public void setScale(float sx, float sy, float px, float py)
设置当前matrix,使作用于点坐标时使点坐标以(px,py)为支点伸缩sx,sy倍。(px,py)在转换过程中不能改变。这个解释有点蒙,验证了下发现其实就是x'=(x+px)*sx,y'=(y+py)*sy
public void setScale(float sx, float sy)
这其实就是setScale(sx,sy,0,0);
public void setSinCos(float sinValue, float cosValue)
这其实就是setSinCos(sinValue,cosValue,0,0);
public void setSinCos(float sinValue, float cosValue, float px, float py)
设置当前matrix,以px,py为支点进行旋转变换,变换方式与sinValue,cosValue的值有关,经过验证,可以得到近似换算公式为:x'=cosValue*x-sinValue*y+(1-cosValue)*px+sinValue*py;y'=sinValue*x+cosValue*y-sinValue*px+(1-cosValue)*py;
public void setSkew(float kx, float ky, float px, float py)
设置当前matrix,以px,py为支点进行倾斜kx,ky.公式变换应该为x'=x+kx*(y-py),y'=ky*(x-px)+y;
public void setSkew(float kx, float ky)
相当于setSkew(kx,ky,0,0);
public void setTranslate(float dx, float dy)
设置matrix,应用时使点坐标(x,y)各自平移为(x+dx,y+dy);
public void setValues(float[] values)
复制9个数据给matrix,由于matrix的变形,或许这些数据会变成16位的数据,所以用getValues()可能不能得到与初始化相同的数据。不出意外的话,values的后三位要是0,0,1,否则可能该matrix变化后得不到你想要的点坐标
public String toShortString ()
public String toString ()
返回一个字符串用来描述该目标和数据,该类的子类是鼓励重写该函数的,详细描述该对象的类型和数据。默认的描述方式如下
getClass().getName() + '@' + Integer.toHexString(hashCode())
补充源码如下
package com.hahajlu;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.view.View;
public class MatrixActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new SampleView(this));
}
}
class SampleView extends View
{
Matrix mt1=new Matrix();
Matrix mt2=new Matrix();
public SampleView(Context context)
{
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
float p[]=new float[]{100.f,100.f};
// mt1.setRotate(90);
mt1.setValues(new float[]{1,0,1,0,1,3,1,2,1});
// mt1.mapPoints(p);
// mt1.setScale(0.5f, 0.5f);
// mt1.mapPoints(p);
// mt1.setTranslate(10.f, 10.f);
//mt1.invert(mt2);
// mt1.setRectToRect(new RectF(0,0,10,10), new RectF(0,0,20,30), Matrix.ScaleToFit.FILL);
// mt1.setScale(0.5f, 0.5f, 20f, 30f);
// mt1.setSinCos(0.5f, 0.6f,30.f,20.f);
// mt1.setSkew(10f, 15f, 20f, 32f);
float values[]=new float[9];
mt1.getValues(values);
mt1.mapPoints(p);
Paint paint=new Paint();
paint.setColor(Color.BLACK);
canvas.drawColor(Color.WHITE);
canvas.drawText("为了", p[0], p[1], paint);
System.out.println("x="+p[0]+"y="+p[1]);
for(int i=0;i<9;i++)
System.out.println("values="+values[i]);
// float radiu=mt1.mapRadius(10.f);
// System.out.println("radiu="+radiu);
super.onDraw(canvas);
}
}
android.graphics.Matrix的更多相关文章
- Matrix: android 中的Matrix (android.graphics.Matrix) (转)
本篇博客主要讲解一下如何处理对一个Bitmap对象进行处理,包括:缩放.旋转.位移.倾斜等.在最后将以一个简单的Demo来演示图片特效的变换. 1. Matrix概述 对于一个图片变换的处理,需要Ma ...
- [Android]android.graphics.Camera实现图像的旋转、缩放,配合Matrix实现图像的倾斜
android.graphics.Camera可以对图像执行一些比较复杂的操作,诸如旋转与绽放,与Matrix可实现图像的倾斜. 个人总结Camera与Matrix的一些区别如下: Camera的ro ...
- Android笔记——Matrix
转自:http://www.cnblogs.com/qiengo/archive/2012/06/30/2570874.html#translate Matrix的数学原理 在Android中,如果你 ...
- Android学习记录(9)—Android之Matrix的用法
Matrix ,中文里叫矩阵,高等数学里有介绍,在图像处理方面,主要是用于平面的缩放.平移.旋转等操作. 首先介绍一下矩阵运算.加法和减法就不用说了,对应位相加就好.图像处理,主要用到的是乘法 .下面 ...
- Android 之 Matrix(转)
原文:http://www.cnblogs.com/qiengo/archive/2012/06/30/2570874.html#code Android Matrix Matrix的数学原理 平 ...
- Android Graphics专题(1)--- Canvas基础
作为Android Graphics专题的开篇.毫无疑问,我们将讨论Android UI技术的核心概念--Canvas. Canvas是Android UI框架的基础,在Android的控件体系中.全 ...
- int android.graphics.Bitmap.getRowBytes()
int android.graphics.Bitmap.getRowBytes() Return the number of bytes between rows in the bitmap's pi ...
- 【转】android Graphics(四):canvas变换与操作
android Graphics(四):canvas变换与操作 分类: 5.andriod开发2014-09-05 15:05 5877人阅读 评论(18) 收藏 举报 目录(?)[+] 前言 ...
- Android开发之parseSdkContent failed Could not initialize class android.graphics.Typeface
在进行android开发过程中,忽然发现经常弹出来parseSdkContent failed 这个错误,然后google了下解决办法 方法1: 删除.android文件 重启eclipse. 该方法 ...
随机推荐
- X86(32位)与X64(64位)有什么区别,如何选择对应的操作系统和应用程序?
X86就是我们一般用的32位的系统,指针长度为32位(386起):X64就是64位的系统,指针长度为64位. 选择硬件对应的软件,建议通过以下三条考虑:1.64位操作系统相对32位操作系统理论上性能会 ...
- 程序员带你十天快速入门Python,玩转电脑软件开发(四)
本系列文章立志于从一个已经习得一门编程语言的基础之上,全面介绍Python的相关开发过程和相关经验总结.本篇文章主要是基于上一篇的程序员带你十天快速入门Python,玩转电脑软件开发(三)的基础之上, ...
- 程序员带你十天快速入门Python,玩转电脑软件开发(三)
声明:本次教程主要适用于已经习得一门编程语言的程序员.想要学习第二门语言.有梦想,立志做全栈攻城狮的你 . 如果是小白,也可以学习本教程.不过可能有些困难.如有问题在文章下方进行讨论.或者添加QQ群5 ...
- hao123 百度品专 按品类 计算 下单数量 商品数量 下单金额?
SELECT * FROM t_tag_source WHERE s_name='hao123'; +--------+----------+---------+--------+-------- ...
- mysql锁表和解锁语句分享
对于MySQL来说,有三种锁的级别:页级.表级.行级 页级的典型代表引擎为BDB. 表级的典型代表引擎为MyISAM,MEMORY以及很久以前的ISAM. 行级的典型代表引擎为INNODB. ...
- C++ 常见问题
1:保证编译后方法名不被修改: The: extern "C" { function declarations here in h file } will disable C++ ...
- SOA,ESB 与 SCA
SOA,ESB与 SCA SOA 与 ESB SOA(Service Oriented Architecture),面向服务体系结构,是一种组件模型架构,一种支撑软件运行的相对稳定的结构.其本质是一种 ...
- Object layout in C++ and access control
The variables are guaranteed to be laid out contiguously, as in C. However, the access blocks may no ...
- iOS: 属性列表介绍 Introduction to Property Lists
iOS: 属性列表介绍 Introduction to Property Lists 从本质上说, 属性列表就是苹果的对象数据序列化与反序列化方式 属性列表使用几种数据类型把数据组织为键值表和值表 P ...
- WPF Window异形窗口演示
我们先通过简单的效果展示,切换展示不同图片: 我们先定义图片资源文件,我们可以在window资源中定义,下面的在app.xaml文件来定义: <Application x:Class=" ...