我正在尝试使用一些简单的 Android 动画来实现一个线程。我刚刚收到 sleep() 错误 - 它说我需要一个方法。我知道可能有一个明显的解决方案。我的应用程序只是在随机位置放置一个在圆圈中移动的球。我想要的是继续在随机位置放置形状。无论如何,有人可以告诉我我的线程做错了什么吗?谢谢。
public class DrawingTheBall extends View {
Bitmap bball;
Random randX, randY;
double theta;
Handler m_handler;
Runnable m_handlerTask; //for some reason I get a syntax error here
m_handler = new Handler();
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);
randX = new Random();
randY = new Random();
theta = 45;
new Thread(this).start();
}
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.nextInt() + (int) (a +r*Math.cos(theta));
}else{
x = 0;
}
if(y < canvas.getHeight()){
y = randY.nextInt() + (int) (b +r*Math.sin(theta));
}else{
y = 0;
}
Paint p = new Paint();
}
m_handlerTask = new Runnable()
{
@Override
public void run() {
// do something
m_handler.postDelayed(m_handlerTask, 1000);
invalidate();
}
};
m_handlerTask.run();
} }