-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e3c473b
commit f8a3355
Showing
13 changed files
with
1,505 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
photto/src/main/java/com/gungoronline/photto/PhotoView/Compat.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
Copyright 2011, 2012 Chris Banes. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package com.gungoronline.photto.PhotoView; | ||
|
||
import android.annotation.TargetApi; | ||
import android.os.Build.VERSION; | ||
import android.os.Build.VERSION_CODES; | ||
import android.view.View; | ||
|
||
class Compat { | ||
|
||
private static final int SIXTY_FPS_INTERVAL = 1000 / 60; | ||
|
||
public static void postOnAnimation(View view, Runnable runnable) { | ||
if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) { | ||
postOnAnimationJellyBean(view, runnable); | ||
} else { | ||
view.postDelayed(runnable, SIXTY_FPS_INTERVAL); | ||
} | ||
} | ||
|
||
@TargetApi(16) | ||
private static void postOnAnimationJellyBean(View view, Runnable runnable) { | ||
view.postOnAnimation(runnable); | ||
} | ||
} |
205 changes: 205 additions & 0 deletions
205
photto/src/main/java/com/gungoronline/photto/PhotoView/CustomGestureDetector.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
/* | ||
Copyright 2011, 2012 Chris Banes. | ||
<p/> | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
<p/> | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
<p/> | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package com.gungoronline.photto.PhotoView; | ||
|
||
import android.content.Context; | ||
import android.view.MotionEvent; | ||
import android.view.ScaleGestureDetector; | ||
import android.view.VelocityTracker; | ||
import android.view.ViewConfiguration; | ||
|
||
/** | ||
* Does a whole lot of gesture detecting. | ||
*/ | ||
class CustomGestureDetector { | ||
|
||
private static final int INVALID_POINTER_ID = -1; | ||
|
||
private int mActivePointerId = INVALID_POINTER_ID; | ||
private int mActivePointerIndex = 0; | ||
private final ScaleGestureDetector mDetector; | ||
|
||
private VelocityTracker mVelocityTracker; | ||
private boolean mIsDragging; | ||
private float mLastTouchX; | ||
private float mLastTouchY; | ||
private final float mTouchSlop; | ||
private final float mMinimumVelocity; | ||
private OnGestureListener mListener; | ||
|
||
CustomGestureDetector(Context context, OnGestureListener listener) { | ||
final ViewConfiguration configuration = ViewConfiguration | ||
.get(context); | ||
mMinimumVelocity = configuration.getScaledMinimumFlingVelocity(); | ||
mTouchSlop = configuration.getScaledTouchSlop(); | ||
|
||
mListener = listener; | ||
ScaleGestureDetector.OnScaleGestureListener mScaleListener = new ScaleGestureDetector.OnScaleGestureListener() { | ||
|
||
@Override | ||
public boolean onScale(ScaleGestureDetector detector) { | ||
float scaleFactor = detector.getScaleFactor(); | ||
|
||
if (Float.isNaN(scaleFactor) || Float.isInfinite(scaleFactor)) | ||
return false; | ||
|
||
if (scaleFactor >= 0) { | ||
mListener.onScale(scaleFactor, | ||
detector.getFocusX(), detector.getFocusY()); | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean onScaleBegin(ScaleGestureDetector detector) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void onScaleEnd(ScaleGestureDetector detector) { | ||
// NO-OP | ||
} | ||
}; | ||
mDetector = new ScaleGestureDetector(context, mScaleListener); | ||
} | ||
|
||
private float getActiveX(MotionEvent ev) { | ||
try { | ||
return ev.getX(mActivePointerIndex); | ||
} catch (Exception e) { | ||
return ev.getX(); | ||
} | ||
} | ||
|
||
private float getActiveY(MotionEvent ev) { | ||
try { | ||
return ev.getY(mActivePointerIndex); | ||
} catch (Exception e) { | ||
return ev.getY(); | ||
} | ||
} | ||
|
||
public boolean isScaling() { | ||
return mDetector.isInProgress(); | ||
} | ||
|
||
public boolean isDragging() { | ||
return mIsDragging; | ||
} | ||
|
||
public boolean onTouchEvent(MotionEvent ev) { | ||
try { | ||
mDetector.onTouchEvent(ev); | ||
return processTouchEvent(ev); | ||
} catch (IllegalArgumentException e) { | ||
// Fix for support lib bug, happening when onDestroy is called | ||
return true; | ||
} | ||
} | ||
|
||
private boolean processTouchEvent(MotionEvent ev) { | ||
final int action = ev.getAction(); | ||
switch (action & MotionEvent.ACTION_MASK) { | ||
case MotionEvent.ACTION_DOWN: | ||
mActivePointerId = ev.getPointerId(0); | ||
|
||
mVelocityTracker = VelocityTracker.obtain(); | ||
if (null != mVelocityTracker) { | ||
mVelocityTracker.addMovement(ev); | ||
} | ||
|
||
mLastTouchX = getActiveX(ev); | ||
mLastTouchY = getActiveY(ev); | ||
mIsDragging = false; | ||
break; | ||
case MotionEvent.ACTION_MOVE: | ||
final float x = getActiveX(ev); | ||
final float y = getActiveY(ev); | ||
final float dx = x - mLastTouchX, dy = y - mLastTouchY; | ||
|
||
if (!mIsDragging) { | ||
// Use Pythagoras to see if drag length is larger than | ||
// touch slop | ||
mIsDragging = Math.sqrt((dx * dx) + (dy * dy)) >= mTouchSlop; | ||
} | ||
|
||
if (mIsDragging) { | ||
mListener.onDrag(dx, dy); | ||
mLastTouchX = x; | ||
mLastTouchY = y; | ||
|
||
if (null != mVelocityTracker) { | ||
mVelocityTracker.addMovement(ev); | ||
} | ||
} | ||
break; | ||
case MotionEvent.ACTION_CANCEL: | ||
mActivePointerId = INVALID_POINTER_ID; | ||
// Recycle Velocity Tracker | ||
if (null != mVelocityTracker) { | ||
mVelocityTracker.recycle(); | ||
mVelocityTracker = null; | ||
} | ||
break; | ||
case MotionEvent.ACTION_UP: | ||
mActivePointerId = INVALID_POINTER_ID; | ||
if (mIsDragging) { | ||
if (null != mVelocityTracker) { | ||
mLastTouchX = getActiveX(ev); | ||
mLastTouchY = getActiveY(ev); | ||
|
||
// Compute velocity within the last 1000ms | ||
mVelocityTracker.addMovement(ev); | ||
mVelocityTracker.computeCurrentVelocity(1000); | ||
|
||
final float vX = mVelocityTracker.getXVelocity(), vY = mVelocityTracker | ||
.getYVelocity(); | ||
|
||
// If the velocity is greater than minVelocity, call | ||
// listener | ||
if (Math.max(Math.abs(vX), Math.abs(vY)) >= mMinimumVelocity) { | ||
mListener.onFling(mLastTouchX, mLastTouchY, -vX, | ||
-vY); | ||
} | ||
} | ||
} | ||
|
||
// Recycle Velocity Tracker | ||
if (null != mVelocityTracker) { | ||
mVelocityTracker.recycle(); | ||
mVelocityTracker = null; | ||
} | ||
break; | ||
case MotionEvent.ACTION_POINTER_UP: | ||
final int pointerIndex = Util.getPointerIndex(ev.getAction()); | ||
final int pointerId = ev.getPointerId(pointerIndex); | ||
if (pointerId == mActivePointerId) { | ||
// This was our active pointer going up. Choose a new | ||
// active pointer and adjust accordingly. | ||
final int newPointerIndex = pointerIndex == 0 ? 1 : 0; | ||
mActivePointerId = ev.getPointerId(newPointerIndex); | ||
mLastTouchX = ev.getX(newPointerIndex); | ||
mLastTouchY = ev.getY(newPointerIndex); | ||
} | ||
break; | ||
} | ||
|
||
mActivePointerIndex = ev | ||
.findPointerIndex(mActivePointerId != INVALID_POINTER_ID ? mActivePointerId | ||
: 0); | ||
return true; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
photto/src/main/java/com/gungoronline/photto/PhotoView/OnGestureListener.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
Copyright 2011, 2012 Chris Banes. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package com.gungoronline.photto.PhotoView; | ||
|
||
interface OnGestureListener { | ||
|
||
void onDrag(float dx, float dy); | ||
|
||
void onFling(float startX, float startY, float velocityX, | ||
float velocityY); | ||
|
||
void onScale(float scaleFactor, float focusX, float focusY); | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
photto/src/main/java/com/gungoronline/photto/PhotoView/OnMatrixChangedListener.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.gungoronline.photto.PhotoView; | ||
|
||
import android.graphics.RectF; | ||
|
||
/** | ||
* Interface definition for a callback to be invoked when the internal Matrix has changed for | ||
* this View. | ||
*/ | ||
public interface OnMatrixChangedListener { | ||
|
||
/** | ||
* Callback for when the Matrix displaying the Drawable has changed. This could be because | ||
* the View's bounds have changed, or the user has zoomed. | ||
* | ||
* @param rect - Rectangle displaying the Drawable's new bounds. | ||
*/ | ||
void onMatrixChanged(RectF rect); | ||
} |
14 changes: 14 additions & 0 deletions
14
photto/src/main/java/com/gungoronline/photto/PhotoView/OnOutsidePhotoTapListener.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.gungoronline.photto.PhotoView; | ||
|
||
import android.widget.ImageView; | ||
|
||
/** | ||
* Callback when the user tapped outside of the photo | ||
*/ | ||
public interface OnOutsidePhotoTapListener { | ||
|
||
/** | ||
* The outside of the photo has been tapped | ||
*/ | ||
void onOutsidePhotoTap(ImageView imageView); | ||
} |
22 changes: 22 additions & 0 deletions
22
photto/src/main/java/com/gungoronline/photto/PhotoView/OnPhotoTapListener.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.gungoronline.photto.PhotoView; | ||
|
||
import android.widget.ImageView; | ||
|
||
/** | ||
* A callback to be invoked when the Photo is tapped with a single | ||
* tap. | ||
*/ | ||
public interface OnPhotoTapListener { | ||
|
||
/** | ||
* A callback to receive where the user taps on a photo. You will only receive a callback if | ||
* the user taps on the actual photo, tapping on 'whitespace' will be ignored. | ||
* | ||
* @param view ImageView the user tapped. | ||
* @param x where the user tapped from the of the Drawable, as percentage of the | ||
* Drawable width. | ||
* @param y where the user tapped from the top of the Drawable, as percentage of the | ||
* Drawable height. | ||
*/ | ||
void onPhotoTap(ImageView view, float x, float y); | ||
} |
17 changes: 17 additions & 0 deletions
17
photto/src/main/java/com/gungoronline/photto/PhotoView/OnScaleChangedListener.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.gungoronline.photto.PhotoView; | ||
|
||
|
||
/** | ||
* Interface definition for callback to be invoked when attached ImageView scale changes | ||
*/ | ||
public interface OnScaleChangedListener { | ||
|
||
/** | ||
* Callback for when the scale changes | ||
* | ||
* @param scaleFactor the scale factor (less than 1 for zoom out, greater than 1 for zoom in) | ||
* @param focusX focal point X position | ||
* @param focusY focal point Y position | ||
*/ | ||
void onScaleChange(float scaleFactor, float focusX, float focusY); | ||
} |
21 changes: 21 additions & 0 deletions
21
photto/src/main/java/com/gungoronline/photto/PhotoView/OnSingleFlingListener.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.gungoronline.photto.PhotoView; | ||
|
||
import android.view.MotionEvent; | ||
|
||
/** | ||
* A callback to be invoked when the ImageView is flung with a single | ||
* touch | ||
*/ | ||
public interface OnSingleFlingListener { | ||
|
||
/** | ||
* A callback to receive where the user flings on a ImageView. You will receive a callback if | ||
* the user flings anywhere on the view. | ||
* | ||
* @param e1 MotionEvent the user first touch. | ||
* @param e2 MotionEvent the user last touch. | ||
* @param velocityX distance of user's horizontal fling. | ||
* @param velocityY distance of user's vertical fling. | ||
*/ | ||
boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY); | ||
} |
16 changes: 16 additions & 0 deletions
16
photto/src/main/java/com/gungoronline/photto/PhotoView/OnViewDragListener.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.gungoronline.photto.PhotoView; | ||
|
||
/** | ||
* Interface definition for a callback to be invoked when the photo is experiencing a drag event | ||
*/ | ||
public interface OnViewDragListener { | ||
|
||
/** | ||
* Callback for when the photo is experiencing a drag event. This cannot be invoked when the | ||
* user is scaling. | ||
* | ||
* @param dx The change of the coordinates in the x-direction | ||
* @param dy The change of the coordinates in the y-direction | ||
*/ | ||
void onDrag(float dx, float dy); | ||
} |
Oops, something went wrong.