2019-10-05 16:00:25 +02:00
|
|
|
package org.grapheneos.pdfviewer;
|
|
|
|
|
|
|
|
import android.annotation.SuppressLint;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.view.ScaleGestureDetector;
|
|
|
|
import android.view.View;
|
|
|
|
|
|
|
|
/*
|
|
|
|
The GestureHelper present a simple gesture api for the PdfViewer
|
|
|
|
*/
|
|
|
|
|
|
|
|
class GestureHelper {
|
|
|
|
public interface GestureListener {
|
|
|
|
// Can be replaced with ratio when supported
|
2020-04-04 17:57:15 +02:00
|
|
|
void onZoomIn(float value);
|
|
|
|
void onZoomOut(float value);
|
2020-04-04 17:54:34 +02:00
|
|
|
void onZoomEnd();
|
2019-10-05 16:00:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressLint("ClickableViewAccessibility")
|
|
|
|
static void attach(Context context, View gestureView, GestureListener listener) {
|
|
|
|
final ScaleGestureDetector scaleDetector = new ScaleGestureDetector(context,
|
|
|
|
new ScaleGestureDetector.SimpleOnScaleGestureListener() {
|
2022-01-31 03:00:42 +01:00
|
|
|
final float SPAN_RATIO = 600;
|
2019-10-05 16:00:25 +02:00
|
|
|
float initialSpan;
|
2020-04-04 18:21:25 +02:00
|
|
|
float prevNbStep;
|
2019-10-05 16:00:25 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onScaleBegin(ScaleGestureDetector detector) {
|
|
|
|
initialSpan = detector.getCurrentSpan();
|
|
|
|
prevNbStep = 0;
|
2020-04-04 17:47:00 +02:00
|
|
|
return true;
|
2019-10-05 16:00:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onScale(ScaleGestureDetector detector) {
|
|
|
|
float spanDiff = initialSpan - detector.getCurrentSpan();
|
2020-04-04 18:21:25 +02:00
|
|
|
float curNbStep = spanDiff / SPAN_RATIO;
|
|
|
|
|
|
|
|
float stepDiff = curNbStep - prevNbStep;
|
|
|
|
if (stepDiff > 0) {
|
|
|
|
listener.onZoomOut(stepDiff);
|
|
|
|
} else {
|
|
|
|
listener.onZoomIn(Math.abs(stepDiff));
|
2019-10-05 16:00:25 +02:00
|
|
|
}
|
2020-04-04 18:21:25 +02:00
|
|
|
prevNbStep = curNbStep;
|
|
|
|
|
2019-10-05 16:00:25 +02:00
|
|
|
return true;
|
|
|
|
}
|
2020-04-04 17:54:34 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onScaleEnd(ScaleGestureDetector detector) {
|
|
|
|
listener.onZoomEnd();
|
|
|
|
}
|
2019-10-05 16:00:25 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
gestureView.setOnTouchListener((view, motionEvent) -> {
|
|
|
|
scaleDetector.onTouchEvent(motionEvent);
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|