public class TextAlign extends GraphicsActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new SampleView(this));
}
private static class SampleView extends View {
private Paint mPaint;
private float mX;
private float[] mPos;
private Path mPath;
private Paint mPathPaint;
private static final int DY = 30;
private static final String TEXT_L = "Left";
private static final String TEXT_C = "Center";
private static final String TEXT_R = "Right";
private static final String POSTEXT = "Positioned";
private static final String TEXTONPATH = "Along a path";
private static void makePath(Path p) {
p.moveTo(10, 0);//绘制波浪线
p.cubicTo(100, -50, 200, 50, 300, 0);
}
private float[] buildTextPositions(String text, float y, Paint paint) {
float[] widths = new float[text.length()];
// initially get the widths for each char
int n = paint.getTextWidths(text, widths);//把text中每个文字的宽度复制到widths数组中, n是text中的字数
// now populate the array, interleaving spaces for the Y values
float[] pos = new float[n * 2];//填充x y的位置
float accumulatedX = 0;
for (int i = 0; i < n; i++) {
pos[i*2 + 0] = accumulatedX;
pos[i*2 + 1] = y;
accumulatedX += widths[i];
}
return pos;
}
public SampleView(Context context) {
super(context);
setFocusable(true);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setTextSize(30);
mPaint.setTypeface(Typeface.SERIF);
mPos = buildTextPositions(POSTEXT, 0, mPaint);
mPath = new Path();
makePath(mPath);
mPathPaint = new Paint();
mPathPaint.setAntiAlias(true);
mPathPaint.setColor(0x800000FF);
mPathPaint.setStyle(Paint.Style.STROKE);
}
@Override protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
Paint p = mPaint;
float x = mX;
float y = 0;
float[] pos = mPos;
// draw the normal strings 绘制普通的文字
p.setColor(0x80FF0000);
canvas.drawLine(x, y, x, y+DY*3, p);
p.setColor(Color.BLACK);
canvas.translate(0, DY);//三种对齐方式 以x左对齐
p.setTextAlign(Paint.Align.LEFT);
canvas.drawText(TEXT_L, x, y, p);
canvas.translate(0, DY);//下移
p.setTextAlign(Paint.Align.CENTER);//以x中间对齐
canvas.drawText(TEXT_C, x, y, p);
canvas.translate(0, DY);//右对齐
p.setTextAlign(Paint.Align.RIGHT);
canvas.drawText(TEXT_R, x, y, p);
canvas.translate(100, DY*2);
// now draw the positioned strings
p.setColor(0xBB00FF00);
for (int i = 0; i < pos.length/2; i++) { //绘制一个网格
canvas.drawLine(pos[i*2+0], pos[i*2+1]-DY,
pos[i*2+0], pos[i*2+1]+DY*2, p);
}
p.setColor(Color.BLACK);
p.setTextAlign(Paint.Align.LEFT);
canvas.drawPosText(POSTEXT, pos, p);//可以通过位置数组来一个一个位置绘制文字,在参考点左边
canvas.translate(0, DY);
p.setTextAlign(Paint.Align.CENTER);//中间
canvas.drawPosText(POSTEXT, pos, p);
canvas.translate(0, DY);
p.setTextAlign(Paint.Align.RIGHT); //右边
canvas.drawPosText(POSTEXT, pos, p);
// now draw the text on path 可以根据路径来绘制文字
canvas.translate(-100, DY*2);
canvas.drawPath(mPath, mPathPaint);
p.setTextAlign(Paint.Align.LEFT);//在路径的左边
canvas.drawTextOnPath(TEXTONPATH, mPath, 0, 0, p);
canvas.translate(0, DY*1.5f);
canvas.drawPath(mPath, mPathPaint);
p.setTextAlign(Paint.Align.CENTER);//在路径的中间
canvas.drawTextOnPath(TEXTONPATH, mPath, 0, 0, p);
canvas.translate(0, DY*1.5f);
canvas.drawPath(mPath, mPathPaint);
p.setTextAlign(Paint.Align.RIGHT);//在路径的右边
canvas.drawTextOnPath(TEXTONPATH, mPath, 0, 0, p);
}
@Override
protected void onSizeChanged(int w, int h, int ow, int oh) {
super.onSizeChanged(w, h, ow, oh);
mX = w * 0.5f; // remember the center of the screen //居中显示
}
}
}
/*
*android 其实也可以自定义字体 通过paint就可以
*/
public class Typefaces extends GraphicsActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new SampleView(this));
}
private static class SampleView extends View {
private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private Typeface mFace;
public SampleView(Context context) {
super(context);
mFace = Typeface.createFromAsset(getContext().getAssets(), "fonts/samplefont.ttf"); //也可以从sd卡等地方拿到字体显示
mPaint.setTextSize(64);
}
@Override protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
mPaint.setTypeface(null);
canvas.drawText("Default", 10, 100, mPaint);
mPaint.setTypeface(mFace);
canvas.drawText("Custom", 10, 200, mPaint);
}
}
}
//打印Unicode编码可以进行,左右按钮 变化mbase 来调整其他unicode值
public class UnicodeChart extends GraphicsActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new SampleView(this));
}
private static class SampleView extends View {
private Paint mBigCharPaint;
private Paint mLabelPaint;
private final char[] mChars = new char[256];
private final float[] mPos = new float[512];
private int mBase;
private static final int XMUL = 20;
private static final int YMUL = 28;
private static final int YBASE = 18;
public SampleView(Context context) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
mBigCharPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mBigCharPaint.setTextSize(15);
mBigCharPaint.setTextAlign(Paint.Align.CENTER);
mLabelPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mLabelPaint.setTextSize(8);
mLabelPaint.setTextAlign(Paint.Align.CENTER);
// the position array is the same for all charts
float[] pos = mPos;
int index = 0;
for (int col = 0; col < 16; col++) {
final float x = col * XMUL + 10;
for (int row = 0; row < 16; row++) {
pos[index++] = x;
pos[index++] = row * YMUL + YBASE;
}
}
}
private float computeX(int index) {
return (index >> 4) * XMUL + 10;
}
private float computeY(int index) {
return (index & 0xF) * YMUL + YMUL;
}
private void drawChart(Canvas canvas, int base) {
char[] chars = mChars;
for (int i = 0; i < 256; i++) {
int unichar = base + i;
chars[i] = (char)unichar;//把每个整型值转为 char并输出
canvas.drawText(Integer.toHexString(unichar),
computeX(i), computeY(i), mLabelPaint);
}
canvas.drawPosText(chars, 0, 256, mPos, mBigCharPaint);
}
@Override protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
canvas.translate(0, 1);
drawChart(canvas, mBase * 256);
}
@Override public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_LEFT:
if (mBase > 0) {
mBase -= 1;
invalidate();
}
return true;
case KeyEvent.KEYCODE_DPAD_RIGHT:
mBase += 1;
invalidate();
return true;
default:
break;
}
return super.onKeyDown(keyCode, event);
}
}
}