java canvas Android,Android Canvas.drawText

应该注意的是documentation建议直接使用 Layout 而不是 Canvas.drawText . 关于使用 StaticLayout 的完整答案是here,但我将在下面提供摘要 .

String text = "This is some text.";

TextPaint textPaint = new TextPaint();

textPaint.setAntiAlias(true);

textPaint.setTextSize(16 * getResources().getDisplayMetrics().density);

textPaint.setColor(0xFF000000);

int width = (int) textPaint.measureText(text);

StaticLayout staticLayout = new StaticLayout(text, textPaint, (int) width, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0, false);

staticLayout.draw(canvas);

以下是自定义视图上下文中的更完整示例:

a2da38d894c1ec016dc1a08f5eeb8de8.png

public class MyView extends View {

String mText = "This is some text.";

TextPaint mTextPaint;

StaticLayout mStaticLayout;

// use this constructor if creating MyView programmatically

public MyView(Context context) {

super(context);

initLabelView();

}

// this constructor is used when created from xml

public MyView(Context context, AttributeSet attrs) {

super(context, attrs);

initLabelView();

}

private void initLabelView() {

mTextPaint = new TextPaint();

mTextPaint.setAntiAlias(true);

mTextPaint.setTextSize(16 * getResources().getDisplayMetrics().density);

mTextPaint.setColor(0xFF000000);

// default to a single line of text

int width = (int) mTextPaint.measureText(mText);

mStaticLayout = new StaticLayout(mText, mTextPaint, (int) width, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0, false);

// New API alternate

//

// StaticLayout.Builder builder = StaticLayout.Builder.obtain(mText, 0, mText.length(), mTextPaint, width)

// .setAlignment(Layout.Alignment.ALIGN_NORMAL)

// .setLineSpacing(1, 0) // multiplier, add

// .setIncludePad(false);

// mStaticLayout = builder.build();

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

// Tell the parent layout how big this view would like to be

// but still respect any requirements (measure specs) that are passed down.

// determine the width

int width;

int widthMode = MeasureSpec.getMode(widthMeasureSpec);

int widthRequirement = MeasureSpec.getSize(widthMeasureSpec);

if (widthMode == MeasureSpec.EXACTLY) {

width = widthRequirement;

} else {

width = mStaticLayout.getWidth() + getPaddingLeft() + getPaddingRight();

if (widthMode == MeasureSpec.AT_MOST) {

if (width > widthRequirement) {

width = widthRequirement;

// too long for a single line so relayout as multiline

mStaticLayout = new StaticLayout(mText, mTextPaint, width, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0, false);

}

}

}

// determine the height

int height;

int heightMode = MeasureSpec.getMode(heightMeasureSpec);

int heightRequirement = MeasureSpec.getSize(heightMeasureSpec);

if (heightMode == MeasureSpec.EXACTLY) {

height = heightRequirement;

} else {

height = mStaticLayout.getHeight() + getPaddingTop() + getPaddingBottom();

if (heightMode == MeasureSpec.AT_MOST) {

height = Math.min(height, heightRequirement);

}

}

// Required call: set width and height

setMeasuredDimension(width, height);

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

// do as little as possible inside onDraw to improve performance

// draw the text on the canvas after adjusting for padding

canvas.save();

canvas.translate(getPaddingLeft(), getPaddingTop());

mStaticLayout.draw(canvas);

canvas.restore();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值