Prosty problem z wątkami z animacją na Androida
Próbuję zaimplementować wątek za pomocą prostej animacji na Androida. Właśnie otrzymuję błąd podczas snu () - mówi, że potrzebuję do tego metody. Wiem, że istnieje prawdopodobnie oczywiste rozwiązanie. Moja aplikacja po prostu umieszcza piłkę, która porusza się po okręgu w przypadkowej lokalizacji. Chcę nadal umieszczać kształty w przypadkowych miejscach. W każdym razie, czy ktoś może powiedzieć, co robię źle z moim wątkiem? Dzięki.
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();
}}