自制移动组件逻辑(求封装好的方法)?
对Image组件进行监听TouchEvent事件,监测触屏位置为point,再将组件位置设置为point
重写onTouchEvent接口方法
public class Forward implements Component.TouchEventListener {
@Override
public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
int action = touchEvent.getAction();
MmiPoint point = touchEvent.getPointerScreenPosition(0);
int x,y;
x = (int) point.getX();
y = (int) point.getY();
if (action == TouchEvent.POINT_MOVE){
component.setPosition(x, y);
}
return true;
}
}
此时存在问题 getPointerScreenPosition 得到的位置坐标point是相对于整个屏幕
但是此时DirectionalLayout布局上边还有一部分TItle块,而设置 component.setPosition(x, y) 是相对于父组件设置的组件位置
当鼠标点击到组件进行移动,组件会向下偏移一部分,此时的(x, y)没有改变,是相对整个屏幕获取触摸点Point,设置组件位置时相对白色背景布局设置。
请问有拖拽组件的封装好的方法吗?
或者有什么去掉上边黑色块的方法么?
感谢
组件拖拽的代码可以参考如下:
public class FourAbilitySlice extends AbilitySlice {
Image image;
Point startDragPoint;
Point componentStartPoint;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_four);
image=findComponentById(ResourceTable.Id_image);
image.setDraggedListener(Component.DRAG_HORIZONTAL_VERTICAL, new Component.DraggedListener() {
@Override
public void onDragDown(Component component, DragInfo dragInfo) {
}
@Override
public void onDragStart(Component component, DragInfo dragInfo) {
//获取组件在父组件中的起始位置
componentStartPoint = new Point(component.getContentPositionX(),component.getContentPositionY());
//开始拖拽起始点
startDragPoint = dragInfo.startPoint;
}
@Override
public void onDragUpdate(Component component, DragInfo dragInfo) {
float xOffset = dragInfo.updatePoint.getPointX() - startDragPoint.getPointX();
float yOffset = dragInfo.updatePoint.getPointY() - startDragPoint.getPointY();
component.setContentPosition(componentStartPoint.getPointX() + xOffset, componentStartPoint.getPointY() + yOffset);
componentStartPoint = new Point(component.getContentPositionX(),component.getContentPositionY());
}
@Override
public void onDragEnd(Component component, DragInfo dragInfo) {
float xOffset = dragInfo.updatePoint.getPointX() - startDragPoint.getPointX();
float yOffset = dragInfo.updatePoint.getPointY() - startDragPoint.getPointY();
component.setContentPosition(componentStartPoint.getPointX() + xOffset, componentStartPoint.getPointY() + yOffset);
}
@Override
public void onDragCancel(Component component, DragInfo dragInfo) {
}
});
}
}