0

3秒後に円が飛び出し、3秒で消えるループを作っています。このプログラムは 3 秒後に円をポップしますが、3 秒間隔に問題があります。

誰かが私のために解決策を持っているなら、それは素晴らしいことです

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

代わりに、すべてのビューに付属する Handler を使用することを検討してください。

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 に答える