是否有可能使用标准的Android API来在屏幕上移动组件?
我想产生一个Android用户界面,允许用户通过选择他们,然后拖拽他们在屏幕上移动添加的组件/构件。
I would like to produce an android user interface which allows the user to move added components/widgets around the screen by selecting them and then dragging them around.
这可能使用标准Android的API?
Is this possible using the standard android apis?
是的。这取决于你想要达到的目的。
Yes. It depends what you are trying to achieve.
这是可以做到的使用的标准API,但此功能不是的标准的API的的一部分。也就是说,没有 widget.DragOverHere()
方法,除非你写一个。
It can be done using the standard APIs, but this functionality is not part of the standard APIs. That is, there is no widget.DragOverHere()
method unless you write one.
这就是说,它不会被可怕复杂的事情。至少,你需要编写视图的自定义子类,并实现两个方法:的onDraw(帆布C)
和 onTouch(MotionEvent五)
。草图:
That said, it would not be terribly complicated to do. At a minimum, you would need to write a custom subclass of View and implement two methods: onDraw(Canvas c)
and onTouch(MotionEvent e)
. A rough sketch:
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
}
}