创建时使用随机颜色创建2D球

时间:2015-09-19 18:57:09

标签: java android animation

所以我有一个球类。球被创造,并开始下降。当球被创造时,我希望它是红色,黄色,绿色和紫色之间的任何颜色。怎么会去做呢。我已经创造了一个球,但唯一的颜色是红色。

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

3 个答案:

答案 0 :(得分:0)

您可以使用Random类通过Color.rgb(int,int,int)函数生成随机颜色,其中int可以介于0到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

答案 1 :(得分:0)

如果您需要来自范围的任何颜色

您必须使用HSB配色方案:

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

如果您需要从定义的颜色数组中进行选择:

最简单的方法是generate random index,你将从中获得颜色:

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

答案 2 :(得分:0)

此时你正在将每个召唤的球都设置为红色。你需要在第一次创建球时设置颜色,你还需要使用随机数生成器。

使用

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

然后要么有一个if语句来查询mColourNo是什么

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

或将颜色值存储在您可以参考的数组中

相关问题