我想制作一个 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/