0

i would like to ask how would you change the background drawable when the android wear is in ambient mode?

Here are my code

 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);




}

and here is how the method is called

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

        startTimerIfNecessary();
    }

It is always drawing the interactive mode drawable. I have no idea why it doesnt change over.

EggRollMan
  • 583
  • 1
  • 8
  • 20

1 Answers1

0

I understand we are talking about these lines:

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

You use shouldShowAmbient field, but it doesn't seem it is set anywhere. WatchFaceService.Engine already isInAmbientMode(). You should use that instead for this condition.

gruszczy
  • 40,948
  • 31
  • 128
  • 181