简体   繁体   中英

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. 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():

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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