Skip to content
This repository has been archived by the owner on Nov 4, 2022. It is now read-only.

Commit

Permalink
Merge pull request #52 from zeoflow/photo-view
Browse files Browse the repository at this point in the history
Created PhotoView
  • Loading branch information
teogor authored Oct 7, 2020
2 parents 8c03a19 + fc364f3 commit a7de9a5
Show file tree
Hide file tree
Showing 19 changed files with 1,498 additions and 31 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ ext {
buildToolsVersion = '30.0.1'
minSdkVersion = 14
targetSdkVersion = 30
versionCode = 7
versionName = "2.0.0"
versionCode = 8
versionName = "2.1.0"

androidx = [
annotation : 'androidx.annotation:annotation:1.0.1',
Expand Down
1 change: 1 addition & 0 deletions material-elements/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def srcDirs = [
'com/zeoflow/material/elements/menu',
'com/zeoflow/material/elements/navigation',
'com/zeoflow/material/elements/pagerindicator',
'com/zeoflow/material/elements/photoview',
'com/zeoflow/material/elements/radiobutton',
'com/zeoflow/material/elements/resources',
'com/zeoflow/material/elements/ripple',
Expand Down
4 changes: 2 additions & 2 deletions material-elements/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ POM_NAME=MaterialElements
POM_ARTIFACT_ID=material-elements
POM_PACKAGING=aar

VERSION_NAME=1.3.0
VERSION_CODE=6
VERSION_NAME=2.1.0
VERSION_CODE=8
GROUP=com.zeoflow

POM_DESCRIPTION=Material Elements help developers execute Material Elements. Developed by a core team of engineers and UX designers, these elements enable a reliable development workflow to build beautiful and functional Android apps.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.zeoflow.material.elements.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);
}
}
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.zeoflow.material.elements.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;
}
}
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.zeoflow.material.elements.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);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.zeoflow.material.elements.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);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.zeoflow.material.elements.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);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.zeoflow.material.elements.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);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.zeoflow.material.elements.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);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.zeoflow.material.elements.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);
}
Loading

0 comments on commit a7de9a5

Please sign in to comment.