文字用法"/>
Paint绘制文字用法
/** * 负责设置获取文字相关的 */ paint.setTextSize(50); paint.setUnderlineText(true); //是否有下划线和设置下划线。 paint.setStrikeThruText(true); //获取与设置是否有文本删除线。 //获取与设置字体类型。Android默认有四种字体样式:BOLD(加粗)、BOLD_ITALIC(加粗并倾斜)、ITALIC(倾斜)、NORMAL(正常),我们也可以通过Typeface类来自定义个性化字体。 paint.setTypeface(Typeface.create(Typeface.DEFAULT,Typeface.BOLD)); //设置文字倾斜,参数没有具体范围,官方推荐值为-0.25,值为负则右倾,为正则左倾,默认值为0。 paint.setTextSkewX(-0.25f); //获取与设置文本对齐方式,取值为CENTER、LEFT、RIGHT,也就是文字绘制是左边对齐、右边还是局中的。 paint.setTextAlign(Paint.Align.LEFT); final String STR = "动脑ABCDEF"; float[] value = new float[1]; int ret = paint.breakText(STR,true,500,value); Log.i("aaaaaa","BreakText="+ret+",STR="+STR.length()+",value="+value[0]+"\n"); //获取文本的宽高,通过bounds的Rect拿到整型。 Rect rect = new Rect(); paint.getTextBounds(STR,0,STR.length(),rect); Log.i("aaaaaaa","RectHeight:"+rect.height()+",RectWidth:"+rect.width()+"\n"); //粗略获取文本的宽度,和上面的getTextBounds比较类似,返回浮点数。 float aa = paint.measureText(STR,0,STR.length()); Log.i("aaaaaaa","measureText:"+aa+"\n"); //精确计算文字宽度,与上面两个类似。 float[] bb = new float[20]; int cc = paint.getTextWidths(STR,1,STR.length(),bb); Log.i("aaaaaaa","getTextWidths:"+cc+"\n"); for(int i = 0;i<bb.length;i++){Log.i("aaaaaaa","measureText:"+bb[i]+"\n"); }/** * 寻找baseLine * Paint.FontMetricsInt * 里面有top,ascent,desent, bottom, leading这几个属性 * top = top线的y坐标 - baseline线的y坐标 * bottom = bottom线的y坐标 - baseline线的y坐标 * ascent = ascent线的y坐标 - baseline线的y坐标 * desent = desent线的y坐标 - baseline线的y坐标 * 计算得baseline = center +(FontMetrics.bottom - FontMetrics.top)/2 - FontMetrics.bottom */ final String STR = "二度看风景defghijl"; paint.setTextSize(100); Paint.FontMetricsInt fontMetricsInt = paint.getFontMetricsInt(); Log.i("aaaaa",fontMetricsInt.top+","+fontMetricsInt.ascent+","+fontMetricsInt.descent+","+fontMetricsInt.bottom+","); canvas.drawText(STR,100,200+(fontMetricsInt.bottom-fontMetricsInt.top)/2-fontMetricsInt.bottom,paint); // 看度量线 paint.setColor(Color.BLUE); canvas.drawLine(0,200,1000,200,paint); canvas.drawLine(0,200+fontMetricsInt.top,1000,200+fontMetricsInt.top,paint); canvas.drawLine(0,200+fontMetricsInt.ascent,1000,200+fontMetricsInt.ascent,paint); canvas.drawLine(0,200+fontMetricsInt.bottom,1000,200+fontMetricsInt.bottom,paint); canvas.drawLine(0,200+fontMetricsInt.descent,1000,200+fontMetricsInt.descent,paint); canvas.drawText(STR,100,200,paint);
更多推荐
Paint绘制文字用法
发布评论