Android--Matrix(一)

來源:互聯網
上載者:User

標籤:

Matrix

Matrix ,中文裡叫矩陣,高等數學裡有介紹,在影像處理方面,主要是用於平面的縮放、平移、旋轉等操作。

在Android中,scale(縮放),rotation(旋轉)和 translation(平移)都是以矩陣(Matrix)的形式定義的,實際上在圖形學中,這些平面圖形的變換都是以矩陣的形式存在的。

左邊一行,會用到右面的兩列。

在 Android 裡面, Matrix 由 9 個 float 值構成,是一個 3*3 的矩陣。如。

       

解釋一下,上面的 sinX 和 cosX ,表示旋轉角度的 cos 值和 sin 值,注意,旋轉角度是按順時針方向計算的。

translateX 和 translateY 表示 x 和 y 的平移量。 

scale 是縮放的比例, 1 是不變, 2 是表示縮放 1/2,這樣子。

public class MyView extends View {    private Bitmap mBitmap;    private Matrix mMatrix = new Matrix();    public MyView(Context context) {        super(context);        initialize();    }    private void initialize() {                mBitmap = ((BitmapDrawable)getResources().getDrawable(R.drawable.show)).getBitmap();                float cosValue = (float) Math.cos(-Math.PI/6);        float sinValue = (float) Math.sin(-Math.PI/6);        mMatrix.setValues(                new float[]{                        cosValue, -sinValue, 100,                        sinValue, cosValue, 100,                        0, 0, 2});    }    @Override protected void onDraw(Canvas canvas) {//        super.onDraw(canvas);  //當然,如果介面上還有其他元素需要繪製,只需要將這句話寫上就行了。        canvas.drawBitmap(mBitmap, mMatrix, null);    }}

 

以左上方為頂點,縮放一半,逆時針旋轉30度,然後沿x軸和y軸分別平移50個像素,代碼裡面寫的是100,為什麼是平移50呢,因為縮放了一半。

大家可以自己設定一下Matrix的值,或者嘗試一下兩個Matrix相乘,得到的值設定進去,這樣才能對Matrix更加熟練。

這裡講的直接賦值的方式也許有點不好理解,不過還好, andrid 提供了對矩陣的更方便的方法,下一篇介紹 。

 

Android--Matrix(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.