首页 > 其他分享 >Andorid自定义动画 文字与画布(一)

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

时间:2023-01-06 10:37:23浏览次数:37  
标签:float 自定义 paint int Typeface 画布 Paint 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);


标签:float,自定义,paint,int,Typeface,画布,Paint,Andorid,样式
From: https://blog.51cto.com/u_13987312/5992396

相关文章

  • Android文字与画布(二)
    画布:Canvas平移:translate()使用来实现画布平移的,向右是X轴正方向,向下是Y轴正方向,画布的原点是(0,0)平移后的画布的左上角是新的坐标原点voidtranslate(floatdx,floatdy)@Over......
  • Canvas与图层(三)恢复画布
    恢复画布有两个函数:restore()与rest无论哪种*恢复画布有两个函数:restore()与restongoCount()restore()就是把回退栈中的最上层画布状态出栈,恢复画布状态,​​具体使用​​r......
  • Canvas与图层(二)画布与图层
    图层(Layer):每次调用canvas.drawXXX系列函数,都会生成一个透明图层来绘制这个图形画布(Bitmap):每块画布都是一个Bitmap,所有的图像都是画在这个Bitmap上的,画布有两种:*一种是Vi......
  • python-自定义模块及导入方法
    1.自定义模块​ 一个较大的程序一般应分为若干个程序块,若个程序块称为模块,每个模块用来实现一部分特定的功能。​ 这样做的目的是为了将代码有组织的存放在一起,方便管......
  • 项目测试中自定义时间
    在项目中测试有时候会加速时间,如活动是七天的周期,测试肯定得加速这个工具类就是为了这种情况,设定多少分一天就可以达到。useCustomDateTime在上线就改为false:使用Loca......
  • Linux连接终端时添加自定义提示信息
    vim./bashrcecho-e"\033{\033[5m"echo-e"\033[44;37m*********************************************************\033[5m"echo-e"\033[44;37m*/    ......
  • 自定义线程池
    自定义线程池packagecom.intell.config;​importorg.springframework.beans.factory.annotation.Value;importorg.springframework.context.annotation.Bean;importor......
  • 自定义注解+面向切面 日志记录
    自定义注解packagecom.example.spring.controller;​importjava.lang.annotation.*;​@Target(value=ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)@Docum......
  • 用XML自定义Excel功能区
    一、XML编写<?xmlversion="1.0"encoding="utf-8"?><customUIxmlns="http://schemas.microsoft.com/office/2006/01/customui"><ribbonstartFromScratch="fal......
  • C# 处理实体类赋值(获取嵌套类型,支持list 自定义类型)
    publicstaticTAESEncrypt<T>(Tobj)whereT:class{if(obj==null){returnobj;}varproperties=typeof(T).GetProperties();foreach(System.Reflecti......