Skip to content

Commit

Permalink
🚧 Support Three-level zoom
Browse files Browse the repository at this point in the history
  • Loading branch information
Flyge committed Jul 29, 2018
1 parent eac3212 commit 73cc6d8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
2 changes: 1 addition & 1 deletion sketch/src/main/java/me/panpf/sketch/zoom/ImageZoomer.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public ImageZoomer(@NonNull ImageView imageView) {

this.imageView = imageView;

this.tapHelper = new TapHelper(appContext, this);
this.tapHelper = new TapHelper(appContext, scales, this);
this.scaleDragHelper = new ScaleDragHelper(appContext, this);
this.scrollBarHelper = new ScrollBarHelper(appContext, this);
this.blockDisplayer = new BlockDisplayer(appContext, this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,8 +488,7 @@ void location(float x, float y, boolean animate) {
final float scale = SketchUtils.formatFloat(getZoomScale(), 2);
final float fullZoomScale = SketchUtils.formatFloat(imageZoomer.getFullZoomScale(), 2);
if (scale == fullZoomScale) {
float[] zoomScales = imageZoomer.getDoubleClickZoomScales();
zoom(zoomScales[zoomScales.length - 1], x, y, false);
zoom(imageZoomer.getMaxZoomScale(), x, y, false);
}

RectF drawRectF = new RectF();
Expand Down
15 changes: 15 additions & 0 deletions sketch/src/main/java/me/panpf/sketch/zoom/Scales.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import me.panpf.sketch.Sketch;
import me.panpf.sketch.decode.ImageSizeCalculator;
import me.panpf.sketch.util.SketchUtils;

public class Scales {

Expand Down Expand Up @@ -102,6 +103,20 @@ public void reset(Context context, Sizes sizes, ScaleType scaleType, float rotat
doubleClickZoomScales = new float[]{minZoomScale, maxZoomScale};
}

public float nextScale(final float currentScale) {
float currentScaleFormat = SketchUtils.formatFloat(currentScale, 2);
float finalScale = -1;
for (float scale : doubleClickZoomScales) {
if (finalScale == -1) {
finalScale = scale;
} else if (currentScaleFormat < SketchUtils.formatFloat(scale, 2)) {
finalScale = scale;
break;
}
}
return finalScale;
}

public void clean() {
fullZoomScale = fillZoomScale = originZoomScale = 1f;
minZoomScale = DEFAULT_MINIMUM_SCALE;
Expand Down
26 changes: 8 additions & 18 deletions sketch/src/main/java/me/panpf/sketch/zoom/TapHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,26 @@
package me.panpf.sketch.zoom;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

import me.panpf.sketch.util.SketchUtils;
import me.panpf.sketch.viewfun.FunctionCallbackView;

class TapHelper extends GestureDetector.SimpleOnGestureListener {

@NonNull
private Scales scales;
@NonNull
private ImageZoomer imageZoomer;
@NonNull
private GestureDetector tapGestureDetector;

TapHelper(Context appContext, ImageZoomer imageZoomer) {
TapHelper(@NonNull Context appContext, @NonNull Scales scales, @NonNull ImageZoomer imageZoomer) {
this.imageZoomer = imageZoomer;
this.scales = scales;
this.tapGestureDetector = new GestureDetector(appContext, this);
}

Expand Down Expand Up @@ -87,23 +92,8 @@ public void onLongPress(MotionEvent e) {

@Override
public boolean onDoubleTap(MotionEvent ev) {
float scale = SketchUtils.formatFloat(imageZoomer.getZoomScale(), 2);

float[] doubleClickZoomScales = imageZoomer.getDoubleClickZoomScales();
if (doubleClickZoomScales.length < 2) {
return true;
}
float finalScale = doubleClickZoomScales[0];
for (int w = doubleClickZoomScales.length - 1; w >= 0; w--) {
float currentScale = doubleClickZoomScales[w];
if (scale < SketchUtils.formatFloat(currentScale, 2)) {
finalScale = currentScale;
break;
}
}

try {
imageZoomer.zoom(finalScale, ev.getX(), ev.getY(), true);
imageZoomer.zoom(scales.nextScale(imageZoomer.getZoomScale()), ev.getX(), ev.getY(), true);
} catch (ArrayIndexOutOfBoundsException e) {
// Can sometimes happen when getX() and getY() is called
}
Expand Down

0 comments on commit 73cc6d8

Please sign in to comment.