0

So I have a ball class. The ball gets created, and starts to drop. When the ball is created I'd like it to be any color between Red, Yellow, Green and Purple. How would one go about it. I created a ball already but the only color is red.

Ball.java:

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.view.View;
import android.view.ViewTreeObserver;

public class RedBall extends View{

    // Ball attributes
    private float ballRadius = 30;
    private float ballX = 50;
    private float ballY = 50;
    private float ballSpeed = 10.0f;
    private RectF ballBounds;
    private Paint ballColor;
    boolean doBallAnimation = false;
    // For game elements
    private int score = 0;
    ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
    long startTime, prevTime; // Used to track elapsed time for animations and fps

    public RedBall(Context context){
        super(context);
        // Initialize game elements
        ballBounds = new RectF();
        ballColor = new Paint();
        this.setFocusableInTouchMode(true);

        getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener(){
                    @Override
                    public void onGlobalLayout(){
                        getViewTreeObserver().removeOnGlobalLayoutListener(this);

                        ballY = 0;
                        ballX = (getWidth()- ballRadius) / 2.0f;

                        doBallAnimation = true;
                        animator.start();
                    }
                }
        );

        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener(){
            @Override
            public void onAnimationUpdate(ValueAnimator arg0){
                long nowTime = System.currentTimeMillis();
                float secs = (float)(nowTime - prevTime) / 1000f;
                prevTime = nowTime;

                if((ballY + ballSpeed) > (getHeight() - (ballRadius)))
                {
                    animator.cancel();
                    return;
                }

                ballY += ballSpeed;

                // Force a redraw to see the ball in its new position
                invalidate();
            }
        });
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.setDuration(3000);
    }

    public void onDraw(Canvas canvas){
        // Draw ball
        if(doBallAnimation)
        {
            ballBounds.set(ballX - ballRadius, ballY - ballRadius, ballX + ballRadius, ballY + ballRadius);
            ballColor.setColor(Color.RED);
            //private Color[] colors = {Color.RED, Color.GREEN, Color.YELLOW, Color.rgb(255, 0, 255)};

            canvas.drawOval(ballBounds, ballColor);
        }
    }
}
cookiemonster
  • 55
  • 1
  • 9

3 Answers3

0

You can use Random Class to generate random colors by using Color.rgb(int,int,int) function where int can be between 0 to 255

    Random rand=new Random();
    Paint paint=new Paint();
    paint.setColor(Color.rgb(rand.nextInt(256),rand.nextInt(256),rand.nextInt(256)));//Will generate random color
Sanjeev
  • 4,255
  • 3
  • 28
  • 37
0

In case you need to have any color from range

You have to use HSB color scheme then:

Random rand = new Random(); // you can find suitable way following link below 

float h = rand.nextFloat() * 0.64f + 0.71f; //Here a range from Green to Purple hardcoded
float s = 1.0f;
float b = 1.0f;

ballColor.setColor(Color.getHSBColor(h, s, b));
canvas.drawOval(ballBounds, ballColor);

In case you need to choose from array of defined colors:

Easiest way is to generate random index at which you will get color from array:

private Color[] colors = {Color.RED, Color.GREEN, Color.YELLOW, Color.rgb(255, 0, 255)};
ballColor.setColor(colors[rand.nextInt(colors.length)]);
canvas.drawOval(ballBounds, ballColor);
Community
  • 1
  • 1
0

at the moment you are setting the ball to red every call to draw. you need to set the colour when you first create the ball and you also need to use a random number generator.

use

Random r = new Random();
int mColorNo = r.nextInt(4 - 1) + 1;

then either have a an if statement to query what mColourNo is

  if(mColorNo == 1)
     ballColor.setColor(Red)
  if(mColorNo == 2)
     ballColor.setColor(Blue)

etc

or store the color values in an array that you can refer to

Rob85
  • 1,719
  • 1
  • 23
  • 46