gpt4 book ai didi

安卓应用 : Timer in On Touch

转载 作者:行者123 更新时间:2023-11-30 03:43:39 25 4
gpt4 key购买 nike

我的代码有问题。我想在 3 秒后 Action_UP 时清除 Canvas (通过重置功能)(这里我使用了计时器)。在此期间,如果发生 Action_Down 或 Action_Move 事件,我必须取消 Timer-Task。

(Action_down 和 Action_move 应该取消 Action_up 中的定时器)我必须做什么??请帮助我。

谢谢

        public boolean onTouch(View view, MotionEvent event) {
// Check event type

switch (event.getAction()) {

// Finger down
case MotionEvent.ACTION_DOWN:

paint.setColor(Color.BLACK);
paint.setStrokeWidth(7f);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setAntiAlias(true);


// Get current position
pX = event.getX();
pY = event.getY();

// Set beginning of path to (posX,posY)

path.moveTo(pX, pY);

if (start)
{
mytimer.cancel();
start = false;
}


break;

// Finger moves
case MotionEvent.ACTION_MOVE:
mX = event.getX();
mY = event.getY();

// Set position of end of path
path.lineTo(mX, mY);


// Draw path
bitmapCanvas.drawPath(path, paint);

// Invalidate canvas (redraw the view)

if (start)
{
mytimer.cancel();
start = false;
}

invalidate();
break;

// Finger up
case MotionEvent.ACTION_UP:

//bitmap = getResizedBitmap(bitmap, 16, 16);
//here We have to start our neocogniton :)

mX = event.getX();
mY = event.getY();

if (mY == pY && mX == pX){
bitmapCanvas.drawPoint(pX, pY, paint);
invalidate();
}

path.reset();

mytimer.schedule(new TimerTask() {
@Override
public void run() {reset();}}, 3000);




break;

}


return true;
}

重置()

    public void reset(){
bitmapCanvas.drawColor(Color.WHITE);
start = true;
}

最佳答案

我想你在第二个 timer.cancel 上遇到了错误,因为这个函数正在取消计时器本身。尝试输入:

 myTimer = new Timer(); 

之前

 myTimer.cancel();

关于安卓应用 : Timer in On Touch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15345456/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com