与Android动画简单的线程问题 [英] Simple thread issue with Android animation

查看:266
本文介绍了与Android动画简单的线程问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一些简单的机器人动画的线程。我刚开始睡眠()的错误 - 它说我需要它的方法。我知道有可能是一个显而易见的解决方案。我的应用程序只是放置一个球,在任意位置移动了一圈。我想要的是继续把形状在随机位置。不管怎么说,能有人告诉我在做什么毛病我的线程?谢谢你。

I'm trying to implement a thread with some simple Android animation. I'm just getting an error with sleep() - It says I need a method for it. I know there is probably an obvious solution. My app just places a ball that moves in a circle at a random location. What I want is to continue placing shapes at random locations. Anyway, can someone please tell what I'm doing wrong with my thread? Thanks.

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

} }

推荐答案

如果您实现线程或HandlerThread,请确保您的UI线程不会阻塞在等待工作线程来完成,不叫 Thread.wait()视频下载()

If you implement Thread or HandlerThread, be sure that your UI thread does not block while waiting for the worker thread to complete—do not call Thread.wait() or Thread.sleep().

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

而不是堵在等待工作线程来完成的,你的主线程应该为其他线程提供了一个处理程序回发到完成时。

Instead of blocking while waiting for a worker thread to complete, your main thread should provide a Handler for the other threads to post back to upon completion.

通过处理器

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

When you require to cancel the handler use 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);

       }


    }


}
 }

这篇关于与Android动画简单的线程问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆