Question

I'm drawing a number of bitmaps on a canvas, and using MotionEvents to let me drag them around.

When each item is pressed, I'd like to display a Toast, or, Toast-like mini information-panel that tracks the movement of the bitmap being dragged during an ACTION_MOVE. The "Toast" would appear on ACTION_DOWN and vanish on ACTION_UP.

The problem with using Toast is that I have to give it a duration, and, also, I can't change its position once it has been displayed. Unless I can kill the Toast for each ACTION_MOVE, and display a new one right away at the current coordinates? (Sorry, thinking aloud at this point, can't get to my dev machine to test...)

I don't know what other options there might be to achieve this, and I'd very much appreciate suggestions from the community.

Was it helpful?

Solution

Hope this helps, just whipped it up, might even compile!

private boolean mDragging = false;
private float mTouchX = 0, mTouchY = 0;
private Paint mTextPaint = new Paint();//need to set this up in onCreate!

public boolean onTouchEvent(MotionEvent event)
{
  mTouchX = event.getX();
  mTouchY = event.getY();

  if(event.getAction() == ACTION_DOWN)
  {
    mDragging = true;
  }
  else if(event.getAction() == ACTION_UP)
  {
    mDragging = false;
  }

  return true;
}

protected void onDraw (Canvas canvas)
{
  /* Put all your bitmap drawing here. */

  /* Draw some info text on top of everything else. */
  if(mDragging)
  {
    String text = mTouchX + ", " + mTouchY;
    canvas.drawText(mTouchX, mTouchY + 50, text, mTextPaint);
  }
}

OTHER TIPS

A Toast isn't suitable in this case for reasons you already mentioned. It will be better to define a region on the Canvas and draw the message string there using drawText. Put this in the onDraw method and call invalidate whenever you need to update the text or the position of the message board.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top