手表上的中心文字 [英] Center Text on Watch
问题描述
我一直在尝试多种方法来将3行文字放在手表上居中,但似乎无法弄清楚.新的表盘服务需要您在画布上绘画.我通过抓住每行文本的长度并除以2来初始化文本偏移量.
I've been trying multiple ways to center 3 lines of text on a watch and can't seem to figure it out. The new watch face services require you to paint to a canvas. I initialize the text offset by grabbing the length of text of each line and dividing by 2.
mXDateOffset = mTextDateColorPaint.measureText("May 21, 2015") /2;
mXTimeOffset = mTextTimeColorPaint.measureText("12:00:00")/2;
mXBatteryOffset = mTextBatteryColorPaint.measureText("99%") /2 ;
然后,当我将文本和绘画元素设置到画布上时,我发现矩形(屏幕)的中心减去屏幕,以找到文本的开始位置.
Then when I set the text and paint elements to the canvas I find the center of the rectangle(screen) minus the screen to find the start position of the text.
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 );
我得到的是这个...
What I get is this...
不确定要做什么.
推荐答案
这个答案有很多,这是我不得不访问onDraw方法的结果:
There is alot that went into this answer here is what I came up with having to access the onDraw methods:
在onCreate()中设置偏移量
in onCreate() set the offsets
mYTimeOffset = getResources().getDimension( R.dimen.y_offset );
mYDateOffset = getResources().getDimension( R.dimen.y_date_offset );
mYBatteryOffset = getResources().getDimension( R.dimen.y_battery_offset );
然后调用绘图时间并设置画布参数
then call the drawtime and set the canvas parameters
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 );
}
这篇关于手表上的中心文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!