I am trying to implement zoom in my app. But I have encountered difficulties.
What I have done so far:
(NOTE: this app is a game, so it does not use invalidate
but a thread)
Rendering and the scaling:
public void render(Canvas c) {
super.draw(c);
if (c != null) {
int ss = c.getSaveCount();
c.scale(scaleFactor, scaleFactor);
//Rendering stuff
c.restoreToCount(ss);
}
}
Current zoom:
(this is called from onTouchEvent in the master class when there are two pointers. THese classes are nested. )
class scaler extends ScaleGestureDetector {
public scaler(Context context, OnScaleGestureListener listener) {
super(context, listener);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
listener.onScale(this);
return super.onTouchEvent(event);
}
@Override
public float getScaleFactor() {
return super.getScaleFactor();
}
}
float startScale;
float endScale;
class ScaleListener implements ScaleGestureDetector.OnScaleGestureListener{
@Override
public boolean onScale(ScaleGestureDetector detector) {
startScale = detector.getScaleFactor();
scaleFactor *= detector.getScaleFactor();
scaleFactor = (scaleFactor < 1 ? 1 : scaleFactor); // prevent our view from becoming too small //
scaleFactor = ((float)((int)(scaleFactor * 100))) / 100; // Change precision to help with jitter when user just rests their fingers //
return true;
}
@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
return true;
}
@Override
public void onScaleEnd(ScaleGestureDetector detector) {
}
}
I have been looking on several questions on SO, and all I have found does not work properly. There are various reasons for the solutions.
The one I am currently using:
Zooms in before zooming out(the scalefactor drops down before zooming out)
It does not zoom in on the area the fingers are in.
So how can I add zoom that zooms in on the area the fingers are in, and make a it so it zooms without resetting the zoom so it is really zoomed in?