View中的神笔:Paint
接下来就进入Android另一个开发方向,View界面,主要涉及到自定义View,是应用于用户交互的前沿。和自定义View相关的框架有两个:一大类是View(../frameworks/base/core/java/android/view/),另一大类是Graphics(../frameworks/base/graphics/)。自定义View最主要的就是使用系统提供的“笔”Paint在系统提供的“画纸”Canvas上绘制好看的界面(不包括动效)。
1、Paint类
了解Paint类基本知识点。
1.1、Paint简介
Paint类包含有关如何绘制几何图形,文本和位图的样式和颜色信息。详细方法查阅官方API文档: Docs > Reference">Android Developers > Docs > Reference
1.2、TextPaint
TextPaint是Paint的唯一子类,在Android框架层android.text包中,添加了几个下划线相关的方法
/**
* 自定义Paint定义下划线
* @hide
*/
@UnsupportedAppUsage
public void setUnderlineText(int color, float thickness) {
underlineColor = color;
underlineThickness = thickness;
}
/**
* 仅当设置了下划线颜色时,才返回自定义厚度。
* @hide
*/
@Override
public float getUnderlineThickness() {
if (underlineColor != 0) {
return underlineThickness;
} else {
return super.getUnderlineThickness();
}
}2、属性方法
介绍Paint类的常用属性和方法,至于开发中具体业务需求可以阅读源码和查阅API文档来获取灵感。
2.1、文字
①setUnderlineText(boolean underlineText):绘制标记UNDERLINE_TEXT_FLAG,将下划线装饰应用于绘制的文本。
②setTextSize(float textSize):设置Paint的文字大小,textSize值必须大于0。
③setTypeface(Typeface typeface):设置绘制文字字体
④setTextScaleX(float scaleX):设置文本的绘画水平缩放比例。 默认值为1.0。 值 > 1.0会使文本更宽。 值 < 1.0将使文本变窄。
⑤setTextSkewX(float skewX):设置文本的绘画水平偏斜系数。 默认值为0。对于近似倾斜文本,请使用-0.25左右的值。
⑥setTextAlign(Align align):设置绘制文本时的对齐方式
⑦setStrikeThruText(boolean strikeThruText):设置删除线,同STRIKE_THRU_TEXT_FLAG标志
/**
* Align指定drawText如何相对于[x,y]坐标对齐其文本。 默认值为LEFT。
*/
public enum Align {
/**
* The text is drawn to the right of the x,y origin
*/
LEFT (0),
/**
* The text is drawn centered horizontally on the x,y origin
*/
CENTER (1),
/**
* The text is drawn to the left of the x,y origin
*/
RIGHT (2);
}... ...
2.2、Color
①setColor(int color):设置Paint的颜色,颜色是一个包含Alpha透明度以及R,G,B的整数。
更多关于颜色Color值的内容,参考Color详解
2.3、Style
①setStyle(Style style):设置Paint的绘制填充样式(绘制Bitmap除外)
style指定要绘制的图元是FILL、STROKE、FILL_AND_STROKE。默认值为FILL,Style是定义在Paint类中的枚举类型。
public enum Style {
FILL (0),
STROKE (1),
FILL_AND_STROKE (2);
}2.4、Stroke
①setStrokeWidth(float width):设置描边宽度,仅当Style是STROKE或FILL_AND_STROKE有效。
②setStrokeCap(Cap cap):Cap指定描边线条和路径的开始和结束的处理方式。默认为BUTT。
/**
* Cap指定描边线条和路径的开始和结束的处理方式。 默认为BUTT。
*/
public enum Cap {
/**
* The stroke ends with the path, and does not project beyond it.
*/
BUTT (0),
/**
* The stroke projects out as a semicircle, with the center at the
* end of the path.
*/
ROUND (1),
/**
* The stroke projects out as a square, with the center at the end
* of the path.
*/
SQUARE (2);
}③setStrokeJoin(Join join):Join指定线和曲线段在描边路径上连接的处理。 默认值为MITER。
/**
* “连接”指定线和曲线段在描边路径上连接的处理。 默认值为MITER。
*/
public enum Join {
/**
* The outer edges of a join meet at a sharp angle
*/
MITER (0),
/**
* The outer edges of a join meet in a circular arc.
*/
ROUND (1),
/**
* The outer edges of a join meet with a straight line
*/
BEVEL (2);
}④setStrokeMiter(float miter):设置绘画的Paint斜角值。 当连接角度很锐利时,此参数用于控制斜接连接的行为。 该值必须≥0。
2.5、Filter过滤
①setColorFilter(ColorFilter filter):ColorFilter、ColorMatrixColorFilter、LightingColorFilter、PorterDuffColorFilter控制RGBA 的数值实现滤镜的效果。
②setMaskFilter(MaskFilter maskfilter):MaskFilter、BlurMaskFilter、EmbossMaskFilter实现模糊等遮罩效果。
③setFilterBitmap(boolean filter):设置Paint标志FILTER_BITMAP_FLAG,可在缩放的位图上进行双线性采样。
2.6、其它
①reset():将绘画恢复为其默认设置
②setAntiAlias(boolean aa):设置抗锯齿可以平滑绘制的内容的边缘,但对形状的内部没有影响。同ANTI_ALIAS_FLAG标志位。
③setShadowLayer(float radius, float dx, float dy,int shadowColor):在主层下方绘制一个阴影层,并具有指定的偏移量和颜色以及高斯模糊半径。 如果radius为0,则删除阴影层。比如绘制文字阴影,见《最最最常用的控件:TextView》,TextView控件有如下属性可以使用
<!-- Place a blurred shadow of text underneath the text, drawn with the specified color.. -->
<attr name="shadowColor" format="color" />
<!-- Horizontal offset of the text shadow. -->
<attr name="shadowDx" format="float" />
<!-- Vertical offset of the text shadow. -->
<attr name="shadowDy" format="float" />
<!-- Blur radius of the text shadow. -->
<attr name="shadowRadius" format="float" />④clearShadowLayer():清除阴影层。其实就是设置radius为0即可
public void clearShadowLayer() {
setShadowLayer(0, 0, 0, 0);
}⑤Shader setShader(Shader shader):给画笔设置“着色器”
相关博客:

