24.Android之Paint和canvas簡單應用學習

來源:互聯網
上載者:User

標籤:

在Android中需要通過graphics類來顯示2D圖形,graphics中包括了Canvas(畫布)、Paint(畫筆)、Color(顏色)、Bitmap(映像)等常用的類。graphics具有繪製點、線、顏色、2D幾何圖形、影像處理等功能。

1.Paint(畫筆)類

要繪製圖形,首先得調整畫筆,按照自己的開發需要設定畫筆的相關屬性。Pain類的常用屬性設定方法如下:

setAntiAlias(); //設定畫筆的鋸齒效果

setColor(); //設定畫筆的顏色

setARGB(); //設定畫筆的A、R、G、B值

setAlpha(); //設定畫筆的Alpha值

setTextSize(); //設定字型的尺寸

setStyle(); //設定畫筆的風格(空心或實心)

setStrokeWidth(); //設定空心邊框的寬度

getColor(); //擷取畫筆的顏色

2.Canvas(畫布)類

drawRect(RectF rect, Paint paint) //繪製地區,參數一為RectF一個地區

drawPath(Path path, Paint paint) //繪製一個路徑,參數一為Path路徑對象

drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint) //貼圖,參數一就是我們常規的Bitmap對象,參數二是來源區域(這裡是bitmap),參數三是目的地區域(應該在canvas的位置和大小),參數四是Paint畫刷對象,因為用到了縮放和展開的可能,當原始Rect不等於目標Rect時效能將會有大幅損失。

drawLine(float startX, float startY, float stopX, float stopY, Paintpaint) //畫線,參數一起始點的x軸位置,參數二起始點的y軸位置,參數三終點的x軸水平位置,參數四y軸垂直位置,最後一個參數為Paint 畫刷對象。

drawPoint(float x, float y, Paint paint) //畫點,參數一水平x軸,參數二垂直y軸,第三個參數為Paint對象。

drawText(String text, float x, floaty, Paint paint) //渲染文本,Canvas類除了上面的還可以描繪文字,參數一是String類型的文本,參數二x軸,參數三y軸,參數四是Paint對象。

drawOval(RectF oval, Paint paint)//畫橢圓,參數一是掃描地區,參數二為paint對象;

drawCircle(float cx, float cy, float radius,Paint paint)// 繪製圓,參數一是中心點的x軸,參數二是中心點的y軸,參數三是半徑,參數四是paint對象;

drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)//畫弧,參數一是RectF對象,一個矩形地區橢圓形的界限用於定義在形狀、大小、電弧,參數二是起始角(度)在電弧的開始,參數三掃掠角度(度)開始順時針測量的,參數四是如果這是真的話,包括橢圓中心的電弧,並關閉它.

 

下面看一個自己寫的簡單例子:

首先,我們需要自訂一個類,比如MyView,繼承於View類。然後,複寫View類的onDraw()函數。最後,在onDraw()函數中使用Paint和Canvas對象繪製我們需要的圖形。

代碼如下:

 1 package com.example.paint_canvasdemo; 2  3 import android.content.Context; 4 import android.graphics.Canvas; 5 import android.graphics.Color; 6 import android.graphics.Paint; 7 import android.graphics.Paint.Style; 8 import android.view.View; 9 import java.lang.Math;10 11 public class MyView extends View {12     13     private int screenW, screenH;    //用於螢幕的寬高14 15     public MyView(Context context) {16         super(context);17         // TODO Auto-generated constructor stub18     }19 20     protected void onDraw(Canvas canvas) {21         super.onDraw(canvas);22         //獲得螢幕的寬和高23         screenW = this.getWidth();24         screenH = this.getHeight();25         26         Paint paint_circle = new Paint();27         // 設定顏色28         paint_circle.setColor(Color.BLUE);29         // 設定空心 Style.FILL為實心30         paint_circle.setStyle(Style.STROKE);31         // 消除鋸齒32         paint_circle.setAntiAlias(true);33         // 設定空心線寬34         paint_circle.setStrokeWidth(10);35         // 畫圓形36         canvas.drawCircle(screenW/2, screenH/2, 200, paint_circle);37         38         // 繪製正方形 39         paint_circle.setColor(Color.RED);40         int x1 = (int) Math.sqrt(2);41         int rect_xy = 200 * x1 / 2;42         canvas.drawRect( screenW/2-rect_xy, screenH/2-rect_xy , screenW/2+rect_xy, screenH/2+rect_xy, paint_circle);  43     }44 45 }

然後修改下MainActivity.java代碼即可:

 1 package com.example.paint_canvasdemo; 2  3 import android.app.Activity; 4 import android.os.Bundle; 5  6 public class MainActivity extends Activity { 7  8     @Override 9     protected void onCreate(Bundle savedInstanceState) {10         super.onCreate(savedInstanceState);11         //setContentView(R.layout.activity_main);12         setContentView(new MyView(this));13     }14 15  16 }

運行效果:

 

24.Android之Paint和canvas簡單應用學習

聯繫我們

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