[问题]为什么我的Handler的handleMessage次数变少了?

public Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                // Upon receiving the update pulse, we have the view perform a
                // update and then enqueue a new message to pulse at the desired
                // next time.
                case UPDATE_MSG: {
                    update();
                    if (mUpdateFlag == false) {
                        LogUtils.d(("end  update 2"));
                        stopUpdating();
                    } else {
                        LogUtils.d("msg");
                        mHandler.sendMessageDelayed(mHandler.obtainMessage(UPDATE_MSG), UPDATE_DELAY);
                    }
                    break;
                }
                default:
                    super.handleMessage(msg);
            }
        }
    };
    /**
     * Start up the pulse to update the screen, clearing any existing pulse to
     * ensure that we don't have multiple pulses running at a time.
     */
    void startUpdating() {
        LogUtils.d("start");
        mStartFlag = true;
        mUpdateFlag = true;
        mHandler.removeMessages(UPDATE_MSG);
        mHandler.sendMessageDelayed(mHandler.obtainMessage(UPDATE_MSG), UPDATE_DELAY);
    }

    /**
     * Stop the pulse to fade the screen.
     */
    void stopUpdating() {
        LogUtils.d("stop");
        mHandler.removeMessages(UPDATE_MSG);
    }
public synchronized void update() {
        LogUtils.d("update 1 " + System.currentTimeMillis());
        if (mCanvas != null) {
            LogUtils.d("update 2 " + System.currentTimeMillis());

            int left = mRect.left;
            int top = mRect.top;
            int right = mRect.right;
            int bottom = mRect.bottom;

            if (left < 0)
                left = 0;
            if (top < 0)
                top = 0;
            if (right < 0)
                right = 0;
            if (bottom < 0)
                bottom = 0;


            if (mNewRegionFlag == true) {
                if (mEvent == 1) {
                    LogUtils.d(TAG, ("end  update 1\n"));
                    mUpdateFlag = false;
                }
            } else {
                LogUtils.d(TAG, ("left=" + left + " top=" + top + " right=" + right + " bottom=" + bottom + ""));
                mCanvas.drawPath(mPath, mPaint); //绘制path
                if (mUpdateType == 0)
                    if (Constants.TEST)
                        invalidate(left, top, right, bottom, UPDATE_MODE_PARTIAL);
                    else
                        invalidate(left, top, right, bottom);
                else
                    invalidate(left, top, right, bottom);
                mNewRegionFlag = true;
            }

        }
    }
onTouchEvent
switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    LogUtils.d(TAG, ("touch down\n"));
                    startUpdating();
                breadk;
                case MotionEvent.ACTION_UP:
                LogUtils.d(TAG, ("touch up\n"));
                 mUpdateFlag = false;
                break;


测试都是以画一个圈来测试log次数

现在是这样操作的,进来初始化该View,直接画线,结果打印如下


然后,我在此activity的基础上新建了一个空白的(清空画布,设置画布)


日志差不多少了一半,(测试多次)


但是总时间是一样的,误差不过50ms,中间的次数变少了,所以才有后者实时速度跟不上前者,update的脉冲没有实时跟上(类似游戏掉帧)。

不知道是什么原因导致的,是不是Handler机制


欢迎留言。。。。。。讨论



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值