【Android入門 十】繪製圖片和圖片旋轉

來源:互聯網
上載者:User

標籤:bitmap   圖形   matrix   canvas   android   

首先簡略說明繪製圖片:

由於canvas.drawBitmap(bitmap, src, dst, paint);有四個參數所以大體有四五步。

1、通過getResource()方法得到drawable檔案夾裡BitmapDrawable類型的對象

2、將BitmapDrawable類型轉化為Bitmap類型的對象

3、建立src, dst,兩個長方形,src是指在原圖形上截得的圖片,dst是指貼到目標位置的長方形。

4、圖片的畫筆一般為null。

5、調用canvas.drawBitmap(bitmap, src, dst, paint)方法。

接著說明如何使圖片旋轉:

主要是通過matrix矩陣。

1、建立矩陣對象

2、確定旋轉方向

3、畫布canvas設定矩陣

注意:在canvas.setMatrix(matrix);後的canvas畫的任何圖形都會旋轉。

再畫完想要旋轉的映像後,調用canvas.setMatrix(null);方法,取消畫布的旋轉效果。


執行個體代碼:

/* * 插入圖片 */BitmapDrawable drawable = (BitmapDrawable)getResources().getDrawable(R.drawable.girls);Bitmap bitmap = drawable.getBitmap();Rect src = new Rect(0,0,440,320);Rect dst = new Rect(maxLeft, maxUp, maxRight, maxBottom);Matrix matrix = new Matrix();matrix.setRotate(45, 0, 0);canvas.setMatrix(matrix);canvas.drawBitmap(bitmap, src, dst, null);//canvas.setMatrix(null);
謝謝~

【Android入門 十】繪製圖片和圖片旋轉

聯繫我們

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