开发者

x and y coordinates for drawing text on (rotated) canvas?

开发者 https://www.devze.com 2023-04-08 04:11 出处:网络
I\'m trying to drawText on a Canvas using Paint and I can\'t seem to figure out the proper x and y to give it. If I log the getWidth and getHeight, I can see that the View is actually giving the width

I'm trying to drawText on a Canvas using Paint and I can't seem to figure out the proper x and y to give it. If I log the getWidth and getHeight, I can see that the View is actually giving the width and height of the rotated canvas. Thus, its something like 20 X 240. What I have is close, but I just want the text to be centered both vertically and horizontally.

Any ideas on how I can do that?

Here's what it looks like now:

x and y coordinates for drawing text on (rotated) canvas?

Here's my code:

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) {
     开发者_JS百科           // 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);



    }
}


I faced the same problem. Here's what worked for me:

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

Your text should be centered:

mTextPaint.setTextAlign(Align.CENTER);
0

精彩评论

暂无评论...
验证码 换一张
取 消