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

weixin_34208283發表於2017-12-20
起始位在 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) 
                精確計算文字寬度,與上面兩個類似。

相關文章