我已经创建了一个自定义视图,我想处理视图中显示的数据的滚动和缩放。
所以我正在尝试将手势和比例检测器结合起来。 这是我的代码,滚动检测器似乎工作,但我不能让比例检测器做出反应。
有什么建议吗?
这是我的代码:
public void initGestureDetectors() {
GestureDetector.SimpleOnGestureListener gestureListener = new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
scrollBy((int)distanceX, (int)distanceY);
return true;
}
};
gestureDetector = new GestureDetector(getContext(), gestureListener);
ScaleGestureDetector.SimpleOnScaleGestureListener scaleListener = new ScaleGestureDetector.SimpleOnScaleGestureListener() {
public boolean onScale (ScaleGestureDetector detector) {
// check scale is not too small
Toast.makeText(getContext(), "Scale x " + detector.getScaleFactor(), Toast.LENGTH_SHORT).show();
if(detector.getScaleFactor() > 0.5) {
return true;
}
return false;
}
};
scaleDetector = new ScaleGestureDetector(getContext(), scaleListener);
setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View view, MotionEvent motionEvent) {
gestureDetector.onTouchEvent(motionEvent);
return true;
}
});
}
public boolean onTouchEvent(MotionEvent event) {
if (event.getPointerCount() > 1) {
Toast.makeText(getContext(), "detected", Toast.LENGTH_SHORT).show();
return scaleDetector.onTouchEvent(event);
}
return gestureDetector.onTouchEvent(event);
}
另外,如何在模拟器中进行“缩放”运动,是否可能?