簡體   English   中英

Android動畫的簡單線程問題

[英]Simple thread issue with Android animation

我正在嘗試用一些簡單的Android動畫實現一個線程。 我只是在睡眠中遇到錯誤() - 它說我需要一個方法。 我知道可能有一個明顯的解決方案。 我的應用程序只是放置一個隨機位置移動的球。 我想要的是繼續在隨機位置放置形狀。 無論如何,有人可以告訴我我的線程做錯了嗎? 謝謝。

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();   

}}

如果實現Thread或HandlerThread,請確保在等待工作線程完成時UI線程不會阻塞 - 不要調用Thread.wait()Thread.sleep()

http://developer.android.com/training/articles/perf-anr.html

在等待工作線程完成時,主線程應該為其他線程提供一個Handler ,以便在完成時回發。

使用處理程序

   Handler m_handler;
   Runnable m_handlerTask ;  
   m_handler = new Handler();
   m_handlerTask = new Runnable()
   {
     @Override 
     public void run() {

    // do something 
    m_handler.postDelayed(m_handlerTask, 1000); // instad of 1000 mention the delay in milliseconds
     }
   };
   m_handlerTask.run(); 

當你需要取消處理程序時,使用m_handler.removeCallbacks(m_handlerTask);

例:

 public class MainActivity extends Activity {

RelativeLayout rl;
int x = 0,y=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    rl = (RelativeLayout) findViewById(R.id.rl);
    CustomView cv = new CustomView(this);
    rl.addView(cv);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public class CustomView extends View {

    Bitmap bball; 
    Random randX, randY;
    double theta;
    Handler m_handler;
    Paint p ;

    int width;
    int height;

    Runnable m_handlerTask;  


    public CustomView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        p= new Paint();
        bball = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);  
        //randX = 1 + (int)(Math.random()*500); 
        //randY = 1 + (int)(Math.random()*500);
        randX = new Random();
        randY = new Random();
        theta = 45;
        m_handler = new Handler();   
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        // TODO Auto-generated method stub
        super.onSizeChanged(w, h, oldw, oldh);
        width =w;
        height=h; 
    }

    public void move()
    {
        m_handlerTask = new Runnable()
           {
             @Override 
             public void run() {
                 //theta = theta + Math.toRadians(2);

                  if(x<300)
                   {
                       x= x+10;
                       invalidate();
                   }
          else if(x>300)
           {
               x=0;
               m_handler.removeCallbacks(m_handlerTask);

           }
          // canvas.drawBitmap(bball, x, y, p);
            m_handler.postDelayed(m_handlerTask, 3000); 

             }
           };
           m_handlerTask.run();  

    }

    public void onDraw(final Canvas canvas){
        super.onDraw(canvas);
       canvas.drawBitmap(bball, x, y, p);  
       if(x<300)
           {
           move();
           }
       else 
       {

           m_handler.removeCallbacks(m_handlerTask);

       }


    }


}
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM