我上了Hover听我的usb鼠标(使用android开发板)。我想使用悬停监听器在画布上创建一个人造鼠标光标,通过绘制一个形状(比如说一个圆圈)并在我将鼠标悬停在画布上时刷新它的位置。 我的问题是当我移动鼠标时,旧的圆形图画仍保留在画布上,所以我得到了一个圆圈而不是一个移动的圆圈。我现在使用的代码显然很愚蠢(试图用计时器延迟画布删除,导致性能非常低)。任何人都可以帮我编写这部分代码(触摸事件工作正常并做其他事情。我只需要hoverlistener部分)
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
canvas.drawPath(drawPath, drawPaint);
}
//respond to touch interaction
@Override
public boolean onTouchEvent(MotionEvent event) {
float touchX = event.getX();
float touchY = event.getY();
//respond to down, move and up events
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
drawPath.moveTo(touchX, touchY);
break;
case MotionEvent.ACTION_MOVE:
drawPath.lineTo(touchX, touchY);
break;
case MotionEvent.ACTION_UP:
drawPath.lineTo(touchX, touchY);
drawCanvas.drawPath(drawPath, drawPaint);
new Timer().schedule(new TimerTask() {
@Override
public void run() {
// this code will be executed after 2 seconds
drawCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
}
}, 2000);
drawPath.reset();
break;
default:
return false;
}
//redraw
invalidate();
return true;
}
private OnHoverListener ohl = new OnHoverListener() {
@Override
public boolean onHover( View v, MotionEvent ev ) {
// Log.i( TAG, ev.toString() );
arrow = BitmapFactory.decodeResource(getResources(),R.drawable.cross);
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
// path.moveTo(eventX, eventY);
return true;
case MotionEvent.ACTION_HOVER_MOVE:
//Log.i(TAG,"hover move");
//drawCanvas.drawColor(Color.RED);
drawCanvas.drawBitmap(arrow,ev.getX(),ev.getY(),drawPaint);
new Timer().schedule(new TimerTask() {
@Override
public void run() {
// this code will be executed after 2 seconds
drawCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
}
}, 10);
return true;
case MotionEvent.ACTION_UP:
// nothing to do
return true;
}
return true;
}
};
答案 0 :(得分:1)
将鼠标光标位置缓存在成员变量中。然后调用invalidate()并让onDraw()执行绘图。