Java 如何在android中将创建的对象添加到arraylist
我已经在canvas方法中创建了对象。但是我需要关于如何创建arraylist以及如何将这四个对象添加到arraylist中的帮助,以便它们不断出现。我已经看过了代码,任何帮助都将不胜感激Java 如何在android中将创建的对象添加到arraylist,java,android,arraylist,Java,Android,Arraylist,我已经在canvas方法中创建了对象。但是我需要关于如何创建arraylist以及如何将这四个对象添加到arraylist中的帮助,以便它们不断出现。我已经看过了代码,任何帮助都将不胜感激 public class BouncingBallView extends View { private int xMin = 0; // This view's bounds private int xMax; private int yMin = 0;
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;
}
}
公共类BouncingBallView扩展视图{
private int xMin=0;//此视图的边界
私有int-xMax;
私有int-yMin=0;
私人国际货币基金组织;
private int xMin1=0;//此视图的边界
私有intxmax1;
私有int yMin1=0;
私人互联网1;
private int xMin2=0;//此视图的边界
私有intxmax2;
私有int yMin2=0;
私人住宅;
private int xMin3=0;//此视图的边界
私有intxmax3;
私有int yMin3=0;
私人互联网3;
专用浮球半径=80;
专用浮球半径2=80;//球的半径
私人浮球x=球半径+20;//球的中心(x,y)
私人浮球Y=10;//球半径+40;
专用浮球X1=球半径2+30;
私人浮球1=15;
私人浮球X2=球半径+20;//球的中心(x,y)
私人浮球2=10;//球半径+40;
私人浮球X3=球半径+20;//球的中心(x,y)
私人浮球y3=10;//球半径+40;
私人浮球速度x=50;//球的速度(x,y)
私人浮球速度x1=25;
私人浮球=30;
私人浮球速度1=15;
私人浮球速度x2=40;//球的速度(x,y)
私人浮球速度x3=20;
私人浮球速度2=20;//球的速度(x,y)
私人浮球速度y3=10;
private RectF ballBounds;//Canvas.drawOval需要
私有RectF-ballbunds2;
私有RectF Ballbunds3;
私有RectF Ballbunds4;
私有油漆;//用于绘图的油漆(例如样式、颜色)
//建造师
公共BouncingBallView(上下文){
超级(上下文);
ballBounds=新的RectF();
Ballbunds2=新的RectF();
ballbunds3=新的RectF();
ballbunds4=新的RectF();
油漆=新油漆();
}
//回调以绘制视图。也由invalidate()调用。
@凌驾
受保护的void onDraw(画布){
球边界2.集合(10,球1,50,球1+40);
油漆。设置颜色(颜色。蓝色);
画布.drawRoundRect(Ballbunds2,6,6,油漆);
//抽签
球界设定(10,球界,50,球界+40);
油漆。设置颜色(颜色。红色);
画布.drawRoundRect(球边,6,6,油漆);
球边界3.组(10,球2,50,球2+40);
油漆。设置颜色(颜色。黄色);
画布.drawRoundRect(Ballbunds3,6,6,油漆);
球边界4.组(10,球3,50,球3+40);
油漆。设置颜色(颜色。绿色);
画布.drawRoundRect(圆球边4,6,6,油漆);
//更新球的位置,包括碰撞检测和反应。
更新();
//耽搁
试试{
睡眠(30);
}捕获(中断异常e){}
invalidate();//强制重新绘制
}
//检测碰撞并更新球的位置。
私有void更新(){
//获得新的(x,y)位置
//ballX+=ballSpeedX;
ballY+=ballY;
ballY1+=ballSpeedY1;
ballY2+=ballSpeedY2;
ballY3+=ballSpeedY3;
//检测碰撞并作出反应
//如果(球X+球半径>X最大){
//ballSpeedX=-ballSpeedX;
//ballX=xMax ballRadius;
// }
//否则如果(球x-球半径yMax){
鲍尔斯皮蒂=-鲍尔斯皮蒂;
ballY=yMax-球半径;
}else if(球半径yMax1){
ballSpeedY1=-ballSpeedY1;
ballY1=yMax1-ballRadius2;
}else if(ballY1-ballRadius2yMax2){
ballSpeedY2=-ballSpeedY2;
ballY2=yMax2-ballRadius2;
}else if(ballY2-ballRadius2yMax3){
ballSpeedY3=-ballSpeedY3;
ballY3=yMax3—ballRadius2;
}else if(ballY3-ballRadius2
此代码进入构造函数内部
ArrayList<RectF> ballBoundArray = new ArrayList<RectF>();
ballBoundArray.add(ballBounds);
ballBoundArray.add(ballBounds2);
ballBoundArray.add(ballBounds3);
ballBoundArray.add(ballBounds4);
ArrayList ballbundarray=new ArrayList();
ballbundarray.add(ballbunds);
ballbundarray.add(ballbunds2);
添加(ballbunds3);
ballbundarray.add(ballbunds4);
编辑:如果希望数组列表在构造函数外部可访问,请使用其他ballbounds将其定义为私有或公共变量。我没有看到canvas方法。