如何在View类中设置计时器?

时间:2015-03-15 08:28:30

标签: android multithreading timer draw

我正在尝试设置一个充当计时器的文本。如果我扩展Activity类,我会轻松创建计时器。但是我在这个View类中面临很多问题。在这里,我制作了一个代码,其中圆的半径将减小,并且对于屏幕上的每次触摸它将增加。同时我需要一个在顶部运行的计时器。因为我需要在每10秒后增加减速速度。

所以这里的计时器起着重要的作用。我尝试使用线程,它应该每1秒更新一次文本。但它显示了此异常IllegalThreadStateException

在此屏幕中是否还有其他方法可以实现计时器? 或者我在代码中犯了什么错误..

感谢您帮助朋友..

package com.kbt.testcircle;

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

public class MyView extends View {
    int radius = 100,i=0;

    boolean freeTouched = false;
    Path freePath;
    Handler handler = new Handler();

    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);
    }

    public void calThread() {

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

            @Override
            public void run() {
                // TODO Auto-generated method stub
                radius -= 1;
                Log.i("kbt", "inside thread 800 seconds");
                invalidate();
            }
        }, 500);


        Log.i("kbt", "Inside thread");
    }
Thread th=new Thread()
{
    public void run(){

        handler.postDelayed(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                i++;
            }
        },1000);
    }
};
    @Override
    protected void onDraw(Canvas canvas) throws IllegalThreadStateException 
    {
        // TODO Auto-generated method stub

        th.start();
        Log.i("kbt","starting");
        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);
        paint.setColor(Color.WHITE);
        paint.setStrokeWidth(0);
        paint.setTextSize(25);
        canvas.drawText("Timer : "+i, getWidth()/4, getHeight()/4, paint);
        calThread();

        Log.i("kbt", "Inside clled");

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
        case MotionEvent.ACTION_UP:

            break;
        case MotionEvent.ACTION_DOWN:

            radius+=5;
            invalidate();
            /* 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;
    }

}

2 个答案:

答案 0 :(得分:3)

  

但它显示此异常IllegalThreadStateException

由非runnable或非线程类引起。扩展View的类没有后台线程,因此您获得IllegalThreadStateException

  

在此屏幕中是否还有其他方法可以实现计时器?

Java提供了一个Timer类,用于所有类类型:

Timer timer = new Timer();
TimerTask task = new TimerTask(){
    @Override
    public void run() {
    // do your thing here
    }
};
timer.schedule(task,
1, // delay the task 1ms before executed
10000); // repeating your task every 10000ms, i.e. 10 seconds

答案 1 :(得分:0)

如果您真的使用线程,则只能启动一次线程。如果您想多次启动线程,请改用Runnable