Surface就像是一塊潔白的畫板编矾,這在前面有介紹到攀涵,那如果想要在這塊畫板上畫畫,我們就需要用到畫筆和顏料洽沟,在這里給隆重介紹一下這兩位成員:Canvas和Paint以故。
Android的Canvas和Java的Canvas不一樣的,這里需要注意一下
從官方文檔介紹看裆操,Canvas繼承自O(shè)bject對象
The Canvas class holds the "draw" calls. To draw something, you need 4 basic components: A Bitmap to hold the pixels, a Canvas to host the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect, Path, text, Bitmap), and a paint (to describe the colors and styles for the drawing).
Canvas擁有draw的調(diào)用怒详,為了繪制一些東西,你需要4個基本的組件:一個Bitmap用于擱置像素踪区,一個Canvas用于繪制(往Bitmap中畫東西)昆烁,一原始元素(矩形,路徑缎岗,文本静尼,Bitmap),一個Paint(描述顏色和風(fēng)格)
而怎么利用Canvas在屏幕上畫畫呢传泊,Canvas有很多draw開頭的方法如:drawRect鼠渺,drawCircle,drawPath眷细,drawBitmap都是繪制基本元素的拦盹,傳入的參數(shù)一般都是位置 + Paint。
我們以drawRect為例:
形參:float,float,float,float,Paint溪椎;
分別代表矩形的左邊普舆,上邊,右邊校读,下邊的位置和矩形的一些特征沼侣,位置好理解,那么Paint是什么呢歉秫?
The Paint class holds the style and color information about how to draw geometries, text and bitmaps.
Paint對象擁有待繪制的幾何圖形蛾洛,文本和位圖的樣式和顏色信息。
也就是說Paint是涂料端考,決定了繪制出來的效果雅潭。