我正在制作一个Android表盘,我注意到无论我做什么,我的背景图像都不会完全居中。这是在模拟器和LG G Watch上测试的,具有相同的效果。
@Override
public void onDraw(Canvas canvas, Rect bounds) {
int width = bounds.width();
int height = bounds.height();
float mCenterX = width / 2f;
float mCenterY = height / 2f;
if (isInAmbientMode()) {
canvas.drawColor(Color.BLACK);
} else {
canvas.drawBitmap(mBackgroundBitmap, mCenterX, mCenterY, null);
}
@Override
public void onApplyWindowInsets(WindowInsets insets) {
super.onApplyWindowInsets(insets);
// Load resources that have alternate values for round watches.
Resources resources = FaceJake.this.getResources();
boolean isRound = insets.isRound();
mXOffset = resources.getDimension(isRound
? R.dimen.digital_x_offset_round : R.dimen.digital_x_offset);
float textSize = resources.getDimension(isRound
? R.dimen.digital_text_size_round : R.dimen.digital_text_size);
public void onCreate(SurfaceHolder holder) {
super.onCreate(holder);
setWatchFaceStyle(new WatchFaceStyle.Builder(FaceJake.this)
.setCardPeekMode(WatchFaceStyle.PEEK_MODE_VARIABLE)
.setBackgroundVisibility(WatchFaceStyle.BACKGROUND_VISIBILITY_INTERRUPTIVE)
.setShowSystemUiTime(false)
.setAcceptsTapEvents(true)
.build());
Resources resources = FaceJake.this.getResources();
mYOffset = resources.getDimension(R.dimen.digital_y_offset);
Drawable backgroundDrawable = resources.getDrawable(R.drawable.jakehappy);
mBackgroundBitmap = ((BitmapDrawable) backgroundDrawable).getBitmap();
mTextPaint = new Paint();
mTextPaint = createTextPaint(resources.getColor(R.color.digital_text));
mTime = new Time();
背景图片是320x320,没有此代码,它也没有居中。我甚至尝试通过中心X和Y浮动将值更改为许多不同的值,甚至100f,但这并没有帮助。 (左图:截图。右:图像为背景)