Skip to content Skip to sidebar Skip to footer

Simple Thread Issue With Android Animation

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 so

Solution 1:

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.

Using a Handler

Handler m_handler;
   Runnable m_handlerTask ;  
   m_handler = newHandler();
   m_handlerTask = newRunnable()
   {
     @Overridepublicvoidrun() {

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

When you require to cancel the handler use m_handler.removeCallbacks(m_handlerTask);

Example:

publicclassMainActivityextendsActivity {

RelativeLayout rl;
intx=0,y=0;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    rl = (RelativeLayout) findViewById(R.id.rl);
    CustomViewcv=newCustomView(this);
    rl.addView(cv);
}

@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    returntrue;
}

publicclassCustomViewextendsView {

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

    int width;
    int height;

    Runnable m_handlerTask;  


    publicCustomView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        p= newPaint();
        bball = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);  
        //randX = 1 + (int)(Math.random()*500); //randY = 1 + (int)(Math.random()*500);
        randX = newRandom();
        randY = newRandom();
        theta = 45;
        m_handler = newHandler();   
    }

    @OverrideprotectedvoidonSizeChanged(int w, int h, int oldw, int oldh) {
        // TODO Auto-generated method stubsuper.onSizeChanged(w, h, oldw, oldh);
        width =w;
        height=h; 
    }

    publicvoidmove()
    {
        m_handlerTask = newRunnable()
           {
             @Overridepublicvoidrun() {
                 //theta = theta + Math.toRadians(2);if(x<300)
                   {
                       x= x+10;
                       invalidate();
                   }
          elseif(x>300)
           {
               x=0;
               m_handler.removeCallbacks(m_handlerTask);

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

             }
           };
           m_handlerTask.run();  

    }

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

           m_handler.removeCallbacks(m_handlerTask);

       }


    }


}
 }

Post a Comment for "Simple Thread Issue With Android Animation"