简体   繁体   English

Android-每隔几秒钟更改一次球的颜色

[英]Android - Change color of a ball every few seconds

I'm still new to android and at the moment I'm working on this app just for practice but one of the issues I'm struggling with is having the ball in the app change colors every few seconds or so, like 3 or 6 seconds around there, problem is the code I use works at first but then after the few second delay it reverts back to changing different colors without waiting. 我还是Android的新手,目前我只是在练习该应用程序,但我苦苦挣扎的问题之一是让应用程序中的球每隔几秒钟就更改一次颜色,例如3或6大约在几秒钟的时间里,问题是我使用的代码起初可以工作,但是几秒钟的延迟之后,它又恢复为更改不同的颜色,而无需等待。 I feel silly for asking this question again since last time I didn't the answer I needed and also since its kinda like the basics of it, and may not be as big of a deal as I assume it maybe. 上次我没有提供所需的答案,而且因为它有点像它的基本知识,所以我再次提出这个问题感到很傻,而且可能没有我想像的那么重要。 would appreciate any help? 会有所帮助吗?

   protected void onDraw(Canvas canvas) {
 // TODO Auto-generated method stub
        ballBounds.set(ballX-ballRadius, ballY-ballRadius, ballX+ballRadius,ballY+ballRadius);
        Handler handler = new Handler();        
        int rnd = (int)(Math.random() * 4);
        switch(rnd){
        case 0:handler.postDelayed(new Runnable(){
            public void run(){
            paint.setColor(Color.BLUE);
            }
        }, 3000);
            break;
        case 1: handler.postDelayed(new Runnable(){
            public void run(){
            paint.setColor(Color.RED);
            }
        }, 3000);
            break;
        case 2: handler.postDelayed(new Runnable(){
            public void run(){
            paint.setColor(Color.GREEN);
            }
        }, 3000);
            break;
        case 3:handler.postDelayed(new Runnable(){
                public void run(){
                paint.setColor(Color.YELLOW);
                }
            }, 3000);
            break;
            }
        canvas.drawOval(ballBounds, paint);

    update();

    try{
        Thread.sleep(20);
    }catch(InterruptedException e){}

    invalidate();

} }

Put your case statement into 1 runnable, but it needs a better home than onDraw(): 将您的case语句放入1个可运行的语句中,但它需要一个比onDraw()更好的宿主:

protected void onDraw(Canvas canvas) {
        ballBounds.set(ballX - ballRadius, ballY - ballRadius, ballX + ballRadius, ballY + ballRadius);
        Handler handler = new Handler();
        int rnd = (int) (Math.random() * 4);
        handler.postDelayed(new Runnable() {
            public void run() {
                switch (rnd) {
                    case 0:
                        paint.setColor(Color.BLUE);
                        break;

                    case 1:
                        paint.setColor(Color.RED);
                        break;

                    case 2:
                        paint.setColor(Color.GREEN);
                        break;

                    case 3:
                        paint.setColor(Color.YELLOW);
                        break;
                }
            }
        }, 3000);
        canvas.drawOval(ballBounds, paint);
        update();
        try {
            Thread.sleep(20);
        } catch (InterruptedException e) {
        }
        invalidate();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM