gpt4 book ai didi

android - 在 Canvas 上垂直绘制文本

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:39:21 25 4
gpt4 key购买 nike

我想学习如何在 Canvas 上绘制垂直文本。对不起,也许是愚蠢的问题,但我无法解决这个问题。我可以这样做:

 if (i ==10)
{
this_str2 = "0.00";
}


canvas.save();
canvas.rotate(-90,190,90);
canvas.drawText(this_str2, x_guide +50, drawSizes[1] + drawSizes[3] - (i * drawSizes[3] / 10) +20, paint);
canvas.restore();
}

但是在X和Y上显示不正常还有什么办法解决这个问题7

最佳答案

试试这个自定义 TextView ,我不记得我从哪里获取的(在 StackOverflow 中),如果有人记得,请在评论中发布一个链接。

import android.content.Context;
import android.graphics.Canvas;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.TextView;

public class VerticalTextView extends TextView
{
final boolean topDown;

public VerticalTextView(Context context, AttributeSet attrs)
{
super(context, attrs);
final int gravity = getGravity();
if (Gravity.isVertical(gravity) && (gravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM)
{
setGravity((gravity & Gravity.HORIZONTAL_GRAVITY_MASK) | Gravity.TOP);
topDown = false;
}
else
topDown = true;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(heightMeasureSpec, widthMeasureSpec);
setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
}

@Override
protected void onDraw(Canvas canvas)
{
TextPaint textPaint = getPaint();
textPaint.setColor(getCurrentTextColor());
textPaint.drawableState = getDrawableState();

canvas.save();

if (topDown)
{
canvas.translate(getWidth(), 0);
canvas.rotate(90);
}
else
{
canvas.translate(0, getHeight());
canvas.rotate(-90);
}

canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop());

getLayout().draw(canvas);
canvas.restore();
}
}

关于android - 在 Canvas 上垂直绘制文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9262494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com