Reputation: 26261
I'm trying to scale a view with setScaleX
and setScaleY
methods using ScaleGestureDetector. It works fine if you want just scale the view 5 to 7 times. But there is a big problem where you need 20-30 bigger view than the original. The ScaleGestureDetector does not take into account current scale of the view, so you cannot scale infinitely - the onScaleBegin isn't fired.
I'm sure it is because of these lines in the ScaleGestureDetector source code:
if (!mInProgress && span >= mMinSpan &&
(wasInProgress || Math.abs(span - mInitialSpan) > mSpanSlop)) {
...
mInProgress = mListener.onScaleBegin(this);
}
Is there an easy way to scale a view 20-30 times with ScaleGestureDetector or there is a custom scale detector for Android?
Upvotes: 0
Views: 761
Reputation: 26261
Here is a really nice to use implementation of scaling. I use it to scale a view up to 20 times with just one pinch-zoom gesture.
private final Matrix mMatrix = new Matrix();
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getPointerCount() == 2) {
float scale = (float) Math.sqrt(getScaleX());
float focalX = (event.getX(0) + event.getX(1)) / 2;
float focalY = (event.getY(0) + event.getY(1)) / 2;
mMatrix.setScale(scale, scale, focalX, focalY);
event.transform(mMatrix);
return mScaleGestureDetector.onTouchEvent(event);
}
return false;
}
In this case, both scales x and y are equal. In order to use different scales just change the second argument of the setScale method to the scale in y-axis.
Upvotes: 1
Reputation: 31871
I'm not sure I understand what you want. ScaleGestureDetector
simply translates the user's touch events into a scale factor.
It's up to you how to use the scale factor. If you're encountering limits, then the limits are in how you're using the scale value. i.e. your code is scaling the View, not the ScaleGestureDetector
.
The code you copied above simply applies a touch slop to the touch events so that it doesn't detect touches that aren't for scaling.
Upvotes: 1