具有时间延迟的AsyncTask onDraw方法

时间:2015-03-01 23:53:55

标签: android android-asynctask ondraw

我想进行一项活动,其中开头的用户将看到第一个语句,然后在2秒之后,将会有第二个语句,最后会出现在onDraw方法中在画布上绘制进度的计时器。

计时器应该只持续5秒,但是当我添加这两个2秒的休息时,用户将只看到最后一秒(5-2-2)。我试图改变计算进度的代码,但我只是让事情变得更糟。

我删除了变量声明和有关正在绘制的形状的所有内容。也许我应该在不同的时刻声明startTime和currentTime。

public class CircleTimer extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       final CircularCountdown view  = new CircularCountdown(this);
        setContentView(view);

        AsyncTask<Void, Boolean, Void> task = new AsyncTask<Void, Boolean, Void>() {

            @Override
            protected Void doInBackground(Void... params) {
                try {
                    Thread.sleep(2000);
                    publishProgress(true);
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                }
                return null;
            }

            protected void onProgressUpdate(Boolean... progress) {
                view.setSecondDrawStep(progress[0]);
                view.invalidate();
            }

            @Override
            protected void onPostExecute(Void result) {
                view.setThirdDrawStep(true);
                view.invalidate();
            }
        };
        task.execute((Void[])null);
    }

    private class CircularCountdown extends View {


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


            maxTime = 5000;

            startTime = System.currentTimeMillis();
            currentTime = startTime;

            viewHandler = new Handler();
            updateView = new Runnable(){
                @Override
                public void run(){
                    currentTime = System.currentTimeMillis();
                    progressMillisecond = Math.abs(startTime - currentTime) % maxTime;
                    progress = (double) (progressMillisecond) / maxTime;
                    CircularCountdown.this.invalidate();
                    viewHandler.postDelayed(updateView, 1000/60);
                }
            };
            viewHandler.post(updateView);
        }

        boolean secondDrawStep = false;
        boolean thirdDrawStep = false;

        public void setSecondDrawStep(boolean flag) {
            secondDrawStep = flag;
        }

        public void setThirdDrawStep(boolean flag) {
            thirdDrawStep = flag;
        }

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

            canvas.drawText("FIRST statement",
                    centerWidthCenter,
                    centerHeightCenter + textOffset,
                    nameTextPaint);

            if (secondDrawStep) {
                canvas.drawColor(Color.WHITE);

                canvas.drawText("SECOND statement",
                        centerWidthCenter,
                        centerHeightCenter + textOffset,
                        nameTextPaint);
            }

            if (thirdDrawStep) {
                canvas.drawColor(Color.WHITE);

                canvas.drawArc(circleBounds, -90, (float) (progress * 360), false, progressPaint);

                canvas.drawText((int) Math.abs(5 - (progressMillisecond / 1000)) + "s",
                        centerWidth,
                        centerHeight + textOffset,
                        textPaint);
            }

        }
    }

}

0 个答案:

没有答案