android影像編輯和處理(二)

來源:互聯網
上載者:User

android影像編輯和處理(二)

1.輸入矩陣:

Matrix矩陣類,當現有位元影像對象上進行繪製或從另一個位元影像對象建立某個位元影像時,可以使用該類。

Matrix矩陣類的每個數字都應用於映像上每個點的3個座標(x,y或z)之一:

如:1 , 0 , 0,

0, 1, 0,

0, 0, 1

第一行:指定x座標:x=1x+0y+0z;

第二行:指定y座標:y=0x+1y+oz;

第三行:指定z座標:z=0x+0y+1z;

 

                               Matrix matrix=new Matrix();matrix.setValues(new float[]{1,0.5f,0,0,1,0,0,0,1});canvas.drawBitmap(bmp, matrix, paint);copyPicture.setImageBitmap(alteredBitmap);
在需要增加結果位元影像大小是可以設定:

 

alteredBitmap = Bitmap.createBitmap(bmp.getWidth()*2, bmp.getWidth(), bmp.getConfig());

2.Matrix類的方法

(1)旋轉:

Matrix matrix=new Matrix();

//matrix.setRotate(15);/*旋轉的角度,圍繞預設點(0,0),正數順時針旋轉,負數逆時針旋轉,預設點是映像左上方*/
matrix.setRotate(15, bmp.getWidth()/2,bmp.getHeight()/2);//中心旋轉
canvas.drawBitmap(bmp, matrix, paint);

(2)縮放:

matrix.setScale(1.5f, 1);//縮放,第一個參數x軸的縮放比例,第二個參數y軸的縮放比例。

(3)平移:

matrix.setTranslate(1.5f, -10);//平移,第一個參數x軸的移動數量,第二個參數y軸的移動數量。

(4)鏡像:

matrix.setScale(-1, 1);
matrix.postTranslate(bmp.getWidth(), 0);//向右移動

(5)翻轉:

matrix.setScale(1,-1);
matrix.postTranslate(0, bmp.getHeight());//向下移動

(6)繪製替代映像:

以前所使用的方法的缺點是映像會被截斷,因為沒有計算轉換後的結果大小。解決方案:當初次建立位元影像對象時,應用Matrix對象而不是繪製到一個空的位元影像對象中。

通過這種方式無需再獲得Canvas和Paint對象。缺點是不能再已變更位元圖對象。

在Bitmap類中存在一個靜態方法createBitmap.

Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, false);//參數不再說明。

樣本:

 

Matrix matrix=new Matrix();matrix.setRotate(15, bmp.getWidth()/2,bmp.getHeight()/2);//中心旋轉Bitmap alteredBitmap=Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, false);
                                copyPicture.setImageBitmap(alteredBitmap);


 

聯繫我們

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