观看中心文字

时间:2015-08-10 21:41:21

标签: android android-service wear-os

我一直在尝试多种方法在手表上居中3行文字,似乎无法弄明白。新的表盘服务要求您绘制到画布上。我通过抓住每一行的文本长度并除以2来初始化文本偏移量。

mXDateOffset = mTextDateColorPaint.measureText("May 21, 2015") /2;
mXTimeOffset = mTextTimeColorPaint.measureText("12:00:00")/2;
mXBatteryOffset = mTextBatteryColorPaint.measureText("99%") /2 ;

然后当我将文本和绘画元素设置到画布时,我发现矩形(屏幕)的中心减去屏幕以找到文本的开始位置。

canvas.drawText( timeText, bounds.centerX() - mXTimeOffset, 40.0f, mTextTimeColorPaint );
canvas.drawText( date, bounds.centerX() - mXDateOffset, 90.0f, mTextDateColorPaint);
canvas.drawText( mBatteryLevel, bounds.centerX() - mXBatteryOffset, 130.0f, mTextBatteryColorPaint );

我得到的是......

enter image description here

不确定还能做什么。

1 个答案:

答案 0 :(得分:7)

这里有很多回答这里是我想要访问onDraw方法的原因:

onCreate()中的

设置了偏移量

mYTimeOffset = getResources().getDimension( R.dimen.y_offset );
mYDateOffset = getResources().getDimension( R.dimen.y_date_offset );
mYBatteryOffset = getResources().getDimension( R.dimen.y_battery_offset );

然后调用drawtime并设置画布参数

    private void drawTimeText(Canvas canvas, Rect bounds) {
        String timeText = getHourString() + ":" + String.format( "%02d", mDisplayTime.minute );
        if( isInAmbientMode() || mIsInMuteMode ) {
            timeText += ( mDisplayTime.hour < 12 ) ? "AM" : "PM";
        } else {
            timeText += String.format( ":%02d", mDisplayTime.second);
        }

        String date = new SimpleDateFormat(DATE_FORMAT_DISPLAYED).format(Calendar.getInstance().getTime());
        canvas.drawText( timeText, bounds.centerX() - (mTextTimeColorPaint.measureText(timeText))/2, mYTimeOffset, mTextTimeColorPaint );
        canvas.drawText( date, bounds.centerX() - (mTextDateColorPaint.measureText(date))/2, mYDateOffset, mTextDateColorPaint);
        if (mBatteryLevel != null)
        canvas.drawText( mBatteryLevel, bounds.centerX() - (mTextBatteryColorPaint.measureText(mBatteryLevel))/2, mYBatteryOffset, mTextBatteryColorPaint );
    }