我环顾四周,但没有任何效果。每次我尝试寻找表盘的屏幕中心时,其最终位置总是总是略高于中心,并位于屏幕的最右边(这与Android Studio原始模板存在的问题不同,在哪里表盘稍微向上,一直到屏幕的最左端)。我可能做错了什么?
(第一个屏幕截图在左边:我的问题。第二个屏幕截图:来自Android Studio的Android Wear模板的原始形式)
@Override
public void onDraw(Canvas canvas, Rect bounds) {
if (isInAmbientMode()) {canvas.drawColor(Color.BLACK);
}
else {
canvas.drawRect(0, 0, bounds.width(), bounds.height(), mBackgroundPaint);
}
// Draw H:MM in ambient mode or H:MM:SS in interactive mode.
long now = System.currentTimeMillis();
mCalendar.setTimeInMillis(now);
int width = bounds.width();
int height = bounds.height();
float centerX = width / 2f;
float centerY = height / 2f;
String text = mAmbient
? String.format("%d:%02d", mCalendar.get(Calendar.HOUR),
mCalendar.get(Calendar.MINUTE))
: String.format("%d:%02d", mCalendar.get(Calendar.HOUR),
mCalendar.get(Calendar.MINUTE));
canvas.drawText(text, centerX, centerY, mTextPaint);
}
完整的代码可以在这里找到
据我所知,您可以快速浏览一下您已经计算出的资源mTextXOffset
,mTextYOffset
但不要将其用于绘图调用,因此,文本原点位于问题屏幕截图的中心,而不是文本中心。
调整您centerX
和centerY
的位置mTextXOffset
,mTextYOffset
应该可以解决问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句