自定义控件之文字绘制
paint与文字设置相关的方法如下
paint.setStrokeCap(Paint.Cap.BUTT);//画笔线帽样式 BUTT无线帽 ROUND圆形线帽 SOUARE 方形线帽
paint.setStrokeWidth(5);//画笔宽度 paint.setColor(Color.RED);//设置颜色 paint.setAntiAlias(true);//抗锯齿功能 paint.setStyle(Paint.Style.FILL);//样式 paint.setTextAlign(Paint.Align.CENTER);//文字对齐方式 paint.setTextSize(50);//设置文字大小 paint.setFakeBoldText(true);//设置是否为粗体 paint.setUnderlineText(true);//设置下画线 paint.setTextSkewX(-0.25f);//字体倾斜度,一般设置为-0.25 paint.setStrikeThruText(true);//设置带有删除线效果 paint.setTextScaleX(2);//水平拉伸,高度不会变
示例代码
/** * 填充样式的区别 * @param canvas */ private void setStyle(Canvas canvas){ Paint paint=new Paint(); paint.setColor(Color.RED); //设置画笔颜色 paint.setStrokeWidth (5);//设置画笔宽度 paint.setAntiAlias(true); //指定是否使用抗锯齿功能,如果使用,会使绘图速度变慢 paint.setTextSize(80);//设置文字大小 //绘图样式,设置为填充 paint.setStyle(Paint.Style.FILL); canvas.drawText("床前明月光", 10,100, paint); } /** * setTextAlign * @param canvas */ private void setAlign(Canvas canvas){ Paint paint=new Paint(); paint.setColor(Color.RED); paint.setStrokeWidth (5); paint.setAntiAlias(true); paint.setTextSize(80); paint.setTextAlign(Paint.Align.RIGHT); canvas.drawText("床前明月光", 400,100, paint); } /** * 设置字体样式_常规设置 * @param canvas */ private void setTextStyle(Canvas canvas){ Paint paint=new Paint(); paint.setColor(Color.RED); paint.setTextSize(80); canvas.drawText("床前明月光", 10,100, paint); paint.setFakeBoldText(true);//设置是否为粗体文字 paint.setUnderlineText(true);//设置下划线 paint.setStrikeThruText(true);//设置带有删除线效果 canvas.drawText("床前明月光", 10,250, paint); } /** * 设置字体样式_字体倾斜度设置 * @param canvas */ private void setTextSkewX(Canvas canvas){ Paint paint=new Paint(); paint.setColor(Color.RED); paint.setTextSize(80); //正常样式 canvas.drawText("床前明月光", 10,100, paint); //向右倾斜 paint.setTextSkewX(-0.25f); canvas.drawText("床前明月光", 10,200, paint); //向左倾斜 paint.setTextSkewX(0.25f); canvas.drawText("床前明月光", 10,300, paint); } /** * 水平拉伸 * @param canvas */ private void setTextScaleX(Canvas canvas){ Paint paint=new Paint(); paint.setColor(Color.RED); paint.setTextSize(80); //正常样式 canvas.drawText("床前明月光", 10,100, paint); //拉伸两倍 paint.setTextScaleX(2); canvas.drawText("床前明月光", 10,200, paint); }
其他方法绘制
/** * 普通绘制 * * @param canvas */ private void drawSubText(Canvas canvas) { Paint paint = new Paint(); paint.setColor(Color.RED); paint.setTextSize(80); canvas.drawText("床前明月光", 2, 4, 10, 100, paint); } /** * 逐个指定文字位置 */ private void drawPosText(Canvas canvas) { Paint paint = new Paint(); paint.setColor(Color.RED); paint.setTextSize(80); float[] pos = new float[]{80, 100, 80, 200, 80, 300, 80, 400}; canvas.drawPosText("床前明月", pos, paint); //pos绘制每个文字的具体位置,两两一组 } /** * 沿路径绘制 * @param canvas */ private void drawPathText(Canvas canvas) { Paint paint = new Paint(); paint.setColor(Color.RED); paint.setStrokeWidth(5); paint.setTextSize(45); paint.setStyle(Paint.Style.STROKE); //先创建两个相同的圆形路径,并先画出两个路径原图 Path circlePath = new Path(); circlePath.addCircle(220, 300, 150, Path.Direction.CCW);//逆向绘制 canvas.drawPath(circlePath, paint);//绘制出路径原形 Path circlePath2 = new Path(); circlePath2.addCircle(600, 300, 150, Path.Direction.CCW); canvas.drawPath(circlePath2, paint);//绘制出路径原形 String string = "床前明月光,疑是地上霜"; paint.setColor(Color.GREEN); //hoffset、voffset参数值全部设为0,看原始状态是怎样的 canvas.drawTextOnPath(string, circlePath, 0, 0, paint); //第二个路径,改变hoffset、voffset参数值 canvas.drawTextOnPath(string, circlePath2, 80, 30, paint); } /** * 自字义字体 * @param canvas */ private void drawCustomFont(Canvas canvas){ //自定义字体,,,迷你简罗卜 Paint paint=new Paint(); paint.setColor(Color.RED); paint.setTextSize(60); AssetManager mgr= mContext.getAssets();//得到AssetManager //根据路径得到Typeface Typeface typeface=Typeface.createFromAsset(mgr, "fonts/jian_luobo.ttf"); paint.setTypeface(typeface);//设置字体样式 canvas.drawText("床前明月光,疑是地上霜",10,550, paint); }
defaultFromStyle函数
public static Typeface defaultFromStyle(int style)
该函数会指定字体样式获取对于的默认字体,取值如下
BOLD 粗体
BOLD_ITALIC粗斜体
ITALIC斜体
NORMAL 正常字体 默认
示例
Typeface typeface = Typeface.defaultFromStyle(Typeface.BOLD);
paint.setTypeface(typeface);
Typeface 中create函数
该函数直接通过指定字体名来加载系统中自带的字体样式,如果字体不存在,则使用系统样式代替返回
示例:
Typeface typeface = Typeface.create("宋体",Typeface.NORMAL);
paint.setTypeface(typeface);
最后,关注【码上加油站】微信公众号后,有疑惑有问题想加油的小伙伴可以码上加入社群,让我们一起码上加油吧!!!