我对android动画有点陌生。我正在做一个项目,该项目将球的图片放置在随机位置 - 之后它将绕圈移动。到目前为止我已经成功了,但现在我想在不同的随机坐标处不断地绘制新的形状。我想过使用一个线程每隔几秒钟绘制一次形状,但如果不把所有东西都搞砸,我似乎无法实现它。
有谁知道我该如何解决这个问题?我也知道我每次都必须不断地重置我的随机坐标。有谁知道我该怎么做?谢谢你的帮助。我的代码如下:
public class DrawingTheBall extends View {
Bitmap bball;
int randX, randY;
double theta;
public DrawingTheBall(Context context) {
super(context);
// TODO Auto-generated constructor stub
bball = BitmapFactory.decodeResource(getResources(), R.drawable.blueball);
randX = 1 + (int)(Math.random()*500);
randY = 1 + (int)(Math.random()*500);
theta = 45;
}
public void onDraw(Canvas canvas){
super.onDraw(canvas);
//Radius, angle, and coordinates for circle motion
float a = 50;
float b = 50;
float r = 50;
int x = 0;
int y = 0;
theta = theta + Math.toRadians(2);
//move ball in circle
if(x < canvas.getWidth()){
x = randX + (int) (a +r*Math.cos(theta));
}else{
x = 0;
}
if(y < canvas.getHeight()){
y = randY + (int) (b +r*Math.sin(theta));
}else{
y = 0;
}
Paint p = new Paint();
//Implement Thread here
thread = new Thread(new Runnable(){
@Override
public void run(){
for(int j = 0; j <= 60; j++){
//It tells me to change variables to Final
//But if I do that it messes up my if statements above
canvas.drawBitmap(bball, x, y, p);
}
};
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //wait one second
}
}
});
thread.start();
//canvas.drawBitmap(bball, x, y, p);
invalidate();
}
}