android.graphics.Camera解讀

來源:互聯網
上載者:User

此Camera非彼Camera,在android.graphics包裡的Camera,可以理解為2D圖形系統中的視角,或者照相機的機位,根據透視的原理我們可以實現一些簡單的3D效果。

在ApiDemo中可參考那個Rotate3dAnimation例子。

其實Camera就兩個方法:translate和rotate,下面逐一探討一番吧。

代碼很簡單,做一個小小的自訂View來做實驗罷了。我引入的這張圖片尺寸是480*600(因為手機是480*854)。

view plaincopy to clipboardprint?
protected void onDraw(Canvas canvas) {  
            canvas.drawColor(0xFFCCCCCC);     
             
            //Log.d(TAG,"bit w "+bitmap.getWidth()+",h "+bitmap.getHeight());  
             
            Paint paint = new Paint();  
            paint.setColor(Color.DKGRAY);  
            paint.setStrokeWidth(2.0f);  
            paint.setStyle(Paint.Style.STROKE);  
             
            canvas.drawLine(0, 600, 480, 600, paint);  
            for(int i=0;50*i<600;i++)  
                canvas.drawLine(0, 50*i, 480, 50*i, paint);  
            for(int i=0;50*i<480;i++)  
                canvas.drawLine(50*i, 0, 50*i, 600, paint);  
             
             
            Matrix matrix = new Matrix();  
             
            Camera c = new Camera();  
            c.save();  
             
            //機位的正向轉動引起圖片向螢幕裡翻轉  
            //但是繞Z軸的轉動,引起圖片平面的旋轉(2D效果)  
            c.rotateZ(45);  
             
            //X引起圖片向右移動50(正向朝右)  
            //Y引起圖片向上移動50(正向朝上)  
            //Z引起圖片縮小(正向指向自己)  
            c.translate(100f, 0f, 300f);  
            c.getMatrix(matrix);  
            //c.applyToCanvas(canvas);  
             
            c.restore();  
             
        //移到中心點上  
            matrix.preTranslate(-240, -300);  
            matrix.postTranslate(240, 300);  
             
            canvas.concat(matrix);  
             
            canvas.drawBitmap(bitmap, 0,0,null);  
            //canvas.drawBitmap(bitmap, matrix, null);  
             
             
        }  
    } 
protected void onDraw(Canvas canvas) {
            canvas.drawColor(0xFFCCCCCC);  
          
            //Log.d(TAG,"bit w "+bitmap.getWidth()+",h "+bitmap.getHeight());
          
            Paint paint = new Paint();
            paint.setColor(Color.DKGRAY);
            paint.setStrokeWidth(2.0f);
            paint.setStyle(Paint.Style.STROKE);
          
            canvas.drawLine(0, 600, 480, 600, paint);
            for(int i=0;50*i<600;i++)
                canvas.drawLine(0, 50*i, 480, 50*i, paint);
            for(int i=0;50*i<480;i++)
                canvas.drawLine(50*i, 0, 50*i, 600, paint);
          
          
            Matrix matrix = new Matrix();
          
            Camera c = new Camera();
            c.save();
          
            //機位的正向轉動引起圖片向螢幕裡翻轉
            //但是繞Z軸的轉動,引起圖片平面的旋轉(2D效果)
            c.rotateZ(45);
          
            //X引起圖片向右移動50(正向朝右)
            //Y引起圖片向上移動50(正向朝上)
            //Z引起圖片縮小(正向指向自己)
            c.translate(100f, 0f, 300f);
            c.getMatrix(matrix);
            //c.applyToCanvas(canvas);
          
            c.restore();
          
        //移到中心點上
            matrix.preTranslate(-240, -300);
            matrix.postTranslate(240, 300);
          
            canvas.concat(matrix);
          
            canvas.drawBitmap(bitmap, 0,0,null);
            //canvas.drawBitmap(bitmap, matrix, null);
          
          
        }
    }

通過實驗可知:

translate(x,y,z),很好理解,見代碼中的注釋。座標軸似乎與我們常規的理解不一樣?為什麼會這樣呢?
rotateX,rotateY,rotateZ,也很好理解,機位的移動,對圖片的影響正好是反向的。

最後要注意的是有兩種方法應用這個機位的修改,效果也不一樣,說不清,您可以自己嘗試一下。

 

本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/sharetop/archive/2010/02/01/5277655.aspx

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.