0

I am tring to make a circle pop after 3 seconds and disappear in 3 seconds in a loop. this program pop the circle after 3 seconds but I have problem with the 3 seconds duration beteen.

it will be great if some one will have a solution for me

public class TestView extends View{

private boolean isPop;

public TestView(Context context) {
    super(context);

    isPop=false;
    // TODO Auto-generated constructor stub
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawColor(Color.RED);

    Paint circle = new Paint();
    circle.setColor(Color.BLUE);
    if (isPop){
        canvas.drawCircle(100, 100, 40, circle);
    }
    invalidate();

    CountDownTimer count = new CountDownTimer(3000, 1000) {

        public void onTick(long millisUntilFinished) {
        }

        public void onFinish() {

            CountDownTimer count = new CountDownTimer(3000, 1000) {

                public void onTick(long millisUntilFinished) {
                    isPop=true;

                }

                public void onFinish() {

                    isPop=false;

                }

             }.start();             
        }

     }.start();
4

1 回答 1

3

Consider using the Handler that comes with every View instead:

class TestView extends View { 
    private Paint circle = new Paint();
    private boolean isPop = false;
    private Runnable everyThreeSeconds = new Runnable() {
        public void run() {
            // Do something spiffy like...
            isPop = !isPop;
            invalidate();

            // Don't forget to call you next three second interval!
            postDelayed(everyThreeSeconds, 3000);
        }
    };

    public TestView(Context context) {
        this(context, null);
    }

    public TestView(Context context, AttributeSet attrs) {
        super(context, attrs);
        circle.setColor(Color.BLUE);
        post(everyThreeSeconds);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.RED);

        if (isPop){
            canvas.drawCircle(100, 100, 40, circle);
        }
    }
}
于 2012-10-16T19:50:18.960 回答