androidpaint字体
Ⅰ android 用paint写字,请问有什么办法能设置 字间距吗
这个方法就可以,那个1.3f就是字间距,可以调节
public static void getText(Canvas canvas,String str,float x,float y){
final TextPaint paint1 = new TextPaint(Paint.ANTI_ALIAS_FLAG);
paint1.setColor(Color.WHITE);
paint1.setAntiAlias(true);
paint1.setTextSize(23);
paint1.setTextAlign(Align.LEFT);
final StaticLayout layout = new StaticLayout(
str, paint1,
(int) (MySurfaceView.screenW * 0.8),
Layout.Alignment.ALIGN_NORMAL, 1.3f, 0, true);
// layout是默认画在Canvas的(0,0)点的,如果需要调整位置只能在draw之前移Canvas的起始坐标
canvas.translate(x, y);
layout.draw(canvas);
}
Ⅱ android 怎样在代码中给widget设置字体
android给控件widget设置自定义字体的方式如下:
自定义字体必须放到asset目录下,需要调用context.getAssets()方法获取自定义字体的资源,由于android Widget是依赖于其他进程存在,故widget中无法使用字体设置的.setTypeface方法,因此在Widget中使用自定义字体,可以将字体转换为图片输出以后,用views.setImageViewBitmap方法可实现自定义功能,参考代码:
java">staticBitmapbuildUpdate(Stringtime,Contextcontext){
BitmapmyBitmap=Bitmap.createBitmap(240,80,Bitmap.Config.ARGB_4444);
CanvasmyCanvas=newCanvas(myBitmap);
Paintpaint=newPaint();
Typefacetf=Typeface.createFromAsset(context.getAssets(),"fonts/Clockopia.ttf");
paint.setAntiAlias(true);
paint.setAlpha(110);//取值范围为0~255,值越小越透明
paint.setSubpixelText(true);
paint.setTypeface(tf);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
paint.setTextSize(80);
paint.setTextAlign(Align.CENTER);
myCanvas.drawText(time,100,60,paint);
returnmyBitmap;
}
//widget中调用
RemoteViewsmViews=newRemoteViews(context.getPackageName(),R.layout.main);
mViews.setImageViewBitmap(R.id.imageView1,buildUpdate(time,context));
Ⅲ android canvas的drawText方法 如何设置字体大小和格式。
Canvas相当于画布,字体的大小格式在Paint上设置才正确, Paint 相当于画笔。代码如下,没有具体参数:
Paint paint = new Paint();
paint.setTextSize(textSize);//设置字体大小
paint.setTypeface(typeface);//设置字体类型
canvas.drawText(text, x, y, paint);//使用画笔paint
@Override
public void onDraw (Canvas canvas) {
Rect targetRect = new Rect(50, 50, 1000, 200);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStrokeWidth(3);
paint.setTextSize(80);
String testString = "测试:ijkJQKA:1234";
paint.setColor(Color.CYAN);
canvas.drawRect(targetRect, paint);
paint.setColor(Color.RED);
FontMetricsInt fontMetrics = paint.getFontMetricsInt();
(3)androidpaint字体扩展阅读:
Screen Space - Camera
此模式类似Screen Space - Overlay,但区别是此模式将Canvas放置于某个Camera前固定距离。此Camera负责渲染所有UI元素,则摄像机参数(Camera Settings)直接影响UI表现。
比如Camera是透视模式(Perspective),则UI元素会基于Field of View的值而扭曲变形。同样的,若屏幕分辨率变更,或者视觉平截体(CameraFrustrum)改变,则Canvas自动调整自身尺寸作自适应。
Ⅳ Android 如何实现竖排文字显示
在android.graphics.Canvas类中有个沿路径画字的方法
void drawTextOnPath(String text, Path path, float hOffset, float vOffset, Paint paint)
Draw the text, with origin at (x,y), using the specified paint, along the specified path.
void drawTextOnPath(char[] text, int index, int count, Path path, float hOffset, float vOffset, Paint paint)
Draw the text, with origin at (x,y), using the specified paint, along the specified path.
Test.java代码://需要在layout中定义Test,且设置背景,在java代码中设置test Text
public class Test extends View {
private Paint paint;
private Path path;
private Matrix matrix;
private int width = -1;
private int height = -1;
private float left = 3;
private float top = 18;
private String title = "";
BitmapDrawable drawable = (BitmapDrawable) getBackground();
public Test(Context context, AttributeSet attrs) {
super(context, attrs);
paint = new Paint();
paint.setColor(Color.WHITE);//定义字体颜色
paint.setTextSize(14);//定义字体大小
path = new Path();
path.lineTo(0,500);//定义字符路径
matrix = new Matrix();
Log.v("onMeasure", "2");
}
@Override
protected void onDraw(Canvas canvas) {
//画背景
Bitmap b = Bitmap.createBitmap(drawable.getBitmap(),0,0,width,height);
canvas.drawBitmap(b, matrix, paint);
//画字
showText(canvas, title);
}
private void showText(Canvas canvas, String text){
float w;
final int len = text.length();
float py = 0 + top;
for(int i=0; i<len; i ++){
char c = text.charAt(i);
w = paint.measureText(text, i, i+1);//获取字符宽度
StringBuffer b = new StringBuffer();
b.append(c);
if(py > 81){//定义字的范围
return;
}
if(isChinese(c)){
py += w;
if(py > 81){
return;
}
canvas.drawText(b.toString(), left, py, paint); //中文处理方法
}else {
canvas.drawTextOnPath(b.toString(), path, py, -left-2, paint);//其他文字处理方法
py += w;
}
}
}
public void setText(String title){
this.title = title;
}
public String getText(){
return title;
}
private boolean isChinese(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
return true;
}
return false;
}
//重写View大小方法,使view大小为背景图片大小
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (null != getBackground()) {
int h = drawable.getIntrinsicHeight();
int w = drawable.getIntrinsicWidth();
Log.v("onMeasure", "null != getBackground() h:" + h + " w:" + w);
width = w;
height = h;
setMeasuredDimension(w, h);
} else {
width = widthMeasureSpec;
height = heightMeasureSpec;
super.measure(widthMeasureSpec, heightMeasureSpec);
}
}
}
在Android中,若要通过程序改变屏幕显示的方向,必须要覆盖setRequestedOrientation()方法,而若要取得目前的屏幕方向,则需要访问getRequestedOrientation()方法。本范例为求简要示范更改做法,设计了一个按钮,当单击按钮的同时,判断当下的屏幕方向,例如竖排(PORTRAIT),则将其更改为横排(LANDSCAPE);若为横排(LANDSCAPE),则将其更改为竖排(PORTRAIT)
Ⅳ Android canvas 如何绘制字体大小为25的一行文本
Canvas相当于画布,字体的大小格式在Paint上设置才正确, Paint 相当于画笔。代码如下,没有具体参数:希望能帮到你 Paint paint = new Paint(); paint.setTextSize(textSize);//设置字体大小 paint.setTypeface(typeface);//设置字体类型
Ⅵ Android中如何让字体旋转
自定义View, 继承Textview ,在onDraw 函数调用super方法之前 使用 canvas.translate 进行旋转操作。
封装一下就可以外部设置旋转角度了。