Skip to content

Commit

Permalink
绘制多行居中文字
Browse files Browse the repository at this point in the history
  • Loading branch information
alidili committed Jan 2, 2018
1 parent c387d71 commit d0a2e45
Showing 1 changed file with 94 additions and 5 deletions.
99 changes: 94 additions & 5 deletions DrawTextDemo/app/src/main/java/com/yl/drawtext/CustomTextView.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
Expand Down Expand Up @@ -37,6 +40,7 @@ private void init() {
paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.GRAY);
paint.setTextSize(sp2px(50));
paint.setStyle(Paint.Style.STROKE);
}

Expand All @@ -50,8 +54,26 @@ public void draw(Canvas canvas) {
// y轴
canvas.drawLine(0, -getHeight() / 2, 0, getHeight() / 2, paint);

// 绘制坐标
drawCoordinate(canvas);

// 绘制居中文字
//drawCenterText(canvas);

// 绘制多行居中文字(方式1)
//drawCenterMultiText1(canvas);

// 绘制多行居中文字(方式2)
//drawCenterMultiText2(canvas);
}

/**
* 绘制坐标
*
* @param canvas 画布
*/
private void drawCoordinate(Canvas canvas) {
// 绘制文字
paint.setTextSize(sp2px(50));
canvas.drawText("YangLe", 0, 0, paint);

// top-红
Expand All @@ -74,15 +96,82 @@ public void draw(Canvas canvas) {
float bottom = fontMetrics.bottom;
paint.setColor(Color.BLUE);
canvas.drawLine(-getWidth() / 2, bottom, getWidth() / 2, bottom, paint);
}

// 绘制居中文字
/*paint.setTextSize(sp2px(50));
paint.setColor(Color.GRAY);
/**
* 绘制居中文字
*
* @param canvas 画布
*/
private void drawCenterText(Canvas canvas) {
// 文字宽
float textWidth = paint.measureText("YangLe");
// 文字baseline在y轴方向的位置
float baseLineY = Math.abs(paint.ascent() + paint.descent()) / 2;
canvas.drawText("YangLe", -textWidth / 2, baseLineY, paint);*/
canvas.drawText("YangLe", -textWidth / 2, baseLineY, paint);
}

/**
* 绘制多行居中文字(方式1)
*
* @param canvas 画布
*/
private void drawCenterMultiText1(Canvas canvas) {
String text = "ABC";

// 画笔
TextPaint textPaint = new TextPaint();
textPaint.setAntiAlias(true);
textPaint.setColor(Color.GRAY);

// 设置宽度超过50dp时换行
StaticLayout staticLayout = new StaticLayout(text, textPaint, dp2px(50),
Layout.Alignment.ALIGN_CENTER, 1, 0, false);
canvas.save();
canvas.translate(-staticLayout.getWidth() / 2, -staticLayout.getHeight() / 2);
staticLayout.draw(canvas);
canvas.restore();
}

/**
* 绘制多行居中文字(方式2)
*
* @param canvas 画布
*/
private void drawCenterMultiText2(Canvas canvas) {
String[] texts = {"A", "B", "C"};
Paint.FontMetrics fontMetrics = paint.getFontMetrics();
float top = Math.abs(fontMetrics.top);
float ascent = Math.abs(fontMetrics.ascent);
float descent = fontMetrics.descent;
float bottom = fontMetrics.bottom;
// 行数
int textLines = texts.length;
// 文字高度
float textHeight = top + bottom;
// 文字总高度
float textTotalHeight = textHeight * textLines;
// 基数
float basePosition = (textLines - 1) / 2f;

for (int i = 0; i < textLines; i++) {
// 文字宽度
float textWidth = paint.measureText(texts[i]);
// 文字baseline在y轴方向的位置
float baselineY;

if (i < basePosition) { // 上
baselineY = -(textTotalHeight / 2 - textHeight * i - top);

} else if (i > basePosition) { // 下
baselineY = textTotalHeight / 2 - textHeight * (textLines - i - 1) - bottom;

} else { // 中
baselineY = (ascent - descent) / 2;
}

canvas.drawText(texts[i], -textWidth / 2, baselineY, paint);
}
}

/**
Expand Down

0 comments on commit d0a2e45

Please sign in to comment.