android - 是否可以使用标准的 android api 在屏幕上移动组件?

标签 android user-interface

我想制作一个 android 用户界面,允许用户通过选择它们在屏幕上移动添加的 components/widgets 和然后将它们拖来拖去。

这可以使用标准的 android api 吗?

最佳答案

是的。这取决于您要实现的目标。

它可以使用标准 API 来完成,但此功能不是标准 API 的一部分。也就是说,除非您编写一个方法,否则没有 widget.DragOverHere() 方法。

也就是说,做起来不会非常复杂。至少,您需要编写 View 的自定义子类并实现两个方法:onDraw(Canvas c)onTouch(MotionEvent e)。粗略的草图:

class MyView extends View {

    int x, y;  //the x-y coordinates of the icon (top-left corner)
    Bitmap bitmap; //the icon you are dragging around

    onDraw(Canvas c) {
      canvas.drawBitmap(x, y, bitmap);
    }

    onTouch(MotionEvent e) {
      switch(e.getAction()) {
      case MotionEvent.ACTION_DOWN:
        //maybe use a different bitmap to indicate 'selected'
        break;
      case MotionEvent.ACTION_MOVE:
        x = (int)e.getX();
        y = (int)e.getY();
        break;
      case MotionEvent.ACTION_UP:
        //switch back to 'unselected' bitmap
        break;
      }
      invalidate(); //redraw the view
    }
}

关于android - 是否可以使用标准的 android api 在屏幕上移动组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/700550/

相关文章:

java - 单击按钮时隐藏 JFrame 而不停止程序

android - 在 sqlite for android 的选择参数参数中使用 int 值

android content provider - 隐藏内部复杂性但允许复杂查询的设计模式

android - 启动android应用程序时如何删除标题?

android - 在android studio中生成sign apk时出错

android - 如何找到 Root设备?

Java GUI,无需任何其他进程

user-interface - 与桌面应用程序相比,开发网络应用程序有哪些好处和挑战?

css - 如何使用 Tailwind 自定义深色模式颜色

winforms - 显示带有 powershell 脚本焦点的 WinForms 对话框