当前位置:   article > 正文

Android自定义View之Paint(二)

android 自定义view 将paint隐藏

上一章节学习了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的影响

  1. //测试breakText()
  2. mPaint.setTextSize(50);
  3. float[] value = new float[1];
  4. int ret = mPaint.breakText(STR, true, 200, value);
  5. Toast.makeText(getContext(),"breakText="+ret+", STR="+STR.length()+", value="+value[0],Toast.LENGTH_LONG).show();
  6. //textSize = 50;maxWidth = 200;breakText=5, STR=8, value=195.0
  7. //textSize = 100;maxWidth = 200;breakText=2, STR=8, value=200.0
  8. //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:偏移量

  1. mPaint.setColor(Color.YELLOW);
  2. mPaint.setShadowLayer(10,5,5,Color.BLACK);
  3. 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的矩阵):

  1. ColorMatrix colorMatrix = new ColorMatrix(new float[]{
  2. 0, 0, 0, 0, 0, // 红色向量
  3. 0, 1, 0, 0, 0, // 绿色向量
  4. 0, 0, 0, 0, 0, // 蓝色向量
  5. 0, 0, 0, 255, 0, // 透明度向量
  6. });
  7. mPaint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
  8. // 设置画笔颜色为自定义颜色
  9. mPaint.setColor(Color.argb(255, 255, 255, 255));
  10. canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint);

以上代码效果等同于

  1. mPaint.setColor(Color.argb(255, 0, 255, 0));
  2. 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. 运行结果同(1
  2. 3)PorterDuffColorFilter(int srcColor, PorterDuff.Mode mode) :
  3. 画布上的元素和我们设置的color进行混合,mode为混合类型
  4. mPaint.setColor(Color.argb(255, 0, 255, 0));
  5. mPaint.setColorFilter(new PorterDuffColorFilter(Color.BLUE, PorterDuff.Mode.ADD));
  6. 运行结果同(1
  7. PorterDuff.Mode mode详解见:https://my.oschina.net/u/2483853/blog/843023
  8. **boolean getFillPath(Path src, Path dst) **
  9. 返回路径是否被填充;处理src,将处理存放在dst里(没有找到使用的demo)
  10. **int getFlags() **
  11. **void setFlags(int flags)**
  12. 获取(设置)paint的flag属性,属性值如下:
  13. ![输入图片说明](https://static.oschina.net/uploads/img/201702/27093939_w2bt.png "在这里输入图片标题")
  14. **float getFontMetrics(Paint.FontMetrics metrics) **
  15. **Paint.FontMetrics getFontMetrics() **
  16. **Paint.FontMetricsInt getFontMetricsInt() **
  17. ** int getFontMetricsInt(Paint.FontMetricsInt fmi) **
  18. getFontMetrics()返回FontMetrics对象;getFontMetrics(Paint.FontMetrics metrics)返回文本的行间距,metrics的值不为空则返回FontMetrics对象的值;getFontMetricsInt()返回FontMetricsInt对象,FontMetricsInt和FontMetrics对象一样,只不过FontMetricsInt返回的是int而FontMetrics返回的是float。FontMetrics与FontMetricsInt都有top、ascent、descent、bottomleading这几个属性
  19. Paint.FontMetricsInt和Paint.FontMetricsInt 详解见:https://my.oschina.net/u/2483853/blog/843023
  20. **float getFontSpacing() **
  21. 获取字符行间距
  22. **int getHinting() **
  23. **void setHinting(int mode) **
  24. 画笔的隐藏模式。可以是 HINTING_OFF or HINTING_ON之一
  25. **MaskFilter getMaskFilter() **
  26. **MaskFilter setMaskFilter(MaskFilter maskfilter) **
  27. 设置滤镜 详见:https://my.oschina.net/u/2483853/blog/848734
  28. PathEffect getPathEffect()
  29. PathEffect setPathEffect(PathEffect effect)
  30. Rasterizer getRasterizer()
  31. Rasterizer setRasterizer(Rasterizer rasterizer)
  32. Shader getShader()
  33. Shader setShader(Shader shader)
  34. Paint.Cap getStrokeCap()
  35. void setStrokeCap(Paint.Cap cap)
  36. Paint.Join getStrokeJoin()
  37. void setStrokeJoin(Paint.Join join)
  38. float getStrokeMiter()
  39. void setStrokeMiter(float miter)
  40. float getStrokeWidth()
  41. void setStrokeWidth(float width)
  42. Paint.Style getStyle()
  43. void setStyle(Paint.Style style)
  44. Paint.Align getTextAlign()
  45. void setTextAlign(Paint.Align align)
  46. void getTextBounds(char[] text, int index, int count, Rect bounds)
  47. void getTextBounds(String text, int start, int end, Rect bounds)
  48. Locale getTextLocale()
  49. void setTextLocale(Locale locale)
  50. void getTextPath(String text, int start, int end, float x, float y, Path path)
  51. void getTextPath(char[] text, int index, int count, float x, float y, Path path)
  52. float getTextScaleX()
  53. void setTextScaleX(float scaleX)
  54. float getTextSize()
  55. void setTextSize(float textSize)
  56. float getTextSkewX()
  57. void setTextSkewX(float skewX)
  58. int getTextWidths(String text, float[] widths)
  59. int getTextWidths(CharSequence text, int start, int end, float[] widths)
  60. int getTextWidths(String text, int start, int end, float[] widths)
  61. int getTextWidths(char[] text, int index, int count, float[] widths)
  62. Typeface getTypeface()
  63. Typeface setTypeface(Typeface typeface)
  64. Xfermode getXfermode()
  65. Xfermode setXfermode(Xfermode xfermode)
  66. final boolean isAntiAlias()
  67. void setAntiAlias(boolean aa)
  68. final boolean isDither()
  69. void setDither(boolean dither)
  70. final boolean isFakeBoldText()
  71. void setFakeBoldText(boolean fakeBoldText)
  72. final boolean isFilterBitmap()
  73. void setFilterBitmap(boolean filter)
  74. final boolean isLinearText()
  75. void setLinearText(boolean linearText)
  76. final boolean isStrikeThruText()
  77. void setStrikeThruText(boolean strikeThruText)
  78. final boolean isSubpixelText()
  79. void setSubpixelText(boolean subpixelText)
  80. final boolean isUnderlineText()
  81. void setUnderlineText(boolean underlineText)
  82. void reset()
  83. void set(Paint src)
  84. void setARGB(int a, int r, int g, int b)

转载于:https://my.oschina.net/u/2483853/blog/848771

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/765514
推荐阅读
相关标签
  

闽ICP备14008679号