您的位置:首页 > 职场人生

面试例题4:绘制5行文本,每一行的字体大小逐渐增加

2012-07-23 20:39 417 查看
题目来自:《android高薪之路—android程序员面试宝典》一书 ,只是为了个人学习方便

实现如图效果:





使用Canvas.drawText方法绘制5行文本,每一行的字体大小逐渐增加

package com.app;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class DrawTextView extends View{
private Paint paint = null;
int y = 0;
public DrawTextView(Context context) {
super(context);
paint = new Paint();
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float textSizeArray[]=new float[]{15,18,21,24,27};
for(int i=0;i<textSizeArray.length;i++){
paint.setTextSize(textSizeArray[i]);
paint.setColor(Color.BLUE);
//获取文本的宽度可以用measureText方法
//public void drawText(String text, float x, float y, Paint paint)
//Parameters
//      text:The text to be drawn
//          x :The x-coordinate of the origin of the text being drawn
//          y :The y-coordinate of the origin of the text being drawn
//    paint :The paint used for the text (e.g. color, size, style)

canvas.drawText( "Android(宽度:" +paint .measureText("Android")+ ")", 0, 50+y , paint );

//每行文字距离5个像素
y+= paint.getTextSize()+5;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐