Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android Camera 獲取 Matrix 作用於 Canvas 的研究

編輯:關於 Android 程式設計

網上查了一些相關文章,自以為看懂了,但實際測試時卻和網上說的並不一樣(後來研究發現,那個人寫的是錯的)。所以在此提醒大家,要勇於質疑,勇於提出問題。

首先說明 Camera 類,它位於 android.graphics 包下,用於圖像 3D 變換,透過旋轉、偏移等方式,最終透過 getMatrix(Matrix) 方法拿到矩陣,作用於 Canvas,使得畫面呈現出立體效果。

Camera 的源碼沒什麼可看的,這裡列舉它的主要方法:

 public native void save(); public native void restore(); public native void translate(float x, float y, float z); public native void rotateX(float deg); public native void rotateY(float deg); public native void rotateZ(float deg); public native void rotate(float x, float y, float z); public native float getLocationX(); public native float getLocationY(); public native float getLocationZ(); public native void setLocation(float x, float y, float z); public void getMatrix(Matrix matrix) { nativeGetMatrix(matrix.native_instance); } public void applyToCanvas(Canvas canvas) { nativeApplyToCanvas(canvas.mNativeCanvas); } public native float dotWithNormal(float dx, float dy, float dz);

這些方法看起來都不難,透過字面意思可以大致了解,其中最後幾個方法目前還未研究,後面研究了補上。

接著我們來看看 Camera 變換的幾個軸坐標。下圖才是正確的 x、y、z 軸坐標,而另一篇文章所說的左手坐標系,經過我的驗證,那個作者寫的是錯誤的。


以下分別對 camera 進行不同軸向的旋轉與位移變換:

進行變換:camera.rotateX(60);


進行變換:camera.rotateY(60);


進行變換:camera.rotateZ(-60);(這裡是 -60)


translate 和 canvas 的偏移類似,這裡不再上圖示意。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved