主要是记一下matrix类的介绍和用法。
这是官方文 档中我觉得比较重要的一句话. The Matrix class holds a 3x3 matrix for transforming coordinates.
* api 翻译:矩阵类拥有一个3*3的矩阵是用来对坐标进行变换的
而其实就是对一个点进行矩阵变换, 而变换的原始矩阵就是一个单位矩阵
f[] ={1.0F,0.0F,0.0F,
0.0F,1.0F,0.0F,
0.0F,0.0F,1.0F}
用一个点{x0,y0,1},(因为主要是对2d图像做变换故最后那个坐标为1)后乘变换矩阵,得到的就是最终点的坐标。然后对想象的每一个点都进行同样的变换就得到你要的效果了。
比如说平移变换如图: 其中
x = x0 + △x
y = y0 + △y
直接操作矩阵其实很麻烦,其实在Matrix 给了很多方法来来很方便的改变变换矩阵。下面就给出一个小的demo。
(注意:記住在imageview做變換其實是對裡面的圖做變換,就一定先設置imageView和图的关系,要不然就不会生效
一定要加上 android:scaleType="matrix"
)
这是布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher"
android:scaleType="matrix"
/>
</RelativeLayout>
下面是activity
package com.example.martix;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.widget.ImageView;
/**
* @author yich
* @description
* @date 2014-11-28 下午5:09:16
* @version 1.0
*/
public class MainActivity extends Activity {
private ImageView imageView;
private Matrix matrix;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView=(ImageView) this.findViewById(R.id.image);
matrix = new Matrix();
/************1.旋转45度,默认以父布局的左上角我圆点旋转
* 其实变换矩阵就是
* f[] = {0.5f,-0.5F,0.0F,
0.5f,0.5F,0.0f,
0.0F,0.0F,1.0F};
*
*
* */
// matrix.postRotate(45.0f);
/************2.镜像*/
//f为变换矩阵
// final float f[] = {1.0F,0.0F,0.0F,
// 0.0F,-1.0F,240.0F,
// 0.0F,0.0F,1.0F};
// matrix.setValues(f);
/************3.切错变换*/
final float f[] = {3.0F,0.5F,0.0F,
0.5F,3.0F,0.0F,
0.0F,0.0F,1.0F};
matrix.setValues(f);
imageView.setImageMatrix(matrix);//把变换方式给imageview
}
}