Android繪圖具體應用方式總結
在Android操作系統中,有很多功能技巧可以幫助我們輕松的實現一些需求。比如對圖像圖像的處理等等。我們在這里就會為大家帶來一些有關Android繪圖的方法,希望能是朋友們充分掌握這方面的應用。#t#
繪制各種圖形、文字使用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繪圖的具體介紹。