0

我已经在 canvas 方法中创建了对象。但是我需要有关如何创建数组列表并将这四个对象添加到数组列表中的帮助,以便它们不断出现。我已经查看了arraylist Heres 代码,任何帮助将不胜感激。

public class BouncingBallView extends View {

    private int xMin = 0;          // This view's bounds
    private int xMax;
    private int yMin = 0;
    private int yMax;
    private int xMin1 = 0;          // This view's bounds
    private int xMax1;
    private int yMin1 = 0;
    private int yMax1;
    private int xMin2 = 0;          // This view's bounds
    private int xMax2;
    private int yMin2 = 0;
    private int yMax2;
    private int xMin3 = 0;          // This view's bounds
    private int xMax3;
    private int yMin3 = 0;
    private int yMax3;

    private float ballRadius = 80;
    private float ballRadius2 = 80;// Ball's radius
    private float ballX = ballRadius + 20;  // Ball's center (x,y)
    private float ballY = 10;//ballRadius + 40;
    private float ballX1= ballRadius2 + 30;
    private float ballY1 = 15;
    private float ballX2 = ballRadius + 20;  // Ball's center (x,y)
    private float ballY2 = 10;//ballRadius + 40;
    private float ballX3 = ballRadius + 20;  // Ball's center (x,y)
    private float ballY3 = 10;//ballRadius + 40;
    private float ballSpeedX = 50;  // Ball's speed (x,y)
    private float ballSpeedX1= 25;
    private float ballSpeedY = 30;
    private float ballSpeedY1= 15;
    private float ballSpeedX2 = 40;  // Ball's speed (x,y)
    private float ballSpeedX3= 20;
    private float ballSpeedY2 = 20;  // Ball's speed (x,y)
    private float ballSpeedY3= 10;
    private RectF ballBounds;      // Needed for Canvas.drawOval
    private RectF ballBounds2;
    private RectF ballBounds3;  
    private RectF ballBounds4;  
    private Paint paint;           // The paint (e.g. style, color) used for drawing

    // Constructor
    public BouncingBallView(Context context) {
        super(context);
        ballBounds = new RectF();
        ballBounds2 = new RectF();
        ballBounds3 = new RectF();
        ballBounds4 = new RectF();
        paint = new Paint();
    }

    // Called back to draw the view. Also called by invalidate().
    @Override
    protected void onDraw(Canvas canvas) {
        ballBounds2.set(10, ballY1, 50, ballY1+40);
        paint.setColor(Color.BLUE);
        canvas.drawRoundRect(ballBounds2,6,6, paint);
        // Draw the ball
        ballBounds.set(10, ballY, 50, ballY+40);
        paint.setColor(Color.RED);
        canvas.drawRoundRect(ballBounds,6,6, paint);

        ballBounds3.set(10, ballY2, 50, ballY2+40);
        paint.setColor(Color.YELLOW);
        canvas.drawRoundRect(ballBounds3,6,6, paint);

        ballBounds4.set(10, ballY3, 50, ballY3+40);
        paint.setColor(Color.GREEN);
        canvas.drawRoundRect(ballBounds4,6,6, paint);

        // Update the position of the ball, including collision detection and reaction.
        update();

        // Delay
        try {  
            Thread.sleep(30);  
        } catch (InterruptedException e) { }      
        invalidate();  // Force a re-draw
    }


    // Detect collision and update the position of the ball.

    private void update() {
        // Get new (x,y) position
        //ballX += ballSpeedX;
        ballY += ballSpeedY;
        ballY1 += ballSpeedY1;
        ballY2 += ballSpeedY2;
        ballY3 += ballSpeedY3;
        // Detect collision and react
        //  if (ballX + ballRadius > xMax) {
        //   ballSpeedX = -ballSpeedX;
        // ballX = xMax-ballRadius;
        //   } 
        // else if (ballX - ballRadius < xMin) {
        //     ballSpeedX = -ballSpeedX;
        //   ballX = xMin+ballRadius;
        // }

        if (ballY + ballRadius > yMax) {
            ballSpeedY = -ballSpeedY;
            ballY = yMax - ballRadius;
        } else if (ballY - ballRadius < yMin) {
            ballSpeedY = -ballSpeedY;
            ballY = yMin + ballRadius;
        }
        if (ballY1 + ballRadius2 > yMax1) {
            ballSpeedY1 = -ballSpeedY1;
            ballY1 = yMax1 - ballRadius2;
        } else if (ballY1 - ballRadius2 < yMin1) {
            ballSpeedY1 = -ballSpeedY1;
            ballY1 = yMin1 + ballRadius2;
        }
        if (ballY2 + ballRadius2 > yMax2) {
            ballSpeedY2 = -ballSpeedY2;
            ballY2 = yMax2 - ballRadius2;
        } else if (ballY2 - ballRadius2 < yMin2) {
            ballSpeedY2 = -ballSpeedY2;
            ballY2 = yMin2 + ballRadius2;
        }
        if (ballY3 + ballRadius2 > yMax3) {
            ballSpeedY3 = -ballSpeedY3;
            ballY3 = yMax3 - ballRadius2;
        } else if (ballY3 - ballRadius2 < yMin3) {
            ballSpeedY3 = -ballSpeedY3;
            ballY3 = yMin3 + ballRadius2;
        }
    }

    // Called back when the view is first created or its size changes.
    @Override
    public void onSizeChanged(int w, int h, int oldW, int oldH) {
        // Set the movement bounds for the ball
        xMax = w-1;
        yMax = h-1;
        xMax1= w-1;
        yMax1= h-1;
        xMax2 = w-1;
        yMax2 = h-1;
        xMax3 = w-1;
        yMax3 = h-1;
    }
}
4

1 回答 1

0

此代码位于您的构造函数中。

ArrayList<RectF> ballBoundArray = new ArrayList<RectF>();
ballBoundArray.add(ballBounds);
ballBoundArray.add(ballBounds2);
ballBoundArray.add(ballBounds3);
ballBoundArray.add(ballBounds4);

编辑:如果您希望在构造函数之外可以访问数组列表,请将其定义为具有其他球边界的私有或公共变量。

于 2013-04-15T22:08:16.003 回答