Skip to content

Commit

Permalink
Merge pull request #4361 from salachi/TIMOB-13494-ImageViewScroll
Browse files Browse the repository at this point in the history
TIMOB-13494-make sure that image doesn't scroll beyond it boundaries
  • Loading branch information
ayeung committed Jun 7, 2013
2 parents cc36fce + 48a0ed0 commit 05fe951
Showing 1 changed file with 30 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,19 @@ public boolean onDown(MotionEvent e)
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float dx, float dy)
{
if (zoomControls.getVisibility() == View.VISIBLE) {
changeMatrix.postTranslate(-dx, -dy);
imageView.setImageMatrix(getViewMatrix());
requestLayout();
scheduleControlTimeout();
return true;
} else {
return false;
boolean retValue = false;
// Allow scrolling only if the image is zoomed in
if (zoomControls.getVisibility() == View.VISIBLE && scaleFactor > 1) {
// check if image scroll beyond its borders
if (!checkImageScrollBeyondBorders(dx, dy)) {
changeMatrix.postTranslate(-dx, -dy);
imageView.setImageMatrix(getViewMatrix());
requestLayout();
scheduleControlTimeout();
retValue = true;
}
}
return retValue;
}

@Override
Expand Down Expand Up @@ -445,4 +449,22 @@ public void setOrientation(int orientation)
this.orientation = orientation;
updateScaleType();
}

private boolean checkImageScrollBeyondBorders(float dx, float dy)
{
float[] matrixValues = new float[9];
Matrix m = new Matrix(changeMatrix);
// Apply the translation
m.postTranslate(-dx, -dy);
m.getValues(matrixValues);
// Image can move only the extra width or height that is available
// after scaling from the original width or height
float scaledAdditionalHeight = imageView.getHeight() * (matrixValues[4] - 1);
float scaledAdditionalWidth = imageView.getWidth() * (matrixValues[0] - 1);
if (matrixValues[5] > -scaledAdditionalHeight && matrixValues[5] < 0 && matrixValues[2] > -scaledAdditionalWidth
&& matrixValues[2] < 0) {
return false;
}
return true;
}
}

0 comments on commit 05fe951

Please sign in to comment.