【问题标题】:x and y coordinates for drawing text on (rotated) canvas?在(旋转的)画布上绘制文本的 x 和 y 坐标?
【发布时间】:2011-09-29 00:53:51
【问题描述】:

我正在尝试使用 Paint 在 Canvas 上绘制文本,但我似乎无法找出合适的 x 和 y 来给出它。如果我记录 getWidth 和 getHeight,我可以看到 View 实际上给出了旋转画布的宽度和高度。因此,它类似于 20 X 240。我所拥有的很接近,但我只希望文本垂直和水平居中。

关于如何做到这一点的任何想法?

这是现在的样子:

这是我的代码:

public class VerticalText extends View {

    private Paint mTextPaint;
    private String mText;
    private int mAscent;


    public ListLabelView(Context context) {
        super(context);
        initListLabel();
        this.setBackgroundColor(Color.BLUE);
    }

    private final void initListLabel() {

        mTextPaint = new Paint();
        mTextPaint.setStyle(Paint.Style.FILL);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextSize(12);
        mTextPaint.setColor(Color.WHITE);

        setPadding(3, 3, 3, 3);
        invalidate();

    }

    /**
     * sets the text to display in this label
     * @param text
     */
    public void setText(String text) {
        mText = text;
        requestLayout();
        invalidate();
    }

    /**
     * sets the text color for this label
     * @param color
     */
    public void setTextColor(int color) {
        mTextPaint.setColor(color);
        invalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(measureHeight(widthMeasureSpec), measureWidth(heightMeasureSpec));

    }

    /**
     * Determines the width of this view
     * @param measureSpec A measureSpec packed into an int
     * @return The width of the view, honoring constraints from measureSpec
     */
    private int measureWidth(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        if (specMode == MeasureSpec.EXACTLY) {
            // We were told how big to be
            result = specSize;
        } else {
            // Measure the text
            result = (int) mTextPaint.measureText(mText) + getPaddingLeft()
                    + getPaddingRight();
            if (specMode == MeasureSpec.AT_MOST) {
                // Respect AT_MOST value if that was what is called for by measureSpec
                result = Math.min(result, specSize);
            }
        }

        return result;
    }

    /**
     * Determines the height of this view
     * @param measureSpec A measureSpec packed into an int
     * @return The height of the view, honoring constraints from measureSpec
     */
    private int measureHeight(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        mAscent = (int) mTextPaint.ascent();
        if (specMode == MeasureSpec.EXACTLY) {
            // We were told how big to be
            result = specSize;
        } else {
            // Measure the text (beware: ascent is a negative number)
            result = (int) (-mAscent + mTextPaint.descent()) + getPaddingTop()
                    + getPaddingBottom();
            if (specMode == MeasureSpec.AT_MOST) {
                // Respect AT_MOST value if that was what is called for by measureSpec
                result = Math.min(result, specSize);
            }
        }
        return result;
    }

    @Override
    protected void onDraw(Canvas canvas) {

        canvas.rotate(-90,  getWidth()/2, getHeight()/2);
//
        mTextPaint.measureText(mText);

        Log.v("width x height:", getWidth() + " X " + getHeight());

        canvas.drawText(mText, (getWidth() - (getWidth())) - 50, getHeight()/2,  mTextPaint);

        super.onDraw(canvas);



    }
}

【问题讨论】:

    标签: android


    【解决方案1】:

    我遇到了同样的问题。这对我有用:

    Rect tb = new Rect();
    mTextPaint.getTextBounds(mText, 0, mText.length(), tb);
    canvas.drawText(mText, getWidth() / 2f, getHeight() / 2f + Math.abs(tb.top) / 2f, mTextPaint);
    

    你的文字应该居中:

    mTextPaint.setTextAlign(Align.CENTER);
    

    【讨论】:

    • 完美!那么 getTextBounds 基本上只是将这些边界传递给 rect?
    • 没错。测量字符串的整个边界以仅获取高度可能有点矫枉过正,但它有效;-)但有时返回的值令人困惑(但当我自己绘制文本时,我从来没有失败过,只有当我试图在 TextView 中破解我)。您可以在文本矿石大小发生变化时测量一次,以避免每次绘制都重新计算。
    • 这适用于所有形状吗?我需要用圆圈包围数字。
    • 返回值描述了一个边界框。因此,如果您适合该边界框,则测量的文本将始终适合。我还将它与圆形和圆形矩形一起使用。我进行了二进制搜索以找到最佳匹配,效果很好。只是避免重新计算,它非常昂贵。
    猜你喜欢
    • 2015-04-18
    • 2012-01-01
    • 1970-01-01
    • 2011-03-11
    • 1970-01-01
    • 2022-09-30
    • 1970-01-01
    • 1970-01-01
    • 2015-03-24
    相关资源
    最近更新 更多