2013-04-15 2 views
0

캔버스 메소드에 이미 객체를 만들었습니다. 그러나 arraylist를 만드는 방법에 대한 도움이 필요하며 arraylist에게이 네 가지 객체를 추가하여 계속해서 나타납니다. 나는 arraylist Heres 코드를 보았습니다. 어떤 도움을 주시면 대단히 감사하겠습니다.생성 된 객체를 android의 arraylist에 추가하는 방법

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; 
    } 
} 
+0

캔버스 방법이 표시되지 않습니다. –

답변

0

이 코드는 생성자 내부에 있습니다.

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

편집 : 당신이 배열 목록 생성자의 외부 액세스 할 수 있도록하려면 다른 ballbounds 수있는 전용 또는 공용 변수로 정의합니다.

관련 문제