用canvas写文本(onDraw视图)

时间:2016-10-23 18:05:28

标签: android canvas view

我有这个视图(颜色选择器视图)

public class ColorPickerOvalView extends View {

    private Paint paint;
    private Paint centerPaint;
    private Paint paintText;
    private final int[] colorsArray;
    private OnColorChangedListener changedListener;
    private String key;
    private boolean trackingCenter;
    private boolean highlightCenter;

    private RectF rectF;

    private static final int CENTER_X = 120;
    private static final int CENTER_Y = 120;
    private static final int CENTER_RADIUS = 39;
    private static final float PI = 3.1415926f;

    public ColorPickerOvalView(Context context) {
        super(context);
        colorsArray = new int[]{
                0xFFFF0000, 0xFFFF00FF, 0xFF0000FF, 0xFF00FFFF, 0xFF00FF00,
                0xFFFFFF00, 0xFFFF0000
        };
    }

    public ColorPickerOvalView(Context context, AttributeSet attrs) {
        super(context, attrs);
        colorsArray = new int[]{
                0xFFFF0000, 0xFFFF00FF, 0xFF0000FF, 0xFF00FFFF, 0xFF00FF00,
                0xFFFFFF00, 0xFFFF0000
        };
    }

    public ColorPickerOvalView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        colorsArray = new int[]{
                0xFFFF0000, 0xFFFF00FF, 0xFF0000FF, 0xFF00FFFF, 0xFF00FF00,
                0xFFFFFF00, 0xFFFF0000
        };
    }

    public void init(OnColorChangedListener listener, int color, String key) {
        //super(c);
        changedListener = listener;

        Shader shader = new SweepGradient(0, 0, colorsArray, null);

        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setShader(shader);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(50);

        centerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        centerPaint.setColor(color);
        centerPaint.setStrokeWidth(10);

        paintText = new Paint();
        paintText.setColor(Color.WHITE);

        this.key = key;

        float raio = CENTER_X - paint.getStrokeWidth() * 0.5f;
        rectF = new RectF(-raio, -raio, raio, raio);
    }


    @Override
    protected void onDraw(Canvas canvas) {

        canvas.translate(CENTER_X, CENTER_X);

        canvas.drawOval(rectF, paint);
        canvas.drawCircle(0, 0, CENTER_RADIUS, centerPaint);

        //write the text
        canvas.drawText("OK", 0, 0, paintText);

        if (trackingCenter) {
            int color = centerPaint.getColor();
            centerPaint.setStyle(Paint.Style.STROKE);

            if (highlightCenter) {
                centerPaint.setAlpha(0xFF);
            } else {
                centerPaint.setAlpha(0x80);
            }

            canvas.drawCircle(0, 0, CENTER_RADIUS + centerPaint.getStrokeWidth(), centerPaint);

            centerPaint.setStyle(Paint.Style.FILL);
            centerPaint.setColor(color);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(CENTER_X * 2, CENTER_Y * 2);
    }

    ...

}

我想写#34;好的"集中在圈子上,但我的确存在错误(不要集中)。 我需要将此文本准确地放在圆心。

enter image description here

我如何集中这篇文章?以及我如何改变它的大小?

由于

2 个答案:

答案 0 :(得分:1)

drawText()开始绘制字符串中第一个字符的左下角。如果您希望它居中,您首先必须计算整个文本块的大小,然后偏移您的起始位置。

要获取文本的大小,可以使用Paint.getTextBounds()。

答案 1 :(得分:1)

您必须将文本宽度的一半向左移动。这意味着:

float textWidth = paintText.measureText(timeText);
float xOffset = textWidth / 2;
canvas.drawText("OK", -xOffset, 0, paintText);