我会为安卓服装创建一个动画WatchFace。我有20个图片要添加(或完全改变)每X毫秒的背景。
现在:我已经跟踪了本教程,但是动画还没有开始。在我的背景中,我只看到了二十张位图中的一张:
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();
}
}有什么建议吗?
发布于 2016-04-13 16:55:01
你误解了CanvasWatchFaceService.Engine是如何绘制的。我猜您发布的代码片段在您的onDraw方法中;对于动画中的每一个帧,这个方法被称为一次。
这意味着您需要将动画“循环”移到onDraw方法之外。有几种方法可以实现这一点,但我已经根据您的代码在下面介绍了一种方法。
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
}请注意,这是我刚才拼凑在一起演示我所讲的内容的一个片段;它绝不是准备好运行的。特别是,您需要在动画帧之间延迟;您可以使用一个Handler来实现这个延迟,就像这个示例中的第二个例子:http://developer.android.com/samples/WatchFace/Wearable/src/com.example.android.wearable.watchface/AnalogWatchFaceService.html#l117
https://stackoverflow.com/questions/36568397
复制相似问题