ScaleGestureDetector使用注意事项
注:一定要通过view的onTouchEvent调用mScaleGestureDetector.onTouchEvent(ev);,只有这样
才能调用回调函数:onScaleBegin
具体可以参加:android源码:KenBurnsActivity.java
下面是转载的文章:
Detects transformation gestures involving more than one pointer ("multitouch") using the supplied MotionEvents.
TheScaleGestureDetector.OnScaleGestureListener callback
will notify users when a particular gesture event has occurred. This class should only be used with MotionEvents
reported via touch. To use this class:
- Create an instance of the ScaleGestureDetector for your View
- In the (View's )onTouchEvent(MotionEvent) method ensure you call (ScaleGestureDetector's )onTouchEvent(MotionEvent). The methods defined in your callback will be executed when the events occur.
下面是测试的代码:
-
public class TouchView extends View {
-
-
private Context mContext;
-
private Paint mPaint;
-
private Rect mRect;
-
private ScaleGestureDetector mScaleGestureDetector;
-
-
public TouchView(Context context) {
-
super(context);
-
mContext = context;
-
mPaint = new Paint();
-
mPaint.setColor(Color.BLUE);
-
mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
-
mRect = new Rect();
-
mRect.left = 10;
-
mRect.top = 10;
-
mRect.right = 300;
-
mRect.bottom = 400;
-
-
mScaleGestureDetector = new ScaleGestureDetector(context, listener);
-
}
-
-
OnScaleGestureListener listener = new OnScaleGestureListener() {
-
-
public void onScaleEnd(ScaleGestureDetector detector) {
-
// TODO Auto-generated method stub
-
Log.i("OnScaleGestureListener", "onScaleEnd");
-
}
-
-
public boolean onScaleBegin(ScaleGestureDetector detector) {
-
// TODO Auto-generated method stub
-
Log.i("OnScaleGestureListener", "onScaleBegin detector.getCurrentSpan()
= " + detector.getCurrentSpan());
-
detector.getCurrentSpan();
-
return true;
-
}
-
-
public boolean onScale(ScaleGestureDetector detector) {
-
// TODO Auto-generated method stub
-
float cur = detector.getCurrentSpan();
-
float pre = detector.getPreviousSpan();
-
float cp = cur - pre;
-
Log.i("OnScaleGestureListener", "onScale detector.getCurrentSpan()
= " + cur
-
+ " detector.getPreviousSpan()
= " + pre
-
+ " cur - pre = " + (cur - pre));
-
if (cp < -100 && !mScaled) {
-
mScaled = true;
-
mRect.left = 10+ 50;
-
mRect.top = 10 + 50;
-
mRect.right = 300 - 50;
-
mRect.bottom = 400 - 50;
-
invalidate();
-
} else if (cp > 100 && mScaled) {
-
mScaled = false;
-
mRect.left = 10;
-
mRect.top = 10 ;
-
mRect.right = 300 ;
-
mRect.bottom = 400;
-
invalidate();
-
}
-
return false;
-
}
-
};
-
-
boolean mScaled = false;
-
/*@Override
-
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
-
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
-
int width = MeasureSpec.getSize(widthMeasureSpec);
-
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
-
int height = MeasureSpec.getSize(heightMeasureSpec);
-
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
-
}*/
-
-
-
-
@Override
-
protected void onDraw(Canvas canvas) {
-
canvas.drawRect(mRect, mPaint);
-
}
-
-
-
-
@Override
-
public boolean onTouchEvent(MotionEvent event) {
-
System.out.println("event.getPointerCount()
: " + event.getPointerCount());
-
mScaleGestureDetector.onTouchEvent(event);
-
return true;
-
}
- }