Android繪圖具體應用方式總結

來源:互聯網
上載者:User

在Android作業系統中,有很多功能技巧可以協助我們輕鬆的實現一些需求。比如對映像映像的處理等等。我們在這裡就會為大家帶來一些有關Android繪圖的方法,希望能是朋友們充分掌握這方面的應用。

  • Android構造塊類別總結
  • Android Button應用法則
  • Android類比SD卡實現方法解析
  • Android onKey操作方式介紹
  • Android訊息傳遞應用功能解析

繪製各種圖形、文字使用Canvas類中drawRect、drawText等方法,詳細函數列表以及參數說明可以查看sdk

圖形的樣式由paint參數控制

Paint類也有很多參數設定方法

座標由Rect和RectF類管理

通過Canvas、Paint和Rect 就可以繪製遊戲中需要的大多數基本圖形了

Android繪圖中需要注意的一些細節

繪製實心矩形,需要設定paint屬性:paint.setStyle(Style.FILL); 通過Style的幾個枚舉值改變繪製樣式

以下寫的有點亂,隨時添加一些記錄點, 以後再整理啦~~~~~

1. Rect對象

一個地區對象Rect(left, top, right, bottom) , 是一個左閉右開的地區,即是說使用 Rect.contains(left, top)為true, Rect.contains(right, bottom)為false

2.drawLine方法

drawLine(float startX, float startY, float stopX, float stopY, Paint paint) 也是一個左閉右開的區間,只會繪製到stopX-1,stopY-1

驗證方法:

 
  1. Canvas c = canvas;  
  2. paint.setColor(Color.RED);  
  3. c.drawLine(x, y, x+c.getWidth()-1, y, paint);  
  4. c.drawLine(x, y+height-1, x+c.getWidth(), y+height-1, paint);  
  5. paint.setColor(Color.BLUE);  
  6. c.drawPoint(x+c.getWidth()-1, y, paint); 

說明drawLine是沒有繪製到右邊最後一個點的

3.drawRect(Rect r, Paint paint)

當繪製空心矩形時,繪製的是一個左閉右閉的地區

驗證方法:

 
  1. rect.set(x, y, x+width, y+height);  
  2. paint.setStyle(Style.STROKE);  
  3. paint.setColor(Color.BLUE);  
  4. c.drawRect(rect, paint);  
  5. paint.setColor(Color.RED);  
  6. c.drawLine(x, y, x+width, y, paint);  
  7. c.drawLine(x, y+height, x+width, y+height, paint);  
  8. c.drawLine(x, y, x, y+height, paint);  
  9. c.drawLine(x+width, y, x+width, y+height, paint); 

當繪製實心矩形時,繪製的是一個左閉右開的地區

驗證方法:

 
  1. rect.set(x, y, x+width, y+height);  
  2. paint.setColor(Color.RED);  
  3. c.drawLine(x, y, x+width, y, paint);  
  4. c.drawLine(x, y+height, x+width, y+height, paint);  
  5. c.drawLine(x, y, x, y+height, paint);  
  6. c.drawLine(x+width, y, x+width, y+height, paint);  
  7. paint.setStyle(Style.FILL);  
  8. paint.setColor(Color.BLUE);  
  9. c.drawRect(rect, paint); 

這個規則跟j2me也是一樣的,在j2me裡,drawRect長寬會多畫出1px。SDK的說明是:

The resulting rectangle will cover an area (width + 1) pixels wide by (height + 1) pixels tall. If either width or height is less than zero, nothing is drawn.

例如drawRect(10,10,100,1)繪製,結果是一個2px高的矩形,用fillRect(10,10,100,1),結果是一個1px高的矩形

以上就是對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.