少年郎,是時(shí)候打造自己的第一款狂拽酷炫的3D效果了!

封面

背景介紹

Android中有兩個(gè)Camera類。一個(gè)是android.hardware.Camera,用于對(duì)設(shè)備的攝像頭進(jìn)行操作。另一個(gè)是android.graphics.Camera,可用于進(jìn)行3D變換,然后把變換后的矩陣Matrix作用于Canvas等,我們本篇要介紹的就是這個(gè)Camera類。

玩轉(zhuǎn)Camera

前面我們提到過,Camera是一個(gè)能夠進(jìn)行3D變化的類,在進(jìn)行玩3D變換后,我們能夠通過mCamera.getMatrix(Matrix)把變換矩陣Matrix賦值,然后可以用在Canvas上?;蛘?,你可以直接通過mCamera.applyToCanvas(Canvas)直接把變換作用到一個(gè)Canvas上。

Android中的三維坐標(biāo)系

Android中的三維坐標(biāo)軸符合左手坐標(biāo)系。需要注意,Android中的三維坐標(biāo)系中的Y軸和二維的是相反的。


Android三維坐標(biāo)系

Camera默認(rèn)的位置是在(0, 0, -8)點(diǎn)。

Camera的變換操作

方法 說明
getMatrix(mMatrix) 給mMatrix賦值。
applyToCanvas(mCanvas) 將變換獲得的Matrix直接作用到mCanvas上。
rotate(x,y,z) 旋轉(zhuǎn)。
rotateX、rotateY、rotateZ 旋轉(zhuǎn)。
getLocationX、getLocationY、getLocationZ 獲得Camera的位置,默認(rèn)是在(0,0,-8)點(diǎn)。
setLocation(x,y,z) 設(shè)置camera的位置。
translate(x,y,z) 平移Camera。
save() 與Canvas的類似。
restore() 與Canvas類似。

Camera的方法并不多,所以使用起來也是比較簡(jiǎn)單明了的。

Camera的使用實(shí)例

由于使用Camera的核心就是獲得一個(gè)變換后的Matrix,所以你需要對(duì)Matrix具有一定的認(rèn)識(shí)。關(guān)于Matrix的入門級(jí)使用,可以參考一下我的這兩篇博客。
Android——Matrix變換矩陣的探索(1)
Android——Matrix變換矩陣的探索(2)

演示Demo1

image

下面是github源碼的鏈接:
Github源碼鏈接

3D ViewGroup演示

image

Github源碼鏈接

Camera用于自定義動(dòng)畫

直接上個(gè)代碼實(shí)例,用法和前面的例子沒什么本質(zhì)區(qū)別,都是通過Camera變換之后獲得Matrix矩陣。

public class Custom3DAnimation extends Animation {

    private Camera mCamera;
    private int centerWidth;
    private int centerHeight;

    public void setmRotateY(float mRotateY) {
        this.mRotateY = mRotateY;
    }

    private float mRotateY;

    public Custom3DAnimation() {
        mCamera = new Camera();
        mRotateY = 90;
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
                Matrix matrix = t.getMatrix();  //獲得Transformation的Matrix
                mCamera.save();//保存當(dāng)前鏡頭狀態(tài)
        mCamera.rotateY(mRotateY * interpolatedTime); //使相機(jī)旋轉(zhuǎn)
        mCamera.getMatrix(matrix); //將旋轉(zhuǎn)變換作用到matrix上
        mCamera.restore(); //合并鏡頭層
        matrix.preTranslate(centerWidth, centerHeight);//操作前平移
        matrix.postTranslate(-centerWidth, -centerHeight); //操作后平移

    }

    @Override
    public void initialize(int width, int height, int parentWidth, int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
                setDuration(5 * 1000); //設(shè)置默認(rèn)持續(xù)時(shí)間
        setFillAfter(true); //設(shè)置動(dòng)畫結(jié)束后是否保持狀態(tài)
        setInterpolator(new LinearInterpolator()); //設(shè)置插值器
        centerWidth = width / 2;
                centerHeight = height / 2;
    }
}

總結(jié)

Camera的使用其實(shí)并不復(fù)雜,只需要記住前面提到的幾個(gè)方法就行。由于Camera最終是輸出一個(gè)矩陣,所以還需要對(duì)矩陣有一定的掌握。上面我已經(jīng)給出了矩陣快速使用的指南,大家可以根據(jù)情況自行參考。

如果你覺得還不錯(cuò),那就動(dòng)動(dòng)小捶捶敲個(gè)關(guān)注、點(diǎn)個(gè)贊哦??!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容