android 繪圖之Canvas,Paint類

來源:互聯網
上載者:User

標籤:android   style   blog   color   strong   os   

Canvas,Paint

1.在android 繪圖但中經常要用到Canvas和Paint類,Canvas好比是一張畫布,上面已經有你想繪製圖畫的輪廓了,而Paint就好比是畫筆,就要給Canvas進行添色等操作。

這兩個類通常都是在onDraw(Canvas canvas)方法中用的。

2.Bitmap:代表一張位元影像,BitmapDrawable裡封裝的突變就是一個Bitmao對象

3.Canvas裡面有一些例如:

drawArc(參數) 繪製弧

drawBitmao(Bitmap bitmap ,Rect rect,Rect dst,Paint paint)  在指定點繪製從源圖中"挖取"的一塊

clipRect(float left,float top,float right,float bottom)  剪下一個矩形地區

clipRegion(Region region)  剪下一個指定地區。

Canvas除了直接繪製一個基本圖形外,還提供了如下方法進行座標變化:

rotate(float degree,float px, float py):對Canvas執行旋轉變化

scale(float sx,float sy,float px,float py):對Cnavas進行縮放變換

skew(float sx,float sy):對Canvas執行傾斜變換

translate(float dx,float dy):對Cnavas執行移動

4.Paint類主要用於設定繪製風格包括畫筆顏色,畫筆粗細,填充風格等,

Paint提供了一些方法

setARGB(int a,int r,int g,int b)/setColor(int color)  :設定顏色

等一些方法

5.下面通過一個例子來說明一下這兩個類:

 1 public class MyView extends View { 2  3     public MyView(Context context) { 4         super(context); 5         // TODO Auto-generated constructor stub 6     } 7  8     public MyView(Context context, AttributeSet attrs) { 9         super(context, attrs);10         // TODO Auto-generated constructor stub11     }12 13     public MyView(Context context, AttributeSet attrs, int defStyle) {14         super(context, attrs, defStyle);15         // TODO Auto-generated constructor stub16     }17 18     // 重寫該方法,進行繪圖19     @Override20     protected void onDraw(Canvas canvas) {21         super.onDraw(canvas);22         // 整張畫布繪製成白色23         canvas.drawColor(Color.WHITE);24         Paint paint = new Paint();25         // 去鋸齒26         paint.setAntiAlias(true);27         paint.setColor(Color.BLUE);28         paint.setStyle(Style.STROKE);29         paint.setStrokeWidth(3);30         // 繪製圖形31         canvas.drawCircle(40, 40, 30, paint);32         // 繪製正方型33         canvas.drawRect(10, 80, 70, 140, paint);34         // 繪製矩形35         canvas.drawRect(10, 150, 70, 190, paint);36         RectF rel = new RectF(10, 200, 70, 230);37         // 繪製圓角矩形38         canvas.drawRoundRect(rel, 15, 15, paint);39         RectF rell = new RectF(10, 240, 70, 270);40         // 繪製橢圓41         canvas.drawOval(rell, paint);42         // 定義一個Path對象,封閉成一個三角形43         Path path1 = new Path();44         path1.moveTo(10, 340);45         path1.lineTo(70, 340);46         path1.lineTo(40, 290);47         path1.close();48 49     }50 51 }

聯繫我們

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