Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
sephiroth74 committed Sep 16, 2014
1 parent a94ef3b commit f93c9ab
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 79 deletions.
16 changes: 8 additions & 8 deletions demo/res/layout/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
android:layout_height="fill_parent"
android:orientation="vertical">

<it.sephiroth.android.library.imagezoom.test.ImageViewOverlay
android:padding="0dip"
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />

<!--<it.sephiroth.android.library.imagezoom.ImageViewTouch-->
<!--<it.sephiroth.android.library.imagezoom.test.ImageViewOverlay-->
<!--android:padding="0dip"-->
<!--android:id="@+id/image"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="0dp"-->
<!--android:layout_weight="1" />-->

<it.sephiroth.android.library.imagezoom.ImageViewTouch
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />

<LinearLayout
android:layout_weight="0"
android:layout_width="match_parent"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public void selectRandomImage(boolean small) {
Log.d("image", imageUri.toString());

final DisplayMetrics metrics = getResources().getDisplayMetrics();
int size = (int) (Math.min(metrics.widthPixels, metrics.heightPixels) / 0.45);
int size = (int) (Math.min(metrics.widthPixels, metrics.heightPixels) / 0.55);

if (small) {
size /= 3;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,10 @@ public boolean canScroll(){
* negative value means scroll from left to right
* @return true if there is some more place to scroll, false - otherwise.
*/
@SuppressWarnings("unused")
public boolean canScroll(int direction) {
RectF bitmapRect = getBitmapRect();
updateRect(bitmapRect, mScrollRect);
updateRect(bitmapRect, mScrollPoint);
Rect imageViewRect = new Rect();
getGlobalVisibleRect(imageViewRect);

Expand All @@ -264,7 +265,7 @@ public boolean canScroll(int direction) {
}
}

double bitmapScrollRectDelta = Math.abs(bitmapRect.left - mScrollRect.left);
double bitmapScrollRectDelta = Math.abs(bitmapRect.left - mScrollPoint.x);
return bitmapScrollRectDelta > SCROLL_DELTA_THRESHOLD;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ public enum DisplayType {
protected final Matrix mDisplayMatrix = new Matrix();
protected final float[] mMatrixValues = new float[9];

protected PointF mCenter = new PointF();

protected DisplayType mScaleType = DisplayType.FIT_IF_BIGGER;
protected boolean mScaleTypeChanged;
Expand All @@ -106,11 +105,15 @@ public enum DisplayType {
protected int mMinFlingVelocity;
protected int mMaxFlingVelocity;

protected PointF mCenter = new PointF();
protected RectF mBitmapRect = new RectF();
protected RectF mBitmapRectTmp = new RectF();
protected RectF mCenterRect = new RectF();
protected RectF mScrollRect = new RectF();
protected RectF mViewPort;
protected RectF mViewPortOld;
protected PointF mScrollPoint = new PointF();
protected RectF mViewPort = new RectF();
protected RectF mViewPortOld = new RectF();

private Animator mCurrentAnimation;

private OnDrawableChangeListener mDrawableChangeListener;
private OnLayoutChangeListener mOnLayoutChangeListener;
Expand Down Expand Up @@ -141,15 +144,10 @@ public void setOnLayoutChangeListener(OnLayoutChangeListener listener) {
}

protected void init(Context context, AttributeSet attrs, int defStyle) {

ViewConfiguration configuration = ViewConfiguration.get(context);
mMinFlingVelocity = configuration.getScaledMinimumFlingVelocity();
mMaxFlingVelocity = configuration.getScaledMaximumFlingVelocity();

mDefaultAnimationDuration = getResources().getInteger(android.R.integer.config_shortAnimTime);
mViewPort = new RectF();
mViewPortOld = new RectF();

setScaleType(ImageView.ScaleType.MATRIX);
}

Expand Down Expand Up @@ -220,7 +218,7 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
float deltaX = 0;
float deltaY = 0;

if(changed) {
if (changed) {
mViewPortOld.set(mViewPort);
onViewPortChanged(left, top, right, bottom);

Expand Down Expand Up @@ -365,15 +363,15 @@ else if (changed) {
protected void onConfigurationChanged(final Configuration newConfig) {
super.onConfigurationChanged(newConfig);

if(DEBUG){
if (DEBUG) {
Log.i(TAG, "onConfigurationChanged. scale: " + getScale() + ", minScale: " + getMinScale() + ", mUserScaled: " + mUserScaled);
}

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

if(DEBUG){
if (DEBUG) {
Log.v(TAG, "mUserScaled: " + mUserScaled);
}
}
Expand Down Expand Up @@ -479,22 +477,8 @@ public void run() {
}

protected void _setImageDrawable(final Drawable drawable, final Matrix initial_matrix, float min_zoom, float max_zoom) {

if (DEBUG) {
Log.i(TAG, "_setImageDrawable");
}

mBaseMatrix.reset();

if (drawable != null) {
if (DEBUG) {
Log.d(TAG, "size: " + drawable.getIntrinsicWidth() + "x" + drawable.getIntrinsicHeight());
}
super.setImageDrawable(drawable);
}
else {
super.setImageDrawable(null);
}
super.setImageDrawable(drawable);

if (min_zoom != ZOOM_INVALID && max_zoom != ZOOM_INVALID) {
min_zoom = Math.min(min_zoom, max_zoom);
Expand Down Expand Up @@ -527,18 +511,23 @@ protected void _setImageDrawable(final Drawable drawable, final Matrix initial_m
mMaxZoomDefined = false;
}

if (initial_matrix != null) {
mNextMatrix = new Matrix(initial_matrix);
}

if (DEBUG) {
Log.v(TAG, "mMinZoom: " + mMinZoom + ", mMaxZoom: " + mMaxZoom);
}
if (initial_matrix != null) mNextMatrix = new Matrix(initial_matrix);
if (DEBUG) Log.v(TAG, "mMinZoom: " + mMinZoom + ", mMaxZoom: " + mMaxZoom);

mBitmapChanged = true;
updateDrawable(drawable);
requestLayout();
}

protected void updateDrawable(Drawable newDrawable) {
if (null != newDrawable) {
mBitmapRect.set(0, 0, newDrawable.getIntrinsicWidth(), newDrawable.getIntrinsicHeight());
}
else {
mBitmapRect.setEmpty();
}
}

/**
* Fired as soon as a new Bitmap has been set
*
Expand Down Expand Up @@ -584,9 +573,8 @@ protected void onLayoutChanged(int left, int top, int right, int bottom) {
protected float computeMaxZoom() {
final Drawable drawable = getDrawable();
if (drawable == null) return 1f;

float fw = (float) drawable.getIntrinsicWidth() / mViewPort.width();
float fh = (float) drawable.getIntrinsicHeight() / mViewPort.height();
float fw = mBitmapRect.width() / mViewPort.width();
float fh = mBitmapRect.height() / mViewPort.height();
float scale = Math.max(fw, fh) * 4;

if (DEBUG) {
Expand Down Expand Up @@ -663,7 +651,6 @@ public Matrix getImageViewMatrix(Matrix supportMatrix) {

@Override
public void setImageMatrix(Matrix matrix) {

Matrix current = getImageMatrix();
boolean needUpdate = false;

Expand All @@ -672,7 +659,6 @@ public void setImageMatrix(Matrix matrix) {
}

super.setImageMatrix(matrix);

if (needUpdate) onImageMatrixChanged();
}

Expand Down Expand Up @@ -701,8 +687,8 @@ public Matrix getDisplayMatrix() {
}

protected void getProperBaseMatrix(Drawable drawable, Matrix matrix, RectF rect) {
float w = drawable.getIntrinsicWidth();
float h = drawable.getIntrinsicHeight();
float w = mBitmapRect.width();
float h = mBitmapRect.height();
float widthScale, heightScale;

matrix.reset();
Expand Down Expand Up @@ -737,13 +723,9 @@ public RectF getBitmapRect() {
}

protected RectF getBitmapRect(Matrix supportMatrix) {
final Drawable drawable = getDrawable();

if (drawable == null) return null;
Matrix m = getImageViewMatrix(supportMatrix);
mBitmapRect.set(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
m.mapRect(mBitmapRect);
return mBitmapRect;
m.mapRect(mBitmapRectTmp, mBitmapRect);
return mBitmapRectTmp;
}

protected float getScale(Matrix matrix) {
Expand Down Expand Up @@ -805,7 +787,7 @@ else if (rect.bottom < mViewPort.bottom) {
deltaX = (mViewPort.width() - width) / 2 - (rect.left - mViewPort.left);
}
else if (rect.left > mViewPort.left) {
deltaX = -(rect.left - mViewPort.left);
deltaX = - (rect.left - mViewPort.left);
}
else if (rect.right < mViewPort.right) {
deltaX = mViewPort.right - rect.right;
Expand Down Expand Up @@ -869,8 +851,10 @@ protected void zoomTo(float scale, float centerX, float centerY) {
center(true, true);
}

@SuppressWarnings ("unused")
protected void onZoom(float scale) {}

@SuppressWarnings ("unused")
protected void onZoomAnimationCompleted(float scale) {}

/**
Expand All @@ -885,32 +869,28 @@ public void scrollBy(float x, float y) {

protected void panBy(double dx, double dy) {
RectF rect = getBitmapRect();
mScrollRect.set((float) dx, (float) dy, 0, 0);
updateRect(rect, mScrollRect);

//Log.v(TAG, "panBy: " + mScrollRect);
mScrollPoint.set((float) dx, (float) dy);
updateRect(rect, mScrollPoint);

if(mScrollRect.left != 0 || mScrollRect.top != 0) {
postTranslate(mScrollRect.left, mScrollRect.top);
if (mScrollPoint.x != 0 || mScrollPoint.y != 0) {
postTranslate(mScrollPoint.x, mScrollPoint.y);
center(true, true);
}
}

protected void updateRect(RectF bitmapRect, RectF scrollRect) {
protected void updateRect(RectF bitmapRect, PointF scrollRect) {
if (bitmapRect == null) return;

// if (bitmapRect.top >= 0 && bitmapRect.bottom <= mThisHeight) scrollRect.top = 0;
// if (bitmapRect.left >= 0 && bitmapRect.right <= mThisWidth) scrollRect.left = 0;
// if (bitmapRect.top + scrollRect.top >= 0 && bitmapRect.bottom > mThisHeight) scrollRect.top = (int) (0 - bitmapRect.top);
// if (bitmapRect.bottom + scrollRect.top <= (mThisHeight - 0) && bitmapRect.top < 0) scrollRect.top = (int) ((mThisHeight - 0) - bitmapRect.bottom);
// if (bitmapRect.left + scrollRect.left >= 0) scrollRect.left = (int) (0 - bitmapRect.left);
// if (bitmapRect.right + scrollRect.left <= (mThisWidth - 0)) scrollRect.left = (int) ((mThisWidth - 0) - bitmapRect.right);
// if (bitmapRect.top >= 0 && bitmapRect.bottom <= mThisHeight) scrollRect.top = 0;
// if (bitmapRect.left >= 0 && bitmapRect.right <= mThisWidth) scrollRect.left = 0;
// if (bitmapRect.top + scrollRect.top >= 0 && bitmapRect.bottom > mThisHeight) scrollRect.top = (int) (0 - bitmapRect.top);
// if (bitmapRect.bottom + scrollRect.top <= (mThisHeight - 0) && bitmapRect.top < 0) scrollRect.top = (int) ((mThisHeight - 0) - bitmapRect
// .bottom);
// if (bitmapRect.left + scrollRect.left >= 0) scrollRect.left = (int) (0 - bitmapRect.left);
// if (bitmapRect.right + scrollRect.left <= (mThisWidth - 0)) scrollRect.left = (int) ((mThisWidth - 0) - bitmapRect.right);
}

Animator mCurrentAnimation;

protected void stopAllAnimations(){
if(null != mCurrentAnimation){
protected void stopAllAnimations() {
if (null != mCurrentAnimation) {
mCurrentAnimation.cancel();
mCurrentAnimation = null;
}
Expand All @@ -923,7 +903,7 @@ protected void scrollBy(float distanceX, float distanceY, final long durationMs)
stopAllAnimations();

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

Expand Down

0 comments on commit f93c9ab

Please sign in to comment.