Skip to content

Commit

Permalink
animations. handle configuration changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sephiroth74 committed Sep 9, 2014
1 parent 0ca6fd2 commit 05b834d
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public void selectRandomImage(boolean small) {
if (null != bitmap) {
Log.d(LOG_TAG, "screen size: " + metrics.widthPixels + "x" + metrics.heightPixels);
Log.d(LOG_TAG, "bitmap size: " + bitmap.getWidth() + "x" + bitmap.getHeight());
mImage.setImageBitmap(bitmap, null, - 1, 8f);
mImage.setImageBitmap(bitmap, null, -1, -1);

mImage.setOnDrawableChangedListener(
new OnDrawableChangeListener() {
Expand Down
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ apply plugin: 'signing'


group 'it.sephiroth.android.library.imagezoom'
version '2.0.0-SNAPSHOT'
version '2.0.1-SNAPSHOT'


dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ protected void onZoomAnimationCompleted(float scale) {
}
}

protected float onDoubleTapPost(float scale, float maxZoom) {
protected float onDoubleTapPost(float scale, final float maxZoom, final float minScale) {
if (mDoubleTapDirection == 1) {
if ((scale + (mScaleFactor * 2)) <= maxZoom) {
return scale + mScaleFactor;
Expand All @@ -135,7 +135,7 @@ protected float onDoubleTapPost(float scale, float maxZoom) {
}
else {
mDoubleTapDirection = 1;
return 1f;
return minScale;
}
}

Expand All @@ -152,13 +152,31 @@ public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float d
}

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (Math.abs(velocityX) > mMinFlingVelocity * 4 || Math.abs(velocityY) > mMinFlingVelocity * 4) {
if (Math.abs(velocityX) > (mMinFlingVelocity * 4) || Math.abs(velocityY) > (mMinFlingVelocity * 4)) {

if (LOG_ENABLED) {
Log.i(LOG_TAG, "onFling");
Log.v(LOG_TAG, "velocity: " + velocityY);
Log.v(LOG_TAG, "diff: " + (e2.getY() - e1.getY()));
}

final float scale = Math.min(Math.max(2f, getScale() / 2), 3.f);

float scaledDistanceX = ((velocityX) / mMaxFlingVelocity) * (getWidth() * scale);
float scaledDistanceY = ((velocityY) / mMaxFlingVelocity) * (getHeight() * scale);


if (LOG_ENABLED) {
Log.v(LOG_TAG, "scale: " + getScale() + ", scale_final: " + scale);
Log.v(LOG_TAG, "scaledDistanceX: " + scaledDistanceX);
Log.v(LOG_TAG, "scaledDistanceY: " + scaledDistanceY);
}

mUserScaled = true;
float scale = Math.max(1, getScale());
final float x = ((velocityX * scale / mMaxFlingVelocity) * getWidth()) / scale;
final float y = ((velocityY * scale / mMaxFlingVelocity) * getHeight()) / scale;

scrollBy(x, y, mDefaultAnimationDuration);
double total = Math.sqrt(Math.pow(scaledDistanceX, 2) + Math.pow(scaledDistanceY, 2));

scrollBy(scaledDistanceX, scaledDistanceY, (long) Math.min(Math.max(300, total / 5), 800));

postInvalidate();
return true;
Expand Down Expand Up @@ -232,7 +250,7 @@ public boolean onDoubleTap(MotionEvent e) {
mUserScaled = true;
float scale = getScale();
float targetScale;
targetScale = onDoubleTapPost(scale, getMaxScale());
targetScale = onDoubleTapPost(scale, getMaxScale(), getMinScale());
targetScale = Math.min(getMaxScale(), Math.max(targetScale, getMinScale()));
zoomTo(targetScale, e.getX(), e.getY(), mDefaultAnimationDuration);
}
Expand Down Expand Up @@ -282,6 +300,12 @@ public boolean onSingleTapUp(MotionEvent e) {

@Override
public boolean onDown(MotionEvent e) {
if (LOG_ENABLED) {
Log.i(LOG_TAG, "onDown");
}
stopAllAnimations();


return ImageViewTouch.this.onDown(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ViewConfiguration;
Expand Down Expand Up @@ -80,8 +80,7 @@ public enum DisplayType {
protected static final boolean LOG_ENABLED = false;

public static final float ZOOM_INVALID = - 1f;

protected final Handler mHandler = new Handler();
public static final long DEFAULT_ANIMATION_DURATION = 200;

protected Matrix mBaseMatrix = new Matrix();
protected Matrix mSuppMatrix = new Matrix();
Expand Down Expand Up @@ -316,7 +315,7 @@ else if (changed) {
zoomTo(scale);
}
else {
if (Math.abs(old_scale - old_min_scale) > 0.001) {
if (Math.abs(old_scale - old_min_scale) > 0.1) {
scale = (old_matrix_scale / new_matrix_scale) * old_scale;
}
if (LOG_ENABLED) {
Expand All @@ -330,8 +329,6 @@ else if (changed) {
Log.d(LOG_TAG, "old scale: " + old_scale);
Log.d(LOG_TAG, "new scale: " + scale);
}


}

mUserScaled = false;
Expand All @@ -351,7 +348,7 @@ else if (changed) {
if (mBitmapChanged) mBitmapChanged = false;

if (LOG_ENABLED) {
Log.d(LOG_TAG, "new scale: " + getScale());
Log.d(LOG_TAG, "scale: " + getScale() + ", minScale: " + getMinScale() + ", maxScale: " + getMaxScale());
}
}
}
Expand All @@ -362,7 +359,23 @@ else if (changed) {

if (mBitmapChanged) mBitmapChanged = false;
if (mScaleTypeChanged) mScaleTypeChanged = false;
}
}

@Override
protected void onConfigurationChanged(final Configuration newConfig) {
super.onConfigurationChanged(newConfig);

if(LOG_ENABLED){
Log.i(LOG_TAG, "onConfigurationChanged. scale: " + getScale() + ", minScale: " + getMinScale());
}

if(mUserScaled) {
mUserScaled = Math.abs(getScale() - getMinScale()) > 0.1f;
}

if(LOG_ENABLED){
Log.v(LOG_TAG, "mUserScaled: " + mUserScaled);
}
}

Expand Down Expand Up @@ -821,10 +834,6 @@ protected void center(boolean horizontal, boolean vertical) {
RectF rect = getCenter(mSuppMatrix, horizontal, vertical);

if (rect.left != 0 || rect.top != 0) {

if (LOG_ENABLED) {
Log.i(LOG_TAG, "center");
}
postTranslate(rect.left, rect.top);
}
}
Expand Down Expand Up @@ -869,18 +878,12 @@ else if (rect.right < viewWidth) {

protected void postTranslate(float deltaX, float deltaY) {
if (deltaX != 0 || deltaY != 0) {
if (LOG_ENABLED) {
Log.i(LOG_TAG, "postTranslate: " + deltaX + "x" + deltaY);
}
mSuppMatrix.postTranslate(deltaX, deltaY);
setImageMatrix(getImageViewMatrix());
}
}

protected void postScale(float scale, float centerX, float centerY) {
if (LOG_ENABLED) {
Log.i(LOG_TAG, "postScale: " + scale + ", center: " + centerX + "x" + centerY);
}
mSuppMatrix.postScale(scale, scale, centerX, centerY);
setImageMatrix(getImageViewMatrix());
}
Expand Down Expand Up @@ -960,18 +963,29 @@ protected void updateRect(RectF bitmapRect, RectF scrollRect) {
if (bitmapRect.right + scrollRect.left <= (mThisWidth - 0)) scrollRect.left = (int) ((mThisWidth - 0) - bitmapRect.right);
}

Animator mCurrentAnimation;

protected void stopAllAnimations(){
if(null != mCurrentAnimation){
mCurrentAnimation.cancel();
mCurrentAnimation = null;
}
}

protected void scrollBy(float distanceX, float distanceY, final long durationMs) {
final ValueAnimator anim1 = ValueAnimator.ofFloat(0, distanceX).setDuration(durationMs);
final ValueAnimator anim2 = ValueAnimator.ofFloat(0, distanceY).setDuration(durationMs);

AnimatorSet set = new AnimatorSet();
set.playTogether(
stopAllAnimations();

mCurrentAnimation = new AnimatorSet();
((AnimatorSet)mCurrentAnimation).playTogether(
anim1, anim2
);

set.setDuration(durationMs);
set.setInterpolator(new DecelerateInterpolator());
set.start();
mCurrentAnimation.setDuration(durationMs);
mCurrentAnimation.setInterpolator(new DecelerateInterpolator());
mCurrentAnimation.start();

anim2.addUpdateListener(
new ValueAnimator.AnimatorUpdateListener() {
Expand All @@ -991,7 +1005,7 @@ public void onAnimationUpdate(final ValueAnimator animation) {
}
);

set.addListener(
mCurrentAnimation.addListener(
new Animator.AnimatorListener() {
@Override
public void onAnimationStart(final Animator animation) {
Expand Down Expand Up @@ -1020,11 +1034,8 @@ public void onAnimationRepeat(final Animator animation) {
protected void zoomTo(float scale, float centerX, float centerY, final long durationMs) {
if (scale > getMaxScale()) scale = getMaxScale();

final long startTime = System.currentTimeMillis();
final float oldScale = getScale();

final float deltaScale = scale - oldScale;

Matrix m = new Matrix(mSuppMatrix);
m.postScale(scale, scale, centerX, centerY);
RectF rect = getCenter(m, true, true);
Expand All @@ -1033,10 +1044,12 @@ protected void zoomTo(float scale, float centerX, float centerY, final long dura
final float destX = centerX + rect.left * scale;
final float destY = centerY + rect.top * scale;

ValueAnimator animator = ValueAnimator.ofFloat(oldScale, finalScale);
animator.setDuration(durationMs);
animator.setInterpolator(new DecelerateInterpolator(1.0f));
animator.addUpdateListener(
stopAllAnimations();

ValueAnimator animation = ValueAnimator.ofFloat(oldScale, finalScale);
animation.setDuration(durationMs);
animation.setInterpolator(new DecelerateInterpolator(1.0f));
animation.addUpdateListener(
new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(final ValueAnimator animation) {
Expand All @@ -1045,7 +1058,7 @@ public void onAnimationUpdate(final ValueAnimator animation) {
}
}
);
animator.start();
animation.start();
}

@Override
Expand Down

0 comments on commit 05b834d

Please sign in to comment.