我已经尝试了一段时间并在这里得到了解决方案:
public class CanvasContainerScrollView extends ScrollView {
public boolean enableScrollView = true;
public CanvasContainerScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
public boolean onTouchEvent(MotionEvent event) {
android.util.Log.v ("scrollview", "touched");
if (enableScrollView) {
return super.onTouchEvent(event);
}
return false;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (enableScrollView) {
return super.onInterceptTouchEvent(event);
}
return false;
}
}
设置 enableScrollView 将启用滚动或虎钳。在 CanvasView onTouchEvent -
@Override
public boolean onTouchEvent(MotionEvent ev) {
final int action = ev.getAction();
android.util.Log.v("canvasView", "touched");
switch (action) {
case MotionEvent.ACTION_DOWN: {
final float x = ev.getX();
final float y = ev.getY();
int i = 2;
for (i = goList.size() - 1; i >= 0; --i) {
if (currentObjectLayer == -1 && goList.get(i).inHitArea(x, y)) {
parent.enableScrollView = false;
if (currentObjectLayer != -1) {
continue;
}
goList.get(i).selected();
currentObjectLayer = i;
// break;
} else {
// if (goList.get(i).inHitArea(x, y)) {
// parent.enableScrollView = false;
// } else {
// parent.enableScrollView = true;
// }
goList.get(i).unselected();
}
}
if (currentObjectLayer == -1) {
parent.enableScrollView = true;
}
// Remember where we started
lastTouchX = x;
lastTouchY = y;
break;
}
case MotionEvent.ACTION_MOVE: {
final float x = ev.getX();
final float y = ev.getY();
// Calculate the distance moved
final float dx = x - lastTouchX;
final float dy = y - lastTouchY;
// Move the object
posX += dx;
posY += dy;
// Remember this touch position for the next move event
lastTouchX = x;
lastTouchY = y;
// android.util.Log.v("image", "dx=" + dx + ":dy=" + dy);
if (currentObjectLayer != -1) {
goList.get(currentObjectLayer).left += dx;
goList.get(currentObjectLayer).top += dy;
goList.get(currentObjectLayer).dx = dx;
goList.get(currentObjectLayer).dy = dy;
}
// Invalidate to request a redraw
invalidate();
break;
}
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
// goList.get(currentObjectLayer).objectType = 0;
currentObjectLayer = -1;
parent.enableScrollView = true;
break;
}
invalidate();
return true;
}