-
-
Save alabiboo/eff2aba6d6ecc93d842950b0ef36a9a4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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