在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
驗證方法:
- Canvas c = canvas;
- paint.setColor(Color.RED);
- c.drawLine(x, y, x+c.getWidth()-1, y, paint);
- c.drawLine(x, y+height-1, x+c.getWidth(), y+height-1, paint);
- paint.setColor(Color.BLUE);
- c.drawPoint(x+c.getWidth()-1, y, paint);
說明drawLine是沒有繪製到右邊最後一個點的
3.drawRect(Rect r, Paint paint)
當繪製空心矩形時,繪製的是一個左閉右閉的地區
驗證方法:
- rect.set(x, y, x+width, y+height);
- paint.setStyle(Style.STROKE);
- paint.setColor(Color.BLUE);
- c.drawRect(rect, paint);
- paint.setColor(Color.RED);
- c.drawLine(x, y, x+width, y, paint);
- c.drawLine(x, y+height, x+width, y+height, paint);
- c.drawLine(x, y, x, y+height, paint);
- c.drawLine(x+width, y, x+width, y+height, paint);
當繪製實心矩形時,繪製的是一個左閉右開的地區
驗證方法:
- rect.set(x, y, x+width, y+height);
- paint.setColor(Color.RED);
- c.drawLine(x, y, x+width, y, paint);
- c.drawLine(x, y+height, x+width, y+height, paint);
- c.drawLine(x, y, x, y+height, paint);
- c.drawLine(x+width, y, x+width, y+height, paint);
- paint.setStyle(Style.FILL);
- paint.setColor(Color.BLUE);
- 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繪圖的具體介紹。