Search code examples
androidcanvashovercursordraw

Android: Draw A "Mouse Cursor" on a canvas. onHoverListener


I got on Hover listening to my usb mouse (using an android development board). I want to use the hover listener to create an artificial mouse cursor on the canvas, by drawing a shape (lets say a circle) and refreshing location of it while I hover my mouse on canvas. My problem is the old circle drawings remain on the canvas as I move the mouse, So I get a trail of circles instead of a single moving circle. The code I use now is obviously dumb (trying to delay the canvas deletion with a timer, causing very low performance). Can anyone help me code this part (the on touch event is working fine and doing something else. I just need the hoverlistener part )

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;
    }
};

Solution

  • Cache your mouse cursor location in a member variable. Then call invalidate() and let onDraw() do the drawing.