上一章节学习了Paint的几种flag,和内部类的作用和属性,这一章节开始学习Paint的方法
**float ascent() ** 返回基于当前字体和文字大小baseline到字符最高处的距离。
**float descent() ** 返回基于当前字体和文字大小baseline到字符最低处的距离
ascent() + descent() 可以看成文字的height
**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)
返回测量文本的width
**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属性,属性值如下:
-
- 
-
- **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)