我有一个任务。它是在屏幕上绘制一些(多个)圆圈。单击它们后,它们必须开始移动。我只有一个圆圈的代码。给我怎么做这个任务的方法,比如5圈。提前致谢!
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
class MyView extends View {
//public Paint c;
public Paint p;
private static final int RADIUS = 46;
private int centerX;
private int centerY;
private int speedX = 50;
private int speedY = 40;
//private Paint paint; // Создай его где-нибудь там в конструкторе
public MyView(Context context) {
super(context);
p = new Paint();
p.setColor(Color.GREEN);
}
@Override
protected void onSizeChanged(int w, int h, int oldW, int oldH) {
centerX = w / 2;
centerY = h / 2;
}
protected void onDraw(Canvas c) {
int w = getWidth();
int h = getHeight();
centerX += speedX;
centerY += speedY;
int rightLimit = w - RADIUS;
int bottomLimit = h - RADIUS;
if (centerX >= rightLimit) {
centerX = rightLimit;
speedX *= -1;
}
if (centerX <= RADIUS) {
centerX = RADIUS;
speedX *= -1;
}
if (centerY >= bottomLimit) {
centerY = bottomLimit;
speedY *= -1;
}
if (centerY <= RADIUS) {
centerY = RADIUS;
speedY *= -1;
}
c.drawCircle(centerX, centerY, RADIUS, p);
postInvalidateDelayed(200);
}
}
}