Skip to content

Commit

Permalink
maintainZoomAfterSetImage added to public API.
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeOrtiz committed Dec 17, 2013
1 parent cb70fd5 commit 50a7158
Showing 1 changed file with 43 additions and 5 deletions.
48 changes: 43 additions & 5 deletions src/com/example/touch/TouchImageView.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ public static enum State { NONE, DRAG, ZOOM, FLING, ANIMATE_ZOOM };
//
private float matchViewWidth, matchViewHeight, prevMatchViewWidth, prevMatchViewHeight;

//
// After setting image, a value of true means the new image should maintain
// the zoom of the previous image. False means it should be resized within the view.
//
private boolean maintainZoomAfterSetImage;

//
// True when maintainZoomAfterSetImage has been set to true and setImage has been called.
//
private boolean setImageCalledRecenterImage;

private ScaleGestureDetector mScaleDetector;
private GestureDetector mGestureDetector;

Expand Down Expand Up @@ -115,6 +126,7 @@ private void sharedConstructing(Context context) {
maxScale = 3;
superMinScale = SUPER_MIN_MULTIPLIER * minScale;
superMaxScale = SUPER_MAX_MULTIPLIER * maxScale;
maintainZoomAfterSetImage = true;
setImageMatrix(matrix);
setScaleType(ScaleType.MATRIX);
setState(NONE);
Expand All @@ -124,25 +136,39 @@ private void sharedConstructing(Context context) {
@Override
public void setImageResource(int resId) {
super.setImageResource(resId);
setImageCalled();
savePreviousImageValues();
fitImageToView();
}

@Override
public void setImageBitmap(Bitmap bm) {
super.setImageBitmap(bm);
setImageCalled();
savePreviousImageValues();
fitImageToView();
}

@Override
public void setImageDrawable(Drawable drawable) {
super.setImageDrawable(drawable);
setImageCalled();
savePreviousImageValues();
fitImageToView();
}

@Override
public void setImageURI(Uri uri) {
super.setImageURI(uri);
setImageCalled();
savePreviousImageValues();
fitImageToView();
}

private void setImageCalled() {
if (!maintainZoomAfterSetImage) {
setImageCalledRecenterImage = true;
}
}

/**
Expand All @@ -156,7 +182,7 @@ private void savePreviousImageValues() {
prevMatchViewHeight = matchViewHeight;
prevMatchViewWidth = matchViewWidth;
prevViewHeight = viewHeight;
prevViewWidth = viewHeight;
prevViewWidth = viewWidth;
}
}

Expand Down Expand Up @@ -217,6 +243,16 @@ public float getMinZoom() {
return minScale;
}

/**
* After setting image, a value of true means the new image should maintain
* the zoom of the previous image. False means the image should be resized within
* the view.
* @param maintainZoom
*/
public void maintainZoomAfterSetImage(boolean maintainZoom) {
maintainZoomAfterSetImage = maintainZoom;
}

/**
* Get the current zoom. This is the zoom relative to the initial
* scale, not the original resource.
Expand Down Expand Up @@ -364,6 +400,9 @@ private void fitImageToView() {
if (drawable == null || drawable.getIntrinsicWidth() == 0 || drawable.getIntrinsicHeight() == 0) {
return;
}
if (matrix == null || prevMatrix == null) {
return;
}

int drawableWidth = drawable.getIntrinsicWidth();
int drawableHeight = drawable.getIntrinsicHeight();
Expand All @@ -382,13 +421,14 @@ private void fitImageToView() {
float redundantXSpace = viewWidth - (scale * drawableWidth);
matchViewWidth = viewWidth - redundantXSpace;
matchViewHeight = viewHeight - redundantYSpace;

if (normalizedScale == 1) {
if (normalizedScale == 1 || setImageCalledRecenterImage) {
//
// Stretch and center image to fit view
//
matrix.setScale(scale, scale);
matrix.postTranslate(redundantXSpace / 2, redundantYSpace / 2);
normalizedScale = 1;
setImageCalledRecenterImage = false;

} else {
prevMatrix.getValues(m);
Expand Down Expand Up @@ -424,7 +464,6 @@ private void fitImageToView() {
//
matrix.setValues(m);
}
fixTrans();
setImageMatrix(matrix);
}

Expand Down Expand Up @@ -594,7 +633,6 @@ public boolean onTouch(View v, MotionEvent event) {
}

setImageMatrix(matrix);
invalidate();
//
// indicate event was handled
//
Expand Down

0 comments on commit 50a7158

Please sign in to comment.