Skip to content

Instantly share code, notes, and snippets.

@mitchtabian
Last active October 14, 2022 15:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mitchtabian/e4826c9a746d7f440f239292ccf11f2f to your computer and use it in GitHub Desktop.
Save mitchtabian/e4826c9a746d7f440f239292ccf11f2f to your computer and use it in GitHub Desktop.
public void fitToScreen(){
mSaveScale = 1;
float scale;
Drawable drawable = getDrawable();
if (drawable == null || drawable.getIntrinsicWidth() == 0
|| drawable.getIntrinsicHeight() == 0)
return;
int bmWidth = drawable.getIntrinsicWidth();
int bmHeight = drawable.getIntrinsicHeight();
Log.d(TAG, "bmWidth: " + bmWidth + " bmHeight : " + bmHeight);
float scaleX = (float) viewWidth / (float) bmWidth;
float scaleY = (float) viewHeight / (float) bmHeight;
scale = Math.min(scaleX, scaleY);
mMatrix.setScale(scale, scale);
// Center the image
float redundantYSpace = (float) viewHeight
- (scale * (float) bmHeight);
float redundantXSpace = (float) viewWidth
- (scale * (float) bmWidth);
redundantYSpace /= (float) 2;
redundantXSpace /= (float) 2;
Log.d(TAG, "fitToScreen: redundantXSpace: " + redundantXSpace);
Log.d(TAG, "fitToScreen: redundantYSpace: " + redundantYSpace);
mMatrix.postTranslate(redundantXSpace, redundantYSpace);
origWidth = viewWidth - 2 * redundantXSpace;
origHeight = viewHeight - 2 * redundantYSpace;
setImageMatrix(mMatrix);
}
void fixTranslation() {
mMatrix.getValues(mMatrixValues); //put matrix values into a float array so we can analyze
float transX = mMatrixValues[Matrix.MTRANS_X]; //get the most recent translation in x direction
float transY = mMatrixValues[Matrix.MTRANS_Y]; //get the most recent translation in y direction
float fixTransX = getFixTranslation(transX, viewWidth, origWidth * mSaveScale);
float fixTransY = getFixTranslation(transY, viewHeight, origHeight * mSaveScale);
if (fixTransX != 0 || fixTransY != 0)
mMatrix.postTranslate(fixTransX, fixTransY);
}
float getFixTranslation(float trans, float viewSize, float contentSize) {
float minTrans, maxTrans;
if (contentSize <= viewSize) { // case: NOT ZOOMED
minTrans = 0;
maxTrans = viewSize - contentSize;
}
else { //CASE: ZOOMED
minTrans = viewSize - contentSize;
maxTrans = 0;
}
if (trans < minTrans) { // negative x or y translation (down or to the right)
Log.d(TAG, "getFixTranslation: minTrans: " + minTrans + ", trans: " + trans +
"\ndifference: " + (-trans + minTrans));
return -trans + minTrans;
}
if (trans > maxTrans) { // positive x or y translation (up or to the left)
Log.d(TAG, "getFixTranslation: maxTrans: " + maxTrans + ", trans: " + trans +
"\ndifference: " + (-trans + maxTrans));
return -trans + maxTrans;
}
return 0;
}
PointF mLast = new PointF();
PointF mStart = new PointF();
@Override
public boolean onTouch(View view, MotionEvent event) {
mScaleDetector.onTouchEvent(event);
mGestureDetector.onTouchEvent(event);
PointF curr = new PointF(event.getX(), event.getY());
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mLast.set(curr);
mStart.set(mLast);
mode = DRAG;
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
float dx = curr.x - mLast.x;
float dy = curr.y - mLast.y;
mMatrix.postTranslate(dx, dy);
mLast.set(curr.x, curr.y);
}
break;
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
break;
}
setImageMatrix(mMatrix);
return false;
}
// Image States
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;
// Scales
float mSaveScale = 1f;
float mMinScale = 1f;
float mMaxScale = 4f;
// view dimensions
float origWidth, origHeight;
int viewWidth, viewHeight;
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
mode = ZOOM;
return true;
}
@Override
public boolean onScale(ScaleGestureDetector detector) {
Log.d(TAG, "onScale: "+ detector.getScaleFactor());
float mScaleFactor = detector.getScaleFactor();
float origScale = mSaveScale;
mSaveScale *= mScaleFactor;
if (mSaveScale > mMaxScale) {
mSaveScale = mMaxScale;
mScaleFactor = mMaxScale / origScale;
} else if (mSaveScale < mMinScale) {
mSaveScale = mMinScale;
mScaleFactor = mMinScale / origScale;
}
if (origWidth * mSaveScale <= viewWidth
|| origHeight * mSaveScale <= viewHeight){
mMatrix.postScale(mScaleFactor, mScaleFactor, viewWidth / 2,
viewHeight / 2);
}
else{
mMatrix.postScale(mScaleFactor, mScaleFactor,
detector.getFocusX(), detector.getFocusY());
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment