garsim
garsim

Reputation: 160

Android Wear: animation in WatchFace

I would to create an animated WatchFace for Android Wear. I've 20 images to add (or to change completely) every X ms to the background.

Now: i've followed this tutorial but the animation doesn't start. I see only one of the twenty bitmap over my background:

if (isInAmbientMode()) {
        canvas.drawBitmap(mBackgroundAmbient, SRC, DEST, null);
} else {
       canvas.drawBitmap(mBackground, SRC, DEST, null);
       for (int i = 0; i < LoopBMP.length; i++) {
            canvas.save();

            Bitmap cloud = LoopBMP[i];
            canvas.drawBitmap(cloud,centerX, centerY,null);
            canvas.restore();
       }
 }

Any suggestion?

Upvotes: 1

Views: 852

Answers (1)

Sterling
Sterling

Reputation: 6635

You're misunderstanding how the CanvasWatchFaceService.Engine does its drawing. I'm guessing that the code snippet you posted is in your onDraw method; this method is called once for each frame of your animation.

Meaning that you need to move your animation "loop" outside of the onDraw method. There are several ways to accomplish this, but I've dummied up one below based on your code.

private int i;

@Override
public void onDraw(Canvas canvas, Rect bounds) {
    super.onDraw(canvas, bounds);

    // probably other code here

    if (isInAmbientMode()) {
        canvas.drawBitmap(mBackgroundAmbient, SRC, DEST, null);
    } else {
        canvas.drawBitmap(mBackground, SRC, DEST, null);
        if (i < LoopBMP.length) {
            canvas.save();
            Bitmap cloud = LoopBMP[i];
            canvas.drawBitmap(cloud,centerX, centerY,null);
            canvas.restore();
            i++;
            // probably want an X-ms delay here to time the animation
            invalidate();
        } else {
            i = 0;
        }
    }

    // probably other code here
}

Note that this is a snippet I just threw together to demonstrate what I'm talking about; it's by no means ready-to-run. In particular, you will want a delay between frames of your animation; you can implement that with a Handler like the one used for the second hand in this sample: http://developer.android.com/samples/WatchFace/Wearable/src/com.example.android.wearable.watchface/AnalogWatchFaceService.html#l117

Upvotes: 1

Related Questions