Android 2D绘图(Canvas+paint)的理解
在2D绘制中常用的两个类:
Canvas(画布):通过Canvas设置绘制的形状和路径
Paint(画笔):通过Paint设置颜色、阴影、透明度等等
类构造方法:
(1)Paint:
Android API Paint 使用方法总结:https://blog.csdn.net/shell812/article/details/49781397?ref=myread
文本设置相关方法:
isUnderlineText() //判断是否有下划线
setUnderlineText(boolean underlineText) //设置下划线
setLinearText(boolean linearText) //设置线性文本
getLetterSpacing() //获取字符间的间距
setLetterSpacing(float letterSpacing) //设置字符间距
getFontSpacing() //获取行间距
isStrikeThruText() //判断文本是否有删除线
setStrikeThruText(boolean strikeThruText) //设置文本删除线
getTextSize() //获取字体大小
setTextSize(float textSize) //设置字体大小
setFakeBoldText(boolean fakeBoldText) //设置伪粗体文本,设置在小字体上效果会非常差
getTextSkewX() //获取斜体文字的值
setTextSkewX(float skewX) //设置斜体文字的值,负数为右倾斜,正数为左倾斜 官方推荐-0.25
getTextScaleX() //获取文字水平缩放值
setTextScaleX(float scaleX) //设置文本水平缩放值 1.0为原始值
getTextAlign() //获取文本对其方式
setTextAlign(Paint.Align align) //设置文本对其方式 参数:Paint.Align.center,Paint.Align.left,Paint.Align.right
ascent() //baseline之上至字符最高处的距离
descent() //baseline下面到字符最低处的距离
measureText(CharSequence text, int start, int end) //测绘文本的宽度
getTextBounds(char[] text, int index, int count, Rect bounds) //获取文本宽高
getTextWidths(String text, int start, int end, float[] widths) //精确获取文本宽度
getTextLocale() //获取文本语言地理位置
setTextLocale(Locale locale) //设置文本地理位置,也就是设置对应的语言
setSubpixelText(boolean subpixelText) 参数:true或者false 设置为true,将有助于文本在LCD屏幕上的显示效果getTypeface() //获取文字字体类型
setTypeface(Typeface typeface) ///设置字体风格,包括粗体,斜体以及衬线体,非衬线体等
常用的字体类型名称: Typeface.DEFAULT //常规字体类型 Typeface.DEFAULT_BOLD //黑体字体类型 Typeface.MONOSPACE //等宽字体类型 Typeface.SANS_SERIF //sans serif字体类型 常用的字体风格名称: Typeface.BOLD //粗体 Typeface.BOLD_ITALIC //粗斜体 Typeface.ITALIC //斜体 Typeface.NORMAL //常规
控件的字体设置的两种方式
1.在xml中设置。
使用android:typeFace来设置:
Android:typeface=”sans”
2.在Java程序中:
eg1:Typeface font = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD); p.setTypeface( font );
eg2:
String familyName = “宋体”; Typeface font = Typeface.create(familyName,Typeface.BOLD); p.setTypeface(font);
绘图设置相关方法:
//设置画笔颜色
setARGB(int a, int r, int g, int b) //设置透明度和颜色 a:透明度,取值范围为0~255,数值越小越透明。 r:红色的颜色值,取值范围为0~255。 g:绿色的颜色值,取值范围为0~255。setAlpha(int a) //设置透明度0~255,要在setColor后面设置才生效 常用于一些图片重叠或者特效显示的场合 b:蓝色的颜色值,取值范围为0~255。
setColor(int color)
eg:setColor(getResources().getColor(R.color.colorAccent))
R.color:
colorPrimary – 应用程序工具条的颜色。 colorPrimaryDark – 状态条的颜色,它是深色的colorPrimary 。 colorAccent – 控件颜色,例如复选框、单选按钮、文本框等。 windowBackground – 屏幕背景色。 textColorPrimary – 应用程序的文本颜色。 statusBarColor – 状态条颜色。 navigationBarColor – 导航条颜色。
//获取画笔颜色
getAlpha()
getColor()
isAntiAlias() //判断是否抗锯齿
setAntiAlias(boolean aa) //设置抗锯齿,虽然耗资源耗时间,但是一般会开启 (边缘柔化处理) setDither(boolean dither) //设定是否使用图像抖动处理,会使绘制出来的图片颜色更加平滑和饱满,图像更加清晰
setFilterBitmap(boolean filter) //如果该项设置为true,则图像在动画进行中会滤掉对Bitmap图像的优化操作来加快显示速度,本设置项依赖于dither和xfermode的设置
getStyle() //获取画笔样式
setStyle(Paint.Style style) //设置画笔样式 实心:Paint.Style.FILL 空心:Paint.Style.STROKE 同时实心和空心 : Paint.Style.FILL_OR_STROKE
setStrokeWidth(float width) //当画笔样式为STROKE或FILL_OR_STROKE时,设置笔刷的粗细度
setStrokeMiter(