Android繪圖具體應(yīng)用方式總結(jié)
在Android操作系統(tǒng)中,有很多功能技巧可以幫助我們輕松的實(shí)現(xiàn)一些需求。比如對圖像圖像的處理等等。我們在這里就會為大家?guī)硪恍┯嘘P(guān)Android繪圖的方法,希望能是朋友們充分掌握這方面的應(yīng)用。#t#
繪制各種圖形、文字使用Canvas類中drawRect、drawText等方法,詳細(xì)函數(shù)列表以及參數(shù)說明可以查看sdk
圖形的樣式由paint參數(shù)控制
Paint類也有很多參數(shù)設(shè)置方法
坐標(biāo)由Rect和RectF類管理
通過Canvas、Paint和Rect 就可以繪制游戲中需要的大多數(shù)基本圖形了
Android繪圖中需要注意的一些細(xì)節(jié)
繪制實(shí)心矩形,需要設(shè)置paint屬性:paint.setStyle(Style.FILL); 通過Style的幾個枚舉值改變繪制樣式
以下寫的有點(diǎn)亂,隨時(shí)添加一些記錄點(diǎn), 以后再整理啦~~~~~
1. Rect對象
一個區(qū)域?qū)ο驲ect(left, top, right, bottom) , 是一個左閉右開的區(qū)域,即是說使用 Rect.contains(left, top)為true, Rect.contains(right, bottom)為false
2.drawLine方法
drawLine(float startX, float startY, float stopX, float stopY, Paint paint) 也是一個左閉右開的區(qū)間,只會繪制到stopX-1,stopY-1
驗(yàn)證方法:
- 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是沒有繪制到右邊最后一個點(diǎn)的
3.drawRect(Rect r, Paint paint)
當(dāng)繪制空心矩形時(shí),繪制的是一個左閉右閉的區(qū)域
驗(yàn)證方法:
- 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);
當(dāng)繪制實(shí)心矩形時(shí),繪制的是一個左閉右開的區(qū)域
驗(yàn)證方法:
- 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);
這個規(guī)則跟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)繪制,結(jié)果是一個2px高的矩形,用fillRect(10,10,100,1),結(jié)果是一個1px高的矩形
以上就是對Android繪圖的具體介紹。