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机制
欢迎留言。。。。。。讨论