上一章節學習了Paint的幾種flag,和內部類的做用和屬性,這一章節開始學習Paint的方法canvas
**float ascent() ** 返回基於當前字體和文字大小baseline到字符最高處的距離。函數
**float descent() ** 返回基於當前字體和文字大小baseline到字符最低處的距離學習
ascent() + descent() 能夠當作文字的height測試
**float measureText(String text) **字體
**float measureText(CharSequence text, int start, int end) **.net
**float measureText(String text, int start, int end) **code
float measureText(char[] text, int index, int count)對象
返回測量文本的widthblog
**int breakText(CharSequence text, int start, int end, boolean measureForwards, float maxWidth, float[] measuredWidth) **圖片
**int breakText(String text, boolean measureForwards, float maxWidth, float[] measuredWidth) **
**int breakText(char[] text, int index, int count, float maxWidth, float[] measuredWidth) **
在提取指定範圍內(小於maxWidth)的字符串,返回被測量字符串的數量,若是measuredWidth不爲null,將真實寬度存放在其中。
注意,這兩個值都會受textSize的影響
//測試breakText() mPaint.setTextSize(50); float[] value = new float[1]; int ret = mPaint.breakText(STR, true, 200, value); Toast.makeText(getContext(),"breakText="+ret+", STR="+STR.length()+", value="+value[0],Toast.LENGTH_LONG).show(); //textSize = 50;maxWidth = 200;breakText=5, STR=8, value=195.0 //textSize = 100;maxWidth = 200;breakText=2, STR=8, value=200.0 //textSize = 50;maxWidth =300;breakText=8, STR=8, value=293.0
**void clearShadowLayer() **
清除陰影層
**void setShadowLayer(float radius, float dx, float dy, int color) **
設置主層的陰影,當radius(半徑)爲0時,無陰影層。dx,dy:偏移量
mPaint.setColor(Color.YELLOW); mPaint.setShadowLayer(10,5,5,Color.BLACK); canvas.drawText(「3712」, getWidth() / 2 - mBound.width() / 2, getHeight() / 2 + mBound.height() / 2, mPaint);
result:
**int getAlpha() **
**void setAlpha(int a) **
設置透明度
** int getColor() **
**void setColor(int color) **
設置顏色
**ColorFilter getColorFilter() **
**ColorFilter setColorFilter(ColorFilter filter) **
使用該函數時能夠傳入ColorFilter的子類ColorMatrixColorFilter, LightingColorFilter, PorterDuffColorFilter 進行過濾
(1)ColorMatrixColorFilter(是一個4*5的矩陣):
ColorMatrix colorMatrix = new ColorMatrix(new float[]{ 0, 0, 0, 0, 0, // 紅色向量 0, 1, 0, 0, 0, // 綠色向量 0, 0, 0, 0, 0, // 藍色向量 0, 0, 0, 255, 0, // 透明度向量 }); mPaint.setColorFilter(new ColorMatrixColorFilter(colorMatrix)); // 設置畫筆顏色爲自定義顏色 mPaint.setColor(Color.argb(255, 255, 255, 255)); canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint);
以上代碼效果等同於
mPaint.setColor(Color.argb(255, 0, 255, 0)); canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint);
result:
(2)LightingColorFilter:LightingColorFilter(int mul, int add) ``` //透明度部分不受影響 int mul = 0xFF0000FF;//去掉red和green int add = 0xFF00FFFF;//添加blue(原來有的顏色不能去掉) mPaint.setColor(Color.argb(255, 255, 255, 255)); mPaint.setColorFilter(new LightingColorFilter(mul,add)); canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint);
運行結果同(1) (3)PorterDuffColorFilter(int srcColor, PorterDuff.Mode mode) : 畫布上的元素和咱們設置的color進行混合,mode爲混合類型 mPaint.setColor(Color.argb(255, 0, 255, 0)); mPaint.setColorFilter(new PorterDuffColorFilter(Color.BLUE, PorterDuff.Mode.ADD)); 運行結果同(1) PorterDuff.Mode mode詳解見:https://my.oschina.net/u/2483853/blog/843023 **boolean getFillPath(Path src, Path dst) ** 返回路徑是否被填充;處理src,將處理存放在dst裏(沒有找到使用的demo) **int getFlags() ** **void setFlags(int flags)** 獲取(設置)paint的flag屬性,屬性值以下: ![輸入圖片說明](https://static.oschina.net/uploads/img/201702/27093939_w2bt.png "在這裏輸入圖片標題") **float getFontMetrics(Paint.FontMetrics metrics) ** **Paint.FontMetrics getFontMetrics() ** **Paint.FontMetricsInt getFontMetricsInt() ** ** int getFontMetricsInt(Paint.FontMetricsInt fmi) ** getFontMetrics()返回FontMetrics對象;getFontMetrics(Paint.FontMetrics metrics)返回文本的行間距,metrics的值不爲空則返回FontMetrics對象的值;getFontMetricsInt()返回FontMetricsInt對象,FontMetricsInt和FontMetrics對象同樣,只不過FontMetricsInt返回的是int而FontMetrics返回的是float。FontMetrics與FontMetricsInt都有top、ascent、descent、bottom、leading這幾個屬性 Paint.FontMetricsInt和Paint.FontMetricsInt 詳解見:https://my.oschina.net/u/2483853/blog/843023 **float getFontSpacing() ** 獲取字符行間距 **int getHinting() ** **void setHinting(int mode) ** 畫筆的隱藏模式。能夠是 HINTING_OFF or HINTING_ON之一 **MaskFilter getMaskFilter() ** **MaskFilter setMaskFilter(MaskFilter maskfilter) ** 設置濾鏡 詳見:https://my.oschina.net/u/2483853/blog/848734 PathEffect getPathEffect() PathEffect setPathEffect(PathEffect effect) Rasterizer getRasterizer() Rasterizer setRasterizer(Rasterizer rasterizer) Shader getShader() Shader setShader(Shader shader) Paint.Cap getStrokeCap() void setStrokeCap(Paint.Cap cap) Paint.Join getStrokeJoin() void setStrokeJoin(Paint.Join join) float getStrokeMiter() void setStrokeMiter(float miter) float getStrokeWidth() void setStrokeWidth(float width) Paint.Style getStyle() void setStyle(Paint.Style style) Paint.Align getTextAlign() void setTextAlign(Paint.Align align) void getTextBounds(char[] text, int index, int count, Rect bounds) void getTextBounds(String text, int start, int end, Rect bounds) Locale getTextLocale() void setTextLocale(Locale locale) void getTextPath(String text, int start, int end, float x, float y, Path path) void getTextPath(char[] text, int index, int count, float x, float y, Path path) float getTextScaleX() void setTextScaleX(float scaleX) float getTextSize() void setTextSize(float textSize) float getTextSkewX() void setTextSkewX(float skewX) int getTextWidths(String text, float[] widths) int getTextWidths(CharSequence text, int start, int end, float[] widths) int getTextWidths(String text, int start, int end, float[] widths) int getTextWidths(char[] text, int index, int count, float[] widths) Typeface getTypeface() Typeface setTypeface(Typeface typeface) Xfermode getXfermode() Xfermode setXfermode(Xfermode xfermode) final boolean isAntiAlias() void setAntiAlias(boolean aa) final boolean isDither() void setDither(boolean dither) final boolean isFakeBoldText() void setFakeBoldText(boolean fakeBoldText) final boolean isFilterBitmap() void setFilterBitmap(boolean filter) final boolean isLinearText() void setLinearText(boolean linearText) final boolean isStrikeThruText() void setStrikeThruText(boolean strikeThruText) final boolean isSubpixelText() void setSubpixelText(boolean subpixelText) final boolean isUnderlineText() void setUnderlineText(boolean underlineText) void reset() void set(Paint src) void setARGB(int a, int r, int g, int b)