use*_*034 5 user-interface android
我想创建一个android用户界面,允许用户通过选择它们然后拖动它们来移动添加components/ widgets围绕屏幕.
这可能使用标准的android apis吗?
是.这取决于你想要实现的目标.
它可以使用标准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
}
}
Run Code Online (Sandbox Code Playgroud)