Android: Draggable Toast, Or Alternative?
I'm drawing a number of bitmaps on a canvas, and using MotionEvents to let me drag them 开发者_如何学Pythonaround.
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.
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);
}
}
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.
精彩评论