Skip to content

Instantly share code, notes, and snippets.

@strombringer
Created March 15, 2018 12:37
Show Gist options
  • Save strombringer/09bb0a2896986641a612ce7d38e14f36 to your computer and use it in GitHub Desktop.
Save strombringer/09bb0a2896986641a612ce7d38e14f36 to your computer and use it in GitHub Desktop.
Custom ZoomPanLayout for https://github.com/moagrius/TileView/ that allows restricting the scroll bounds with an arbitrary rectangle
public class ZoomPanLayout extends ViewGroup implements
GestureDetector.OnGestureListener,
GestureDetector.OnDoubleTapListener,
ScaleGestureDetector.OnScaleGestureListener,
TouchUpGestureDetector.OnTouchUpListener {
private boolean isFinderActive(){
// return true if FinderView is available and visible
}
private Rect getFinderRect() {
// return a Rect that represents the bounds of the visible FinderView
}
private void calculateMinimumScaleToFit() {
int calcWidth = getWidth();
int calcHeight = getHeight();
if (isFinderActive()) {
calcWidth = (int) getFinderRect().width();
calcHeight = (int) getFinderRect().height();
}
mMinimumScaleX = calcWidth / (float) mBaseWidth;
mMinimumScaleY = calcHeight / (float) mBaseHeight;
float recalculatedMinScale = calculatedMinScale(mMinimumScaleX, mMinimumScaleY);
if (recalculatedMinScale != mEffectiveMinScale) {
mEffectiveMinScale = recalculatedMinScale;
if (mScale < mEffectiveMinScale) {
setScale(mEffectiveMinScale);
}
}
}
protected int getScrollLimitX() {
if (isFinderActive())
return mScaledWidth - (int) getFinderRect().right + getOffsetX();
return mScaledWidth - getWidth();
}
protected int getScrollLimitY() {
if (isFinderActive())
return mScaledHeight - (int) getFinderRect().bottom + getOffsetY();
return mScaledHeight - getHeight();
}
protected int getScrollMinX() {
if (isFinderActive())
return -(int) getFinderRect().left + getOffsetX();
return 0;
}
protected int getScrollMinY() {
if (isFinderActive())
return -(int) getFinderRect().top + getOffsetY();
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment