您好,登錄后才能下訂單哦!
這篇文章主要介紹Androi原生繪圖工具Paint怎么用,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
前言:
生活中繪制圖案需要紙和筆,android
中提供了類似的工具Canvas
和Paint
,分別對應(yīng)畫布和畫筆。
The Paint class holds the style and color information about how to draw geometries, text and bitmaps.
Paint
擁有繪制幾何圖形、文本、圖片的樣式和顏色信息。
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
擁有繪圖的請求權(quán),想要完成繪制,需要4個基本元素:保存像素的Bitmap
、一個可以調(diào)用draw
方法的canvas
、要繪制的內(nèi)容描述、一個畫筆。憑借這4個元素就可以繪制到一個bitmap
上,然后渲染到屏幕上。
Paint提供了三種初始化方法:
Paint() Paint(int flag) Paint(Paint paint)
setAlpha(int a) 設(shè)置范圍0~255
填充樣式表示繪線和填充的選擇:
Paint.Style.FILL Paint.Style.FILL_AND_STROKE Paint.Style.STROKE
setColor
,setARGB
設(shè)置顏色,設(shè)置畫筆的顏色,setARGB()
參數(shù)范圍0-255。setAntiAlias(boolean)
抗鋸齒,使邊界更順滑(有些屏幕分辨率不高,導(dǎo)致像素點比較大,繪制邊界可能會有顆粒感,打開抗鋸齒邊界顆粒感會減少)。setTextSize(float textSize)
字體大小,單位是px,如果是dp要注意轉(zhuǎn)換。setTextAlign(Paint.Align.RIGHT)
設(shè)置字體對齊方式,根據(jù)下面的實例可以,對齊方式基于開始繪制的點。
mTextPaint.setTextAlign(Paint.Align.LEFT); canvas.drawText("MatumbaMan的博客",200, 200, mTextPaint); mTextPaint.setTextAlign(Paint.Align.RIGHT); canvas.drawText("MatumbaMan的博客",200, 300, mTextPaint); mTextPaint.setTextAlign(Paint.Align.CENTER); canvas.drawText("MatumbaMan的博客",200, 400, mTextPaint);
setShadowLayer(float radius, float dx, float dy, int shadowColor) 設(shè)置陰影
Radius
設(shè)置角度,dx,dy控制字體的上下左右出現(xiàn),有正負之分,dx的正負代表右左,dy的正負代表下上。
mTextPaint.setTextAlign(Paint.Align.LEFT); mTextPaint.setShadowLayer(10, -20, 40, Color.GRAY); canvas.drawText("MatumbaMan的博客",200, 200, mTextPaint); mTextPaint.setTextAlign(Paint.Align.RIGHT); canvas.drawText("MatumbaMan的博客",200, 300, mTextPaint); mTextPaint.setTextAlign(Paint.Align.CENTER); canvas.drawText("MatumbaMan的博客",200, 400, mTextPaint);
setStrokeCap(Paint.Cap.ROUND)設(shè)置繪制起始點和結(jié)尾點的樣式,
三種樣式ROUND
,BUTT
,SQUARE
、
Cap.ROUND(圓形)
Cap.SQUARE(方形)
Paint.Cap.BUTT(無)
Path path = new Path(); path.moveTo(100, 100); path.lineTo(100, 200); path.lineTo(200, 300); canvas.drawPath(path, mPaint2);//無 path.reset(); path.moveTo(300, 100); path.lineTo(300, 200); path.lineTo(400, 300); mPaint2.setStrokeCap(Paint.Cap.ROUND);//圓 canvas.drawPath(path, mPaint2); path.reset(); path.moveTo(500, 100); path.lineTo(500, 200); path.lineTo(600, 300); mPaint2.setStrokeCap(Paint.Cap.SQUARE);//方 canvas.drawPath(path, mPaint2);
setStrokeJoin(Paint.Join join),設(shè)置繪制path連接點的樣式
Join.MITER(結(jié)合處為銳角)
Join.Round(結(jié)合處為圓弧)
Join.BEVEL(結(jié)合處為直線)
Path path = new Path(); path.moveTo(100, 100); path.lineTo(100, 200); path.lineTo(200, 300); mPaint2.setStrokeJoin(Paint.Join.MITER); canvas.drawPath(path, mPaint2); path.reset(); path.moveTo(300, 100); path.lineTo(300, 200); path.lineTo(400, 300); mPaint2.setStrokeJoin(Paint.Join.ROUND); canvas.drawPath(path, mPaint2); path.reset(); path.moveTo(500, 100); path.lineTo(500, 200); path.lineTo(600, 300); mPaint2.setStrokeJoin(Paint.Join.BEVEL); canvas.drawPath(path, mPaint2);
以上是“Androi原生繪圖工具Paint怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。