简体   繁体   中英

sleep method doesn't working if i call second time?

Here i am actually trying to do a simple animation. A circle will getting smaller and smaller with a particular speed. I used sleep method to set interval between each time of reducing the radius of the circle. I gave five seconds for sleep method. But the problem is , it is working for first 5 seconds after launching the app and it is not working second time. It is reducing faster after 5 seconds.

I want to reduce the radius of the circle in particular interval of time. Please tell me how can i implement my idea in my code. Also tell me what mistake i did in my code.

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class MyView extends View {
    int radius=100;

 boolean freeTouched = false;
 Path freePath;

 public MyView(Context context) {
  super(context);
 }

 public MyView(Context context, AttributeSet attrs) {
  super(context, attrs);
 }

 public MyView(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
 }
 Thread th=new Thread()
 {    
     public void run()
 {
     for(int i=100;i>0;i-=2){
         radius=i;
         try {
            sleep(500);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         invalidate();
     }
     }
 };
 public void calThread(){

     Thread th=new Thread(){
         public void run(){
             try {
                sleep(5000); radius-=1; Log.i("kbt","inside thread 800 seconds");
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
         }
     };th.start();
    invalidate();
     Log.i("kbt", "Inside thread");
 }





@Override
 protected void onDraw(Canvas canvas) {
  // TODO Auto-generated method stub

  super.onDraw(canvas);//th.start();
  Paint paint = new Paint();
  paint.setStyle(Paint.Style.STROKE);
  paint.setColor(Color.WHITE);
  paint.setStrokeWidth(3);
  canvas.drawCircle(getWidth()/2, getHeight()/2, radius, paint);
 calThread();
 Log.i("kbt", "Inside clled");

 }

 @Override
 public boolean onTouchEvent(MotionEvent event) {

  switch(event.getAction()){
  case MotionEvent.ACTION_UP:
   freeTouched = false;
   break;
  case MotionEvent.ACTION_DOWN:

     freeTouched = true;
    freePath = new Path();
    freePath.moveTo(event.getX(), event.getY()); 

    break;
  case MotionEvent.ACTION_MOVE:
   freePath.lineTo(event.getX(), event.getY());
   invalidate();
   break;
  }

  return true;
 }

 }

Your calThread() method gets called repeatedly before your sleep() method calling.

Since you repeatedly calling your calThread() method, it is not allowing you to access any one the code below it.

Move the function call below or at the end and then you can able to call sleep() function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM