Skip to content

Commit

Permalink
🎨 Improved ImageZoomer code
Browse files Browse the repository at this point in the history
  • Loading branch information
Flyge committed Oct 22, 2017
1 parent a7d48e6 commit 6782447
Show file tree
Hide file tree
Showing 14 changed files with 631 additions and 731 deletions.
20 changes: 20 additions & 0 deletions sketch/src/main/java/me/xiaopan/sketch/util/SketchUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.text.format.Formatter;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import java.io.Closeable;
Expand Down Expand Up @@ -1237,4 +1239,22 @@ public static String createFileUriDiskCacheKey(String uri, String filePath) {
return uri;
}
}

@SuppressWarnings("WeakerAccess")
public static void postOnAnimation(View view, Runnable runnable) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
view.postOnAnimation(runnable);
} else {
view.postDelayed(runnable, 1000 / 60);
}
}

public static int getPointerIndex(int action) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
return (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
} else {
//noinspection deprecation
return (action & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,14 @@ public void onAttachedToWindow() {
public void onDraw(@NonNull Canvas canvas) {
super.onDraw(canvas);

imageZoomer.onDraw(canvas);

if (SketchUtils.sdkSupportBitmapRegionDecoder()) {
if (hugeImageViewer.isReady()) {
hugeImageViewer.draw(canvas);
}
}

// imageZoomer.onDraw 必须在 hugeImageViewer.draw 之后执行,这样才不会被覆盖掉
imageZoomer.onDraw(canvas);
}

@Override
Expand Down
61 changes: 0 additions & 61 deletions sketch/src/main/java/me/xiaopan/sketch/zoom/CompatUtils.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,40 @@

package me.xiaopan.sketch.zoom;

import android.content.Context;
import android.graphics.RectF;
import android.widget.ImageView;

import me.xiaopan.sketch.SLog;
import me.xiaopan.sketch.util.SketchUtils;
import me.xiaopan.sketch.zoom.scrollerproxy.ScrollerProxy;

class FlingTranslateRunner implements Runnable {
private final ScrollerProxy mScroller;
private ZoomManager zoomManager;
private int mCurrentX, mCurrentY;
class FlingRunner implements Runnable {
private ImageZoomer imageZoomer;
private ScaleDragHelper scaleDragHelper;

FlingTranslateRunner(Context context, ZoomManager zoomManager) {
this.mScroller = ScrollerProxy.getScroller(context);
this.zoomManager = zoomManager;
private ScrollerProxy scroller;
private int currentX;
private int currentY;

FlingRunner(ImageZoomer imageZoomer, ScaleDragHelper scaleDragHelper) {
this.scroller = ScrollerProxy.getScroller(imageZoomer.getImageView().getContext());
this.imageZoomer = imageZoomer;
this.scaleDragHelper = scaleDragHelper;
}

void fling(int velocityX, int velocityY) {
if (!zoomManager.getImageZoomer().isWorking()) {
if (!imageZoomer.isWorking()) {
SLog.w(ImageZoomer.NAME, "not working. fling");
return;
}

RectF drawRectF = new RectF();
zoomManager.getDrawRect(drawRectF);
scaleDragHelper.getDrawRect(drawRectF);
if (drawRectF.isEmpty()) {
return;
}

Size viewSize = zoomManager.getImageZoomer().getViewSize();
Size viewSize = imageZoomer.getViewSize();
final int imageViewWidth = viewSize.getWidth();
final int imageViewHeight = viewSize.getHeight();

Expand Down Expand Up @@ -73,47 +77,47 @@ void fling(int velocityX, int velocityY) {

// If we actually can move, fling the scroller
if (startX != maxX || startY != maxY) {
mCurrentX = startX;
mCurrentY = startY;
mScroller.fling(startX, startY, velocityX, velocityY, minX,
currentX = startX;
currentY = startY;
scroller.fling(startX, startY, velocityX, velocityY, minX,
maxX, minY, maxY, 0, 0);
}

ImageView imageView = zoomManager.getImageZoomer().getImageView();
ImageView imageView = imageZoomer.getImageView();
imageView.removeCallbacks(this);
imageView.post(this);
}

@Override
public void run() {
// remaining post that should not be handled
if (mScroller.isFinished()) {
if (scroller.isFinished()) {
if (SLog.isLoggable(SLog.LEVEL_DEBUG | SLog.TYPE_ZOOM)) {
SLog.d(ImageZoomer.NAME, "finished. fling run");
}
return;
}

if (!zoomManager.getImageZoomer().isWorking()) {
if (!imageZoomer.isWorking()) {
SLog.w(ImageZoomer.NAME, "not working. fling run");
return;
}

if (!mScroller.computeScrollOffset()) {
if (!scroller.computeScrollOffset()) {
if (SLog.isLoggable(SLog.LEVEL_DEBUG | SLog.TYPE_ZOOM)) {
SLog.d(ImageZoomer.NAME, "scroll finished. fling run");
}
return;
}

final int newX = mScroller.getCurrX();
final int newY = mScroller.getCurrY();
zoomManager.translateBy(mCurrentX - newX, mCurrentY - newY);
mCurrentX = newX;
mCurrentY = newY;
final int newX = scroller.getCurrX();
final int newY = scroller.getCurrY();
scaleDragHelper.translateBy(currentX - newX, currentY - newY);
currentX = newX;
currentY = newY;

// Post On animation
CompatUtils.postOnAnimation(zoomManager.getImageZoomer().getImageView(), this);
SketchUtils.postOnAnimation(imageZoomer.getImageView(), this);
}

@SuppressWarnings("WeakerAccess")
Expand All @@ -122,10 +126,10 @@ public void cancelFling() {
SLog.d(ImageZoomer.NAME, "cancel fling");
}

if (mScroller != null) {
mScroller.forceFinished(true);
if (scroller != null) {
scroller.forceFinished(true);
}
ImageView imageView = zoomManager.getImageZoomer().getImageView();
ImageView imageView = imageZoomer.getImageView();
if (imageView != null) {
imageView.removeCallbacks(this);
}
Expand Down
Loading

0 comments on commit 6782447

Please sign in to comment.