实现机器人精确的时序 [英] Achieving accurate timing in android

查看:159
本文介绍了实现机器人精确的时序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在android系统1秒至60显示不同的颜色。我怎么可以在1秒做到这一点显示所有颜色的持续时间(16毫秒)是一样的吗?

I want to display 60 different colors in 1 second in android. How I can do this that the duration of display(16ms) of all colors is same during 1 second?

推荐答案

您需要什么可以在自定义实现查看,你叫无效()每个的onDraw()调用:

What you need can be achieved in a custom View where you call invalidate() on each onDraw() invocation:

int framesToRedraw = 0;


public void startAnimation(int frames){
     framesToRedraw = frames;
     this.invalidate();
}


/**
 * onDraw override.
 * If animation is "on", view is invalidated after each redraw
 * to make android redraw it on the next frame
 */
@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (framesToRedraw > 0) {
         // generate new color randomly
         float[] hsvColor = {0, 1, 1};
         hsvColor[0] = random.nextFloat() * 360f;
         this.setBackgroundColor(Color.HSVToColor(hsvColor));
         framesToRedraw--;
         this.invalidate();  // force the view to be redrawn on each frame
    } 
}

这篇关于实现机器人精确的时序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆