2019-10-05 16:00:25 +02:00
|
|
|
package org.grapheneos.pdfviewer;
|
|
|
|
|
|
|
|
import android.annotation.SuppressLint;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.view.GestureDetector;
|
|
|
|
import android.view.MotionEvent;
|
|
|
|
import android.view.ScaleGestureDetector;
|
|
|
|
import android.view.View;
|
|
|
|
|
|
|
|
/*
|
|
|
|
The GestureHelper present a simple gesture api for the PdfViewer
|
|
|
|
*/
|
|
|
|
|
|
|
|
class GestureHelper {
|
|
|
|
public interface GestureListener {
|
|
|
|
boolean onTapUp();
|
|
|
|
// 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 GestureDetector detector = new GestureDetector(context,
|
|
|
|
new GestureDetector.SimpleOnGestureListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onSingleTapUp(MotionEvent motionEvent) {
|
|
|
|
return listener.onTapUp();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
final ScaleGestureDetector scaleDetector = new ScaleGestureDetector(context,
|
|
|
|
new ScaleGestureDetector.SimpleOnScaleGestureListener() {
|
|
|
|
// As the zoom value is discrete we listen to scaling step and not scaling ratio
|
|
|
|
float SPAN_STEP = 150;
|
|
|
|
float initialSpan;
|
|
|
|
int prevNbStep;
|
|
|
|
|
|
|
|
@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();
|
|
|
|
int curNbStep = (int) (spanDiff/SPAN_STEP);
|
|
|
|
if (curNbStep != prevNbStep) {
|
|
|
|
int stepDiff = curNbStep - prevNbStep;
|
|
|
|
if (stepDiff > 0) {
|
2020-04-04 17:57:15 +02:00
|
|
|
listener.onZoomOut(stepDiff * 0.25f);
|
2019-10-05 16:00:25 +02:00
|
|
|
} else {
|
2020-04-04 17:57:15 +02:00
|
|
|
listener.onZoomIn(Math.abs(stepDiff * 0.25f));
|
2019-10-05 16:00:25 +02:00
|
|
|
}
|
|
|
|
prevNbStep = curNbStep;
|
|
|
|
}
|
|
|
|
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) -> {
|
|
|
|
detector.onTouchEvent(motionEvent);
|
|
|
|
scaleDetector.onTouchEvent(motionEvent);
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|