UI繪制流程 Draw Paint基本屬性(四)

起始位在 ViewRootImpl類中 performTraversals方法 在按順序 測量 布局 后執行
performDraw()> draw()>drawSoftware()> (View)mView.draw(canvas) 在View draw中完成繪制 這里ViewGroup 沒有重寫draw 所以最終 都是調用View draw方法
View draw(Canvas canvas) 的依次執行過程為:
  • 1.繪制背景
  • 2.如有必要,保存畫布的圖層以準備褪色
  • 3.繪制視圖的內容 onDraw(canvas)
  • 4.畫孩子
  • 5.如有必要,繪制漸變邊緣并恢復圖層
  • 6.繪制裝飾(例如滾動條)
在個圖中View都是完成onDraw(canvas) 來達到各種效果

要完成繪制分兩個步驟

  • 1.創建設置畫筆paint
  • 2.canvas進行畫

Paint 基本使用《Paint的方法主要可以抽象成兩大類》:

1. 負責設置獲取圖形繪制、路徑相關的

setStyle(Paint.Style style) 設置畫筆樣式 取值有
  • Paint.Style.FILL :填充內部
  • Paint.Style.STROKE :僅描邊 畫線段
  • Paint.Style.FILL_AND_STROKE :填充內部和描邊
setStrokeWidth(float width) 設置畫筆寬度
setAntiAlias(boolean aa) 設置畫筆是否抗鋸齒 轉角平滑 資源消耗大
setStrokeCap(Paint.Cap cap)設置線冒樣式
setStrokeJoin(Paint.Join join)設置線段連接處樣式
  • Join.MITER(結合處為銳角)
  • Join.Round(結合處為圓弧)
  • Join.BEVEL(結合處為直線)
setStrokeMiter(float miter) 設置筆畫的傾斜度
reset() 清空畫筆復位 恢復到默認設置
set(Paint src) 設置一個外來Paint畫筆。相當于克隆
setARGB(int a, int r, int g, int b) ,getAlpha() ,setAlpha(int a) ,getColor(),setColor(int color) 獲取與設置alpha值、顏色、ARGB等
setDither(boolean dither) 設定圖片是否使用抖動 色階分界線更自然
setPathEffect(PathEffect effect)線段夾角轉彎半徑 或虛線 設置路徑各種效果
  • CornerPathEffect——圓形拐角效果paint.setPathEffect(new CornerPathEffect(100));
  • DashPathEffect( float[],int)——虛線效果 float[] 虛線樣式
setXfermode(Xfermode xfermode)設置圖形重疊時的處理方式,如合并,取交集或并集,經常用來制作橡皮的擦除效果
setMaskFilter(MaskFilter maskfilter)設置MaskFilter,可以用不同的MaskFilter實現濾鏡的效果,如濾化,立體等
setColorFilter(ColorFilter colorfilter) 設置顏色過濾器,可以在繪制顏色時實現不用顏色的變換效果
setShader(Shader shader)設置圖像效果,使用Shader可以繪制出各種漸變效果
setShadowLayer(float radius ,float dx,float dy,int color)在圖形下面設置陰影層,產生陰影效果,radius為陰影的角度,dx和dy為陰影在x軸和y軸上的距離,color為陰影的顏色

2. 負責設置獲取文字相關的

getFontSpacing() 獲取字符行間距。
getLetterSpacing() setLetterSpacing(float letterSpacing) 設置和獲取字符間距
isUnderlineText() setUnderlineText(boolean underlineText) 是否有下劃線和設置下劃線。
isStrikeThruText() setStrikeThruText(boolean strikeThruText) 獲取與設置是否有文本刪除線。
getTextSize() setTextSize(float textSize) 獲取與設置文字大小,注意:Paint.setTextSize傳入的單位是px,TextView.setTextSize傳入的單位是sp,注意使用時不同分辨率處理問題。
getTypeface() setTypeface(Typeface typeface) 獲取與設置字體類型。Android默認有四種字體樣式:BOLD(加粗)、BOLD_ITALIC(加粗并傾斜)、ITALIC(傾斜)、NORMAL(正常),我們也可以通過Typeface類來自定義個性化字體。
getTextSkewX() setTextSkewX(float skewX)獲取與設置文字傾斜,參數沒有具體范圍,官方推薦值為-0.25,值為負則右傾,為正則左傾,默認值為0。
getTextAlign() setTextAlign(Paint.Align align)獲取與設置文本對齊方式,取值為CENTER、LEFT、RIGHT,也就是文字繪制是左邊對齊、右邊還是局中的。
breakText(String text, boolean measureForwards, float maxWidth, float[] measuredWidth) 獲取一行放幾個比如文本閱讀器的翻頁效果,我們需要在翻頁的時候動態折斷或生成一行字符串,這就派上用場了~
    計算指定參數長度能顯示多少個字符,同時可以獲取指定參數下可顯示字符的真實長度,譬如:
                
                private static final String STR = "你好!世界";
                mPaint.setTextSize(50);
                float[] value = new float[1];
                int ret = mPaint.breakText(STR, true, 200, value);
                Log.i("YYYY", "breakText="+ret+", STR="+STR.length()+", value="+value[1]);
                //breakText=5, STR=8, value=195.0

                void getTextBounds(char[] text, int index, int count, Rect bounds) 
                void getTextBounds(String text, int start, int end, Rect bounds) 
                獲取文本的寬高,通過bounds的Rect拿到整型。

                float measureText(String text) 
                float measureText(CharSequence text, int start, int end) 
                float measureText(String text, int start, int end) 
                float measureText(char[] text, int index, int count) 
                粗略獲取文本的寬度,和上面的getTextBounds比較類似,返回浮點數。

                int getTextWidths(String text, int start, int end, float[] widths) 
                int getTextWidths(String text, float[] widths) 
                int getTextWidths(CharSequence text, int start, int end, float[] widths) 
                int getTextWidths(char[] text, int index, int count, float[] widths) 
                精確計算文字寬度,與上面兩個類似。
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。