★60.自定义控件 ★16.Matrix Camera
2017-06-30 本文已影响0人
iDragonfly
Camera常用方法表
方法类别 | 相关API | 简介 |
---|---|---|
基本方法 | save、restore | 保存、 回滚 |
常用方法 | getMatrix、applyToCanvas | 获取Matrix、应用到画布 |
平移 | translate | 位移 |
旋转 | rotat (API 12)、rotateX、rotateY、rotateZ | 各种旋转 |
相机位置 | setLocation (API 12)、getLocationX (API 16)、getLocationY (API 16)、getLocationZ (API 16) | 设置与获取相机位置 |
基础概念
3D坐标系
Android 采用左手坐标系。

三维投影
三维投影是将三维空间中的点映射到二维平面上的方法。
摄像机
摄像机就是我们观察虚拟3D空间的眼睛。
方法
基本方法
camera.save(); // 保存状态
camera.retore(); // 回滚状态
常用方法
getMatrix
// 计算当前状态下矩阵对应的状态,并将计算后的矩阵赋值给参数matrix。
void getMatrix (Matrix matrix)
applyToCanvas
// 计算当前状态下矩阵对应的状态,并将计算后的矩阵应用到指定的canvas上。
void applyToCanvas (Canvas canvas)
平移
void translate (float x, float y, float z)
旋转
// (API 12) 可以控制View同时绕x,y,z轴旋转,可以由下面几种方法复合而来。
void rotate (float x, float y, float z);
// 控制View绕单个坐标轴旋转
void rotateX (float deg);
void rotateY (float deg);
void rotateZ (float deg);


旋转中心【???】
我们都知道,在2D中,不论是旋转,错切还是缩放都是能够指定操作中心点位置的,但是在3D中却没有默认的方法,如果我们想要让图片围绕中心点旋转怎么办? 这就要使用到我们在Matrix
原理提到过的方法:
Matrix temp = new Matrix(); // 临时Matrix变量
this.getMatrix(temp); // 获取Matrix
temp.preTranslate(-centerX, -centerY); // 使用pre将旋转中心移动到和Camera位置相同。
temp.postTranslate(centerX, centerY); // 使用post将图片(View)移动到原来的位置
相机位置
设置相机位置基本可以使用translate
替代。
void setLocation (float x, float y, float z); // (API 12) 设置相机位置,默认位置是(0, 0, -8)
float getLocationX (); // (API 16) 获取相机位置的x坐标,下同
float getLocationY ();
float getLocationZ ();
虚拟相机前后均可以拍摄
当View
不断接近摄像机并越过摄像机位置时,仍能看到View
,并且View
大小会随着距离摄像机的位置越来越远而逐渐变小,你可以理解为它有前置摄像头和后置摄像头。