/*
* 触摸事件处理
*/
@Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Drawable current = markerAt(x, y);
if (current == null) {
mTrackingMarker = addBox(v, x, y);
mTrackingPoint = new Point(x, y);
} else {
//移除前面的方框
removeMarker(v, current);
}
break;
case MotionEvent.ACTION_MOVE:
if (mTrackingMarker != null) {
resizeBox(v, mTrackingMarker, mTrackingPoint, x, y);
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
mTrackingMarker = null;
mTrackingPoint = null;
break;
}
return true;
}
/*
* 更新已经存在的方框
*/
private void resizeBox(View v, Drawable target, Point trackingPoint, int x, int y) {
Rect bounds = new Rect(target.getBounds());
//如果触摸点在已经存在的点的左边更新左边位置
//else说明在右边,更新右边的位置
if (x < trackingPoint.x) {
bounds.left = x;
} else {
bounds.right = x;
}
//如果新的触摸点在已经存在点的上边,更新上边的位置
//else更新下边的位置
if (y < trackingPoint.y) {
bounds.top = y;
} else {
bounds.bottom = y;
}
//更新方框的边框
target.setBounds(bounds);
v.invalidate();
}
94.08 KB, 下载次数: 74
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |