自定义view实现简单的计时器

自定义View学习小记

@Alu

先贴代码

public class CountView extends View {
private Paint mPaint;
private int millisecond;
private int hour;
private int minute;
private int second;
private Rect mBounds;
private Context context;
private boolean isRunning = false;
private Timer timer;
private StringBuffer stringBuffer;
private MyTask myTask;

public CountView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    this.context = context;
    mBounds = new Rect();
    mPaint = new Paint();
    stringBuffer = new StringBuffer();
    stringBuffer.append("00:00:00:00");

}
@Override
public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()){
        case MotionEvent.ACTION_DOWN:
            if (isRunning) {
                timer.cancel();
                timer.purge();
                timer = null;
                myTask = null;
                System.gc();
            } else {
                timer = new Timer();
                myTask = new MyTask();
                timer.schedule(myTask, 0, 10);
            }
            isRunning = !isRunning;
            break;
    }
    return false;
}
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    mPaint.setColor(ContextCompat.getColor(context, R.color.colorPrimary));
    canvas.drawRect(0, 0, getWidth(), getHeight(), mPaint);
    mPaint.setColor(Color.WHITE);
    mPaint.setTextSize(60);
    String text = stringBuffer.toString();
    mPaint.getTextBounds(text, 0, text.length(), mBounds);
    float textWidth = mBounds.width();
    float textHeight = mBounds.height();
    canvas.drawText(text, getWidth() / 2 - textWidth / 2, getHeight() / 2 + textHeight / 2, mPaint);

}

private class MyTask extends TimerTask {

    @Override
    public void run() {
        stringBuffer.delete(0, stringBuffer.length());
        millisecond++;
        if (millisecond > 99) {
            millisecond = 0;
            second++;
        }
        if (second > 59) {
            second = 0;
            minute++;
        }

        if (minute > 59) {
            minute = 0;
            hour++;
        }
        if (hour < 10)
            stringBuffer.append(0).append(hour);
        else
            stringBuffer.append(hour);
        stringBuffer.append(":");
        if (minute < 10)
            stringBuffer.append(0).append(minute);
        else
            stringBuffer.append(minute);
        stringBuffer.append(":");
        if (second < 10)
            stringBuffer.append(0).append(second);
        else
            stringBuffer.append(second);
        stringBuffer.append(":");
        if (millisecond < 10)
            stringBuffer.append(0).append(millisecond);
        else
            stringBuffer.append(millisecond);
        handler.sendEmptyMessage(0);
    }

}

private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        if (msg.what == 0) {
            invalidate();
        }
    }
};

}
利用Handler+Timer来控制时间,StringBuffer和很简单的算法来保存时间。
用法很简单直接在布局中引用:

<com.alu.test.okletusgo.myview.view.CountView
    android:layout_width="150dp"
    android:layout_height="50dp"
    android:layout_centerInParent="true" />

稍加改造也可以有其他用途,例如 在抽奖之类的界面使用.

需要注意的:

view的点击事件里我做了:

timer = new Timer();  
myTask = new MyTask();

原因是个人多次尝试,timerrun方法只会执行 TimerTask 类的run();方法一次,所以手动去将之前用过的对象设置为 null 并调用 System.gc(); ,企图回收对象,(感觉很笨,希望有人看了指点一下),再重新去设置一个新的 timer
另外提一下 Viewinvalidate(); 方法需要在主线程执行 ,这也是使用了handler的原因。

最后:

不求赞,只求提出问题,让我改进学习。

修改暂停继续的操作在 onTouch 的 ACTIONDOWN 情境下 。原因是对计时器的精准控制就应该体现在点下去的瞬间来控制跑秒。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 主要思路 1.我们需要自定义一个继承自FrameLayout的布局,利用FrameLayout布-局的特性(在同一...
    ZebraWei阅读 2,412评论 0 5
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,503评论 25 709
  • 周五,我一直在说服自己,只要你想做,就一定能做到。但是发现自己执行力太差,是没有精神,还是年龄大了,注意力涣散。现...
    向往的1991阅读 135评论 0 0
  • 开工一张新的,有谁知道是什么名号吗?
    joyyang1221阅读 229评论 0 2
  • 我们没有义务去感同身受别人的故事和感情 生活,就应该在不伤害他人的基础上 让自己活的舒服
    树邮阅读 287评论 0 1