改变背景可绘制的android穿戴

时间:2015-03-29 06:30:04

标签: android wear-os ambient

我想问一下,当android磨损处于环境模式时,你会如何改变背景drawable?

这是我的代码

 public void draw(Canvas canvas, Rect bounds,Context context) {
    time.setToNow();
   // canvas.drawColor(Color.parseColor("#e91e63"));

    int width = bounds.width();
    int height = bounds.height();
    Resources resources = context.getResources();

    if (shouldShowAmbient) {
        backgroundDrawable = resources.getDrawable(R.drawable.ambient_domo);
    } else {
        backgroundDrawable = resources.getDrawable(R.drawable.domo);
    }

    mBackgroundBitmap = ((BitmapDrawable) backgroundDrawable).getBitmap();
    // Draw the background, scaled to fit.
    if (mBackgroundScaledBitmap == null
            || mBackgroundScaledBitmap.getWidth() != width
            || mBackgroundScaledBitmap.getHeight() != height) {
        mBackgroundScaledBitmap = Bitmap.createScaledBitmap(mBackgroundBitmap,
                width, height, true /* filter */);
    }
    canvas.drawBitmap(mBackgroundScaledBitmap, 0, 0, null);
    String timeText = String.format(shouldShowSeconds ? TIME_FORMAT_WITH_SECONDS : TIME_FORMAT_WITHOUT_SECONDS, time.hour, time.minute, time.second);
    float timeXOffset = computeXOffset(timeText, timePaint, bounds);
    float timeYOffset = computeTimeYOffset(timeText, timePaint, bounds);
    canvas.drawText(timeText, timeXOffset, timeYOffset, timePaint);

    String dateText = String.format(DATE_FORMAT, time.monthDay, (time.month + 1), time.year);
    float dateXOffset = computeXOffset(dateText, datePaint, bounds);
    float dateYOffset = computeDateYOffset(dateText, datePaint);
    canvas.drawText(dateText, dateXOffset, timeYOffset + dateYOffset, datePaint);




}

以下是该方法的调用方式

 @Override
    public void onAmbientModeChanged(boolean inAmbientMode) {
        super.onAmbientModeChanged(inAmbientMode);
        watchFace.setAntiAlias(!inAmbientMode);
        watchFace.setColor(Color.WHITE);
        watchFace.showAmbient(inAmbientMode);
        watchFace.setShowSeconds(false);
        invalidate();

        startTimerIfNecessary();
    }

始终绘制可绘制的交互模式。我不知道它为什么不改变。

1 个答案:

答案 0 :(得分:0)

我知道我们正在讨论这些问题:

if (shouldShowAmbient) {
    backgroundDrawable = resources.getDrawable(R.drawable.ambient_domo);
} else {
    backgroundDrawable = resources.getDrawable(R.drawable.domo);
}

您使用shouldShowAmbient字段,但它似乎无法在任何地方设置。 WatchFaceService.EngineisInAmbientMode()。您应该使用它来代替这种情况。