Andorid自定义动画 文字与画布(一)

  • Paint设置
//设置画笔宽度
paint.setStrokeWidth(5);
//指定抗锯齿功能
paint.setAntiAlias(true);
//绘图样式
paint.setStyle(Paint.Style.FILL);
//设置文字对齐方式
paint.setTextAlign(Align.CENTER);
//设置文字大小
paint.setTextSize(12);
//粗体文字
paint.setFakeBoldText(true);
//设置下划线
paint.setUnderlineText(true);
//设置字体水平倾斜度
paint.setTextSkewX((float)-0.25);
//设置带有删除线效果
paint.setStrikeThruText(true);
//水平方向拉伸
paint.setTextScaleX(2);

  • Canvas文字 :
//普通绘制,指定绘制起点(x,y)
void drawText(String text, float x, flozt y,Paint paint)

//通过指定字符串的起始位置和终止位置进行绘制,他匀速我们指定CharSequence或者String类型字符串进行绘制, start表示字符串开始的下标,end表示终止下标,x,y表示起始点坐标
void drawText(CharSequence text, int start, int end, float x, float y, Paint paint)
void drawText(String text, int start, int end, dfloat x, float  y, Paint paint)
  • 逐个指定文字位置:
void drawPosText(String text, float[] pos, Paint paint)
void drawPosText(char[] text, int index, int count, float[] pos, Paint paint)


@Override
protected void onDraw(Canvas  canvas){    
    Paint paint = new Paint();    
    paint.setColor(Color.BLUE);    
    paint.setTextSize(80);    
    float[]pos = new float[]{80,100,    
                                    80,200,    
                                    80,300,    
                                    80,400,            
                                    80,500};    //截取部分左闭右开    
    canvas.drawPosText("王拣贤佩奇",pos,  paint);    
}
  • 沿路径绘制:
void drawTextonPath(String text,Path path, float vOffset, Paint paint)
void drawTextOnPath(char[] text, int index, int count, Path path, float hOffset, float vOffset, Paint paint)

* float hOffset:与路径起始点的水平偏移量
* float vOffset:与路径中心的垂直偏移量

Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setTextSize(45);
paint.setStrokeWidth(5);
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 = "王拣贤佩奇王拣贤佩奇王拣贤佩奇";
canvas.drawTextOnPath(string, circlePath, 0,0,paint);
canvas.drawTextOnPath(string, circlePath2, 80, 30,paint);
  • 设置字体样式:
Typeface setTypeface(Typeface typeface)
Typeface是专门用来设置字体样式的类,通过paint.setTypeface()函数来指定即将绘制的文字的字体样式。可以指定系统中的字体样式,也可以在自定义的样式文件中获取,在构建Typeface类时,可以指定所用样式的正常体,斜体,粗体等,如果载指定样式中没有相关文字的样式,就会用默认的样式来显示,一般默认为宋体。

Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setTypeface(Typeface.SERIF);
paint.setTextSize(50);
  • defaultFromStyle()函数:
Typeface defaultFromStyle(int style);
int style取值如下 :
Typeface.NORMAL //正常字体
Typeface.BOLD     //粗体 
Typeface.ITALIC   //斜体
Typeface.BOLD_ITALIC //粗斜体

Paint paint = new Paint();
Typeface typeface = Typeface.defaultFromStyle(Typeface.BOLD_ITALIC);
paint.setColor(Color.BLUE);
paint.setTypeface(typeface);
paint.setTextSize(50);
  • create(String familyName, int style)
Typeface create(String familyName, int style)
该函数直接指定字体名来加载系统自带的字体样式,如果字体样式不存在,则会用系统样式来代替并返回。


Paint paint = new Paint();paint.setColor(Color.BLUE);
paint.setTextSize(50);
String string = "宋体";
Typeface font = Typeface.create(string, Typeface.NORMAL);
paint.setTypeface(font);
canvas.drawText("王拣贤佩奇",10, 100, paint);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wjxbless

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值