Android画布绘制相等距离的多个矩形

时间:2015-01-10 22:20:27

标签: android canvas rect

我有4 Strings。我想在每个周围画一个矩形,让它们彼此相等。我有“工作”的代码,但我的最后一个矩形需要3个空格才能使距离相等,我不明白为什么。我正在使用LG G Watch R,以防相关。

private void drawDate(Canvas canvas, float centerX, float centerY)
    {
        float x = centerX + 75f;
        float y = centerY + 50f;
        String dayOfWeek = daysOfWeek[mTime.weekDay] + ",";
        String month = months[mTime.month];
        String monthDay = mTime.monthDay + ",";
        String year = Integer.toString(mTime.year);
        float height = outLinePaint.getTextSize();
        float totalWidth = outLinePaint.measureText(String.format("%s %s %s %s", dayOfWeek, month, monthDay, year));
        final float spaceWidth = outLinePaint.measureText(" ");
        x -= totalWidth/2f;
        outLinePaint.setStyle(Paint.Style.FILL);

        float halfWidth = outLinePaint.measureText(dayOfWeek)/2f;
        canvas.drawRect(x - halfWidth, y - height, x + halfWidth, y + height /2f , whitePaint);
        canvas.drawText(dayOfWeek, x, y, outLinePaint);

        x += (halfWidth*2f)+spaceWidth;
        halfWidth = outLinePaint.measureText(month)/2f;
        canvas.drawRect(x - halfWidth, y - height, x + halfWidth, y + height /2f , whitePaint);
        canvas.drawText(month, x, y, outLinePaint);

        x += (halfWidth*2f)+spaceWidth;
        halfWidth = outLinePaint.measureText(monthDay)/2f;
        canvas.drawRect(x - halfWidth, y - height, x + halfWidth, y + height /2f , whitePaint);
        canvas.drawText(monthDay, x, y, outLinePaint);

        //THIS LINE
        x += (halfWidth*2f)+spaceWidth+spaceWidth+spaceWidth;
        //it looks right on the canvas, but why can I not simply
        //x += (halfWidth*2f)+spaceWidth; like above?
        halfWidth = outLinePaint.measureText(year)/2f;
        canvas.drawRect(x - halfWidth, y - height, x + halfWidth, y + height /2f , whitePaint);
        canvas.drawText(year, x, y, outLinePaint);

        outLinePaint.setStyle(Paint.Style.STROKE);
    }

注意:此“BUG”仅会影响最后一串

这是我所期望的(如果我使用x += (halfWidth*2f)+spaceWidth+spaceWidth+spaceWidth;,我会得到的):

"Correct"

但是,如果我使用x += (halfWidth*2f)+spaceWidth;,就像我使用其他3,我得到:

enter image description here

1 个答案:

答案 0 :(得分:0)

我已经解决了。我无法得到@pskink的例子来为我的生活工作(它没有画在我的画布上)。问题是我的中心调整。

x位于我Rect的中间而不是左边。我将x的宽度添加到Rect时,我希望x成为x中最左边的点。

我通过将文本对齐设置为左(以便drawDate()是我最左边的点)来修复它,并将我的float spaceWidth; private void drawDate(float centerX, float centerY) { float x = centerX + centerX*DATE_AND_COMPASS_X_OFFSET; float y = centerY + centerY*DATE_AND_COMPASS_Y_OFFSET; String dayOfWeek = daysOfWeek[mTime.weekDay] + ","; String month = months[mTime.month]; String monthDay = mTime.monthDay + ","; String year = Integer.toString(mTime.year); float height = outLinePaint.getTextSize(); float totalWidth = outLinePaint.measureText(String.format("%s %s %s %s", dayOfWeek, monthnthDay, year)); spaceWidth = outLinePaint.measureText(" "); outLinePaint.setStyle(Paint.Style.FILL); x-=totalWidth/2; x+=drawDateInline(dayOfWeek, x, y, height); x+=drawDateInline(month, x, y, height); x+=drawDateInline(monthDay, x, y, height); drawDateInline(year, x, y, height); outLinePaint.setStyle(Paint.Style.STROKE); } private float drawDateInline(String word, float x, float y, float height) { float width = outLinePaint.measureText(word); canvas.drawRect(x, y - height, x + width, y + height /2f, whitePaint); canvas.drawText(word, x, y, outLinePaint); return width+spaceWidth; } 方法更改为:

{{1}}

我也不确定这是一种“坏方法”。