Android:可拖动 Toast,还是替代方案?

发布于 2024-10-07 20:44:19 字数 350 浏览 2 评论 0原文

我在画布上绘制了许多位图,并使用 MotionEvents 让我可以拖动它们。

当按下每个项目时,我想显示一个 Toast 或类似 Toast 的迷你信息面板,用于跟踪在 ACTION_MOVE 期间拖动的位图的移动。 “Toast”将在 ACTION_DOWN 时出现,并在 ACTION_UP 时消失。

使用 Toast 的问题是我必须给它一个持续时间,而且一旦它显示出来我就无法改变它的位置。除非我可以为每个 ACTION_MOVE 杀死 Toast,并立即在当前坐标处显示一个新的 Toast? (抱歉,此时大声思考,无法访问我的开发机器进行测试......)

我不知道还有哪些其他选项可以实现这一目标,并且我非常感谢社区的建议。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那请放手 2024-10-14 20:44:19

希望这有帮助,刚刚启动它,甚至可以编译!

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

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);
  }
}
暖伴 2024-10-14 20:44:19

由于您已经提到的原因,Toast 不适合这种情况。最好在 Canvas 上定义一个区域并使用 drawText 在那里绘制消息字符串。将其放入 onDraw 方法中,并在需要更新文本或留言板位置时调用 invalidate。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文