11 |
12 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # PhotoViewEx
2 |
3 | 基于 [PhotoView](https://github.com/chrisbanes/PhotoView),增加手势旋转、拖动退出预览功能。
4 |
5 | [](https://jitpack.io/#zywudev/PhotoViewEx)
6 |
7 |
8 | ## 效果
9 |
10 | 
11 |
12 | ## 依赖
13 |
14 | 在项目 `build.gradle` 中添加依赖
15 |
16 | ```groovy
17 | allprojects {
18 | repositories {
19 | maven { url "https://jitpack.io" }
20 | }
21 | }
22 | ```
23 |
24 | module 的 `build.gradle` 中添加依赖
25 |
26 | ```groovy
27 | dependencies {
28 | implementation 'com.github.zywudev:PhotoViewEx:1.0.0'
29 | }
30 | ```
31 |
32 | ## 使用方法
33 |
34 | 使用下面的方式即可实现缩放、双指旋转功能。
35 |
36 | ```xml
37 |
41 | ```
42 |
43 | ```java
44 | PhotoView photoView = (PhotoView) findViewById(R.id.iv_photo);
45 | photoView.setImageResource(R.drawable.image);
46 | ```
47 |
48 | **拖动关闭使用方法**:
49 |
50 | 1、Activity 主题设为透明
51 |
52 | ```xml
53 | true
54 | ```
55 |
56 | 2、初始化
57 |
58 | ```java
59 | DragCloseHelper mDragCloseHelper = new DragCloseHelper(this);
60 | ```
61 |
62 | 3、设置需要拖拽的 View 以及背景 ViewGroup
63 |
64 | ```java
65 | mDragCloseHelper.setDragCloseViews(mConstraintLayout,mPhotoView);
66 | ```
67 |
68 | 4、设置监听
69 |
70 | ```java
71 | mDragCloseHelper.setOnDragCloseListener(new OnDragCloseListener() {
72 | @Override
73 | public void onDragBegin() {
74 |
75 | }
76 |
77 | @Override
78 | public void onDragging(float percent) {
79 |
80 | }
81 |
82 | @Override
83 | public void onDragEnd(boolean isShareElementMode) {
84 | if (isShareElementMode) {
85 | onBackPressed();
86 | }
87 | }
88 |
89 | @Override
90 | public void onDragCancel() {
91 |
92 | }
93 |
94 | @Override
95 | public boolean intercept() {
96 | // 默认false
97 | return false;
98 | }
99 | });
100 | ```
101 |
102 | 5、处理 Touch 事件
103 |
104 | ```java
105 | @Override
106 | public boolean dispatchTouchEvent(MotionEvent ev) {
107 | if (mDragCloseHelper.handleEvent(ev)) {
108 | return true;
109 | } else {
110 | return super.dispatchTouchEvent(ev);
111 | }
112 | }
113 | ```
114 |
115 | 更多使用方法参见 [sample](https://github.com/zywudev/PhotoViewEx/tree/master/sample)。
116 |
117 | ## 参考
118 |
119 | - [PhotoView](https://github.com/chrisbanes/PhotoView)
120 |
121 | - [RotatePhotoView](https://github.com/ChenSiLiang/RotatePhotoView)
122 | - [DragCloseHelper](https://github.com/bauer-bao/DragCloseHelper)
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | google()
6 | jcenter()
7 |
8 | }
9 | dependencies {
10 | classpath 'com.android.tools.build:gradle:3.3.2'
11 | classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
12 |
13 |
14 | // NOTE: Do not place your application dependencies here; they belong
15 | // in the individual module build.gradle files
16 | }
17 | }
18 |
19 | allprojects {
20 | repositories {
21 | google()
22 | jcenter()
23 | maven { url 'https://jitpack.io' }
24 | }
25 | }
26 |
27 | task clean(type: Delete) {
28 | delete rootProject.buildDir
29 | }
30 |
31 | ext {
32 | minSdkVersion = 15
33 | targetSdkVersion = 28
34 | compileSdkVersion = 28
35 | buildToolsVersion = '28.0.3'
36 | }
37 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 |
15 |
16 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zywudev/PhotoViewEx/c26a1108c22bf0e7ddf40e56bdfffaca647ab94b/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Apr 29 14:58:11 CST 2019
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/photoviewex/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/photoviewex/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | // JitPack Maven
3 | apply plugin: 'com.github.dcendents.android-maven'
4 | // Your Group
5 | group='com.github.zywudev'
6 |
7 | android {
8 | compileSdkVersion rootProject.ext.compileSdkVersion
9 |
10 | defaultConfig {
11 | minSdkVersion rootProject.ext.minSdkVersion
12 | targetSdkVersion rootProject.ext.targetSdkVersion
13 | versionCode 1
14 | versionName "1.0"
15 |
16 | }
17 |
18 | buildTypes {
19 | release {
20 | minifyEnabled false
21 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
22 | }
23 | }
24 |
25 | }
26 |
27 | dependencies {
28 | implementation "androidx.appcompat:appcompat:1.0.0"
29 | }
30 |
--------------------------------------------------------------------------------
/photoviewex/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/photoviewex/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
--------------------------------------------------------------------------------
/photoviewex/src/main/java/com/wuzy/photoviewex/Compat.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2011, 2012 Chris Banes.
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.wuzy.photoviewex;
17 |
18 | import android.annotation.TargetApi;
19 | import android.os.Build.VERSION;
20 | import android.os.Build.VERSION_CODES;
21 | import android.view.View;
22 |
23 | class Compat {
24 |
25 | private static final int SIXTY_FPS_INTERVAL = 1000 / 60;
26 |
27 | public static void postOnAnimation(View view, Runnable runnable) {
28 | if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
29 | postOnAnimationJellyBean(view, runnable);
30 | } else {
31 | view.postDelayed(runnable, SIXTY_FPS_INTERVAL);
32 | }
33 | }
34 |
35 | @TargetApi(16)
36 | private static void postOnAnimationJellyBean(View view, Runnable runnable) {
37 | view.postOnAnimation(runnable);
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/photoviewex/src/main/java/com/wuzy/photoviewex/CustomGestureDetector.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2011, 2012 Chris Banes.
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.wuzy.photoviewex;
17 |
18 | import android.content.Context;
19 | import android.view.MotionEvent;
20 | import android.view.ScaleGestureDetector;
21 | import android.view.VelocityTracker;
22 | import android.view.ViewConfiguration;
23 |
24 | /**
25 | * Does a whole lot of gesture detecting.
26 | */
27 | class CustomGestureDetector {
28 |
29 | private static final int INVALID_POINTER_ID = -1;
30 |
31 | private int mActivePointerId = INVALID_POINTER_ID;
32 | private int mActivePointerIndex = 0;
33 | private final ScaleGestureDetector mScaleGestureDetector;
34 | private final RotateGestureDetector mRotateGestureDetector;
35 |
36 | private VelocityTracker mVelocityTracker;
37 | private boolean mIsDragging;
38 | private float mLastTouchX;
39 | private float mLastTouchY;
40 | private final float mTouchSlop;
41 | private final float mMinimumVelocity;
42 | private OnGestureListener mListener;
43 |
44 | CustomGestureDetector(Context context, OnGestureListener listener) {
45 | final ViewConfiguration configuration = ViewConfiguration
46 | .get(context);
47 | mMinimumVelocity = configuration.getScaledMinimumFlingVelocity();
48 | mTouchSlop = configuration.getScaledTouchSlop();
49 |
50 | mListener = listener;
51 | ScaleGestureDetector.OnScaleGestureListener mScaleListener = new ScaleGestureDetector.OnScaleGestureListener() {
52 |
53 | @Override
54 | public boolean onScale(ScaleGestureDetector detector) {
55 | float scaleFactor = detector.getScaleFactor();
56 |
57 | if (Float.isNaN(scaleFactor) || Float.isInfinite(scaleFactor))
58 | return false;
59 |
60 | if (scaleFactor >= 0) {
61 | mListener.onScale(scaleFactor,
62 | detector.getFocusX(), detector.getFocusY());
63 | }
64 | return true;
65 | }
66 |
67 | @Override
68 | public boolean onScaleBegin(ScaleGestureDetector detector) {
69 | return true;
70 | }
71 |
72 | @Override
73 | public void onScaleEnd(ScaleGestureDetector detector) {
74 | // NO-OP
75 | }
76 | };
77 | mScaleGestureDetector = new ScaleGestureDetector(context, mScaleListener);
78 |
79 | RotateGestureDetector.OnRotateGestureListener onRotateGestureListener = new RotateGestureDetector.OnRotateGestureListener() {
80 | @Override
81 | public void onRotate(int degrees, int pivotX, int pivotY) {
82 | mListener.onRotate(degrees, pivotX, pivotY);
83 | }
84 |
85 | @Override
86 | public void onToRightAngle(int pivotX, int pivotY) {
87 | mListener.onToRightAngle(pivotX,pivotY);
88 | }
89 | };
90 | mRotateGestureDetector = new RotateGestureDetector(context, onRotateGestureListener);
91 | }
92 |
93 | private float getActiveX(MotionEvent ev) {
94 | try {
95 | return ev.getX(mActivePointerIndex);
96 | } catch (Exception e) {
97 | return ev.getX();
98 | }
99 | }
100 |
101 | private float getActiveY(MotionEvent ev) {
102 | try {
103 | return ev.getY(mActivePointerIndex);
104 | } catch (Exception e) {
105 | return ev.getY();
106 | }
107 | }
108 |
109 | public boolean isScaling() {
110 | return mScaleGestureDetector.isInProgress();
111 | }
112 |
113 | public boolean isDragging() {
114 | return mIsDragging;
115 | }
116 |
117 | public boolean isRotating() {
118 | return mRotateGestureDetector.isRotating();
119 | }
120 |
121 | public boolean onTouchEvent(MotionEvent ev) {
122 | try {
123 | mRotateGestureDetector.onTouchEvent(ev);
124 | mScaleGestureDetector.onTouchEvent(ev);
125 | return processTouchEvent(ev);
126 | } catch (IllegalArgumentException e) {
127 | // Fix for support lib bug, happening when onDestroy is called
128 | return true;
129 | }
130 | }
131 |
132 | private boolean processTouchEvent(MotionEvent ev) {
133 | final int action = ev.getAction();
134 | switch (action & MotionEvent.ACTION_MASK) {
135 | case MotionEvent.ACTION_DOWN:
136 | mActivePointerId = ev.getPointerId(0);
137 |
138 | mVelocityTracker = VelocityTracker.obtain();
139 | if (null != mVelocityTracker) {
140 | mVelocityTracker.addMovement(ev);
141 | }
142 |
143 | mLastTouchX = getActiveX(ev);
144 | mLastTouchY = getActiveY(ev);
145 | mIsDragging = false;
146 | break;
147 | case MotionEvent.ACTION_MOVE:
148 | final float x = getActiveX(ev);
149 | final float y = getActiveY(ev);
150 | final float dx = x - mLastTouchX, dy = y - mLastTouchY;
151 |
152 | if (!mIsDragging) {
153 | // Use Pythagoras to see if drag length is larger than
154 | // touch slop
155 | mIsDragging = Math.sqrt((dx * dx) + (dy * dy)) >= mTouchSlop;
156 | }
157 |
158 | if (mIsDragging) {
159 | mListener.onDrag(dx, dy);
160 | mLastTouchX = x;
161 | mLastTouchY = y;
162 |
163 | if (null != mVelocityTracker) {
164 | mVelocityTracker.addMovement(ev);
165 | }
166 | }
167 | break;
168 | case MotionEvent.ACTION_CANCEL:
169 | mActivePointerId = INVALID_POINTER_ID;
170 | // Recycle Velocity Tracker
171 | if (null != mVelocityTracker) {
172 | mVelocityTracker.recycle();
173 | mVelocityTracker = null;
174 | }
175 | break;
176 | case MotionEvent.ACTION_UP:
177 | mActivePointerId = INVALID_POINTER_ID;
178 | if (mIsDragging) {
179 | if (null != mVelocityTracker) {
180 | mLastTouchX = getActiveX(ev);
181 | mLastTouchY = getActiveY(ev);
182 |
183 | // Compute velocity within the last 1000ms
184 | mVelocityTracker.addMovement(ev);
185 | mVelocityTracker.computeCurrentVelocity(1000);
186 |
187 | final float vX = mVelocityTracker.getXVelocity(), vY = mVelocityTracker
188 | .getYVelocity();
189 |
190 | // If the velocity is greater than minVelocity, call
191 | // listener
192 | if (Math.max(Math.abs(vX), Math.abs(vY)) >= mMinimumVelocity) {
193 | mListener.onFling(mLastTouchX, mLastTouchY, -vX,
194 | -vY);
195 | }
196 | }
197 | }
198 |
199 | // Recycle Velocity Tracker
200 | if (null != mVelocityTracker) {
201 | mVelocityTracker.recycle();
202 | mVelocityTracker = null;
203 | }
204 | break;
205 | case MotionEvent.ACTION_POINTER_UP:
206 | final int pointerIndex = Util.getPointerIndex(ev.getAction());
207 | final int pointerId = ev.getPointerId(pointerIndex);
208 | if (pointerId == mActivePointerId) {
209 | // This was our active pointer going up. Choose a new
210 | // active pointer and adjust accordingly.
211 | final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
212 | mActivePointerId = ev.getPointerId(newPointerIndex);
213 | mLastTouchX = ev.getX(newPointerIndex);
214 | mLastTouchY = ev.getY(newPointerIndex);
215 | }
216 | break;
217 | }
218 |
219 | mActivePointerIndex = ev
220 | .findPointerIndex(mActivePointerId != INVALID_POINTER_ID ? mActivePointerId
221 | : 0);
222 | return true;
223 | }
224 | }
225 |
--------------------------------------------------------------------------------
/photoviewex/src/main/java/com/wuzy/photoviewex/DragCloseHelper.java:
--------------------------------------------------------------------------------
1 | package com.wuzy.photoviewex;
2 |
3 | import android.animation.Animator;
4 | import android.animation.ValueAnimator;
5 | import android.app.Activity;
6 | import android.content.Context;
7 | import android.view.MotionEvent;
8 | import android.view.View;
9 | import android.view.ViewConfiguration;
10 | import android.view.animation.LinearInterpolator;
11 |
12 | import androidx.annotation.FloatRange;
13 |
14 | /**
15 | * @author wuzy
16 | * @date 2019/4/30
17 | * @description 拖拽图片关闭类
18 | */
19 | public class DragCloseHelper {
20 |
21 | private final static long DURATION = 100;
22 | private static final float MIN_SCALE = 0.4F;
23 | private final static int MAX_EXIT_Y = 500;
24 |
25 | private float mMinScale = MIN_SCALE;
26 | private int mMaxExitY = MAX_EXIT_Y;
27 | private ViewConfiguration mViewConfiguration;
28 | private boolean mIsSwipeToClose;
29 | private float mLastX, mLastRawY, mLastY, mLastRawX;
30 | private int mLastPointerId;
31 | private float mCurrentTranslationY, mCurrentTranslationX;
32 | private float mLastTranslationY, mLastTranslationX;
33 | private boolean mIsResetingAnimate = false;
34 | private boolean mIsShareElementMode = false;
35 |
36 | private View mParentView, mChildView;
37 |
38 | private OnDragCloseListener mOnDragCloseListener;
39 | private Context mContext;
40 |
41 | public DragCloseHelper(Context context) {
42 | this.mContext = context;
43 | mViewConfiguration = ViewConfiguration.get(context);
44 | }
45 |
46 | public void setOnDragCloseListener(OnDragCloseListener onDragCloseListener) {
47 | mOnDragCloseListener = onDragCloseListener;
48 | }
49 |
50 | /**
51 | * 设置拖拽关闭的view
52 | *
53 | * @param parentV
54 | * @param childV
55 | */
56 | public void setDragCloseViews(View parentV, View childV) {
57 | this.mParentView = parentV;
58 | this.mChildView = childV;
59 | }
60 |
61 | /**
62 | * 设置最大退出距离
63 | *
64 | * @param maxExitY
65 | */
66 | public void setMaxExitY(int maxExitY) {
67 | this.mMaxExitY = maxExitY;
68 | }
69 |
70 | /**
71 | * 设置最小缩放尺寸
72 | *
73 | * @param minScale
74 | */
75 | public void setMinScale(@FloatRange(from = 0.1f, to = 1.0f) float minScale) {
76 | this.mMinScale = minScale;
77 | }
78 |
79 | public void setShareElementMode(boolean shareElementMode) {
80 | mIsShareElementMode = shareElementMode;
81 | }
82 |
83 | public boolean handleEvent(MotionEvent event) {
84 | if (mOnDragCloseListener != null && mOnDragCloseListener.intercept()) {
85 | //拦截
86 | mIsSwipeToClose = false;
87 | return false;
88 | } else {
89 | //不拦截
90 | if (event.getAction() == MotionEvent.ACTION_DOWN) {
91 | //初始化数据
92 | mLastPointerId = event.getPointerId(0);
93 | reset(event);
94 | } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
95 | if (event.getPointerCount() > 1) {
96 | //如果有多个手指
97 | if (mIsSwipeToClose) {
98 | //已经开始滑动关闭,恢复原状,否则需要派发事件
99 | mIsSwipeToClose = false;
100 | resetCallBackAnimation();
101 | return true;
102 | }
103 | reset(event);
104 | return false;
105 | }
106 | if (mLastPointerId != event.getPointerId(0)) {
107 | //手指不一致,恢复原状
108 | if (mIsSwipeToClose) {
109 | resetCallBackAnimation();
110 | }
111 | reset(event);
112 | return true;
113 | }
114 | float currentY = event.getY();
115 | float currentX = event.getX();
116 | if (mIsSwipeToClose || Math.abs(currentY - mLastY) > 2 * mViewConfiguration.getScaledTouchSlop()) {
117 | //已经触发或者开始触发,更新view
118 | mLastY = currentY;
119 | mLastX = currentX;
120 | float currentRawY = event.getRawY();
121 | float currentRawX = event.getRawX();
122 | if (!mIsSwipeToClose) {
123 | //准备开始
124 | mIsSwipeToClose = true;
125 | if (mOnDragCloseListener != null) {
126 | mOnDragCloseListener.onDragBegin();
127 | }
128 | }
129 | //已经开始,更新view
130 | mCurrentTranslationY = currentRawY - mLastRawY + mLastTranslationY;
131 | mCurrentTranslationX = currentRawX - mLastRawX + mLastTranslationX;
132 | float percent = 1 - Math.abs(mCurrentTranslationY / (mMaxExitY + mChildView.getHeight()));
133 | if (percent > 1) {
134 | percent = 1;
135 | } else if (percent < 0) {
136 | percent = 0;
137 | }
138 | mParentView.getBackground().mutate().setAlpha((int) (percent * 255));
139 | if (mOnDragCloseListener != null) {
140 | mOnDragCloseListener.onDragging(percent);
141 | }
142 | mChildView.setTranslationY(mCurrentTranslationY);
143 | mChildView.setTranslationX(mCurrentTranslationX);
144 | if (percent < mMinScale) {
145 | percent = mMinScale;
146 | }
147 | mChildView.setScaleX(percent);
148 | mChildView.setScaleY(percent);
149 | return true;
150 | }
151 | } else if (event.getAction() == MotionEvent.ACTION_UP) {
152 | //手指抬起事件
153 | if (mIsSwipeToClose) {
154 | if (mCurrentTranslationY > mMaxExitY) {
155 | if (mIsShareElementMode) {
156 | //会执行共享元素的离开动画
157 | if (mOnDragCloseListener != null) {
158 | mOnDragCloseListener.onDragEnd(true);
159 | }
160 | } else {
161 | //会执行定制的离开动画
162 | exitWithTranslation(mCurrentTranslationY);
163 | }
164 | } else {
165 | resetCallBackAnimation();
166 | }
167 | mIsSwipeToClose = false;
168 | return true;
169 | }
170 | } else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
171 | //取消事件
172 | if (mIsSwipeToClose) {
173 | resetCallBackAnimation();
174 | mIsSwipeToClose = false;
175 | return true;
176 | }
177 | }
178 | }
179 | return false;
180 | }
181 |
182 |
183 | private void reset(MotionEvent event) {
184 | mIsSwipeToClose = false;
185 | mLastY = event.getY();
186 | mLastX = event.getX();
187 | mLastRawY = event.getRawY();
188 | mLastRawX = event.getRawX();
189 | mLastTranslationY = 0;
190 | mLastTranslationX = 0;
191 | }
192 |
193 | /**
194 | * 更新缩放的view
195 | */
196 | private void updateChildView(float transX, float transY) {
197 | mChildView.setTranslationY(transY);
198 | mChildView.setTranslationX(transX);
199 | float percent = Math.abs(transY / (mMaxExitY + mChildView.getHeight()));
200 | float scale = 1 - percent;
201 | if (scale < mMinScale) {
202 | scale = mMinScale;
203 | }
204 | mChildView.setScaleX(scale);
205 | mChildView.setScaleY(scale);
206 | }
207 |
208 | /**
209 | * 恢复到原位动画
210 | */
211 | private void resetCallBackAnimation() {
212 | if (mIsResetingAnimate || mCurrentTranslationY == 0) {
213 | return;
214 | }
215 | final float ratio = mCurrentTranslationX / mCurrentTranslationY;
216 | ValueAnimator animatorY = ValueAnimator.ofFloat(mCurrentTranslationY, 0);
217 | animatorY.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
218 | @Override
219 | public void onAnimationUpdate(ValueAnimator animation) {
220 | if (mIsResetingAnimate) {
221 | mCurrentTranslationY = (float) animation.getAnimatedValue();
222 | mCurrentTranslationX = ratio * mCurrentTranslationY;
223 | mLastTranslationY = mCurrentTranslationY;
224 | mLastTranslationX = mCurrentTranslationX;
225 | updateChildView(mLastTranslationX, mCurrentTranslationY);
226 | }
227 | }
228 | });
229 | animatorY.addListener(new Animator.AnimatorListener() {
230 | @Override
231 | public void onAnimationStart(Animator animation) {
232 | mIsResetingAnimate = true;
233 | }
234 |
235 | @Override
236 | public void onAnimationEnd(Animator animation) {
237 | if (mIsResetingAnimate) {
238 | mParentView.getBackground().mutate().setAlpha(255);
239 | mCurrentTranslationY = 0;
240 | mCurrentTranslationX = 0;
241 | mIsResetingAnimate = false;
242 | if (mOnDragCloseListener != null) {
243 | mOnDragCloseListener.onDragCancel();
244 | }
245 | }
246 | }
247 |
248 | @Override
249 | public void onAnimationCancel(Animator animation) {
250 |
251 | }
252 |
253 | @Override
254 | public void onAnimationRepeat(Animator animation) {
255 |
256 | }
257 | });
258 | animatorY.setDuration(DURATION).start();
259 | }
260 |
261 | public void exitWithTranslation(float currentY) {
262 | int targetValue = currentY > 0 ? mChildView.getHeight() : -mChildView.getHeight();
263 | ValueAnimator anim = ValueAnimator.ofFloat(mCurrentTranslationY, targetValue);
264 | anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
265 | @Override
266 | public void onAnimationUpdate(ValueAnimator animation) {
267 | updateChildView(mCurrentTranslationX, (float) animation.getAnimatedValue());
268 | }
269 | });
270 | anim.addListener(new Animator.AnimatorListener() {
271 | @Override
272 | public void onAnimationStart(Animator animation) {
273 |
274 | }
275 |
276 | @Override
277 | public void onAnimationEnd(Animator animation) {
278 | if (mOnDragCloseListener != null) {
279 | mOnDragCloseListener.onDragEnd(false);
280 | }
281 | ((Activity) mContext).finish();
282 | ((Activity) mContext).overridePendingTransition(R.anim.dchlib_anim_empty, R.anim.dchlib_anim_alpha_out_long_time);
283 | }
284 |
285 | @Override
286 | public void onAnimationCancel(Animator animation) {
287 |
288 | }
289 |
290 | @Override
291 | public void onAnimationRepeat(Animator animation) {
292 |
293 | }
294 | });
295 | anim.setDuration(DURATION);
296 | anim.setInterpolator(new LinearInterpolator());
297 | anim.start();
298 | }
299 |
300 | }
301 |
302 |
--------------------------------------------------------------------------------
/photoviewex/src/main/java/com/wuzy/photoviewex/OnDragCloseListener.java:
--------------------------------------------------------------------------------
1 | package com.wuzy.photoviewex;
2 |
3 | /**
4 | * @author wuzy
5 | * @date 2019/4/30
6 | * @description
7 | */
8 | public interface OnDragCloseListener {
9 |
10 | void onDragBegin();
11 |
12 | void onDragging(float percent);
13 |
14 | void onDragEnd(boolean isShareElementMode);
15 |
16 | void onDragCancel();
17 |
18 | boolean intercept();
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/photoviewex/src/main/java/com/wuzy/photoviewex/OnGestureListener.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2011, 2012 Chris Banes.
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.wuzy.photoviewex;
17 |
18 | interface OnGestureListener {
19 |
20 | void onDrag(float dx, float dy);
21 |
22 | void onFling(float startX, float startY, float velocityX,
23 | float velocityY);
24 |
25 | void onScale(float scaleFactor, float focusX, float focusY);
26 |
27 | void onRotate(int degrees, int pivotX, int pivotY);
28 |
29 | void onToRightAngle(int pivotX, int pivotY);
30 |
31 | }
--------------------------------------------------------------------------------
/photoviewex/src/main/java/com/wuzy/photoviewex/OnMatrixChangedListener.java:
--------------------------------------------------------------------------------
1 | package com.wuzy.photoviewex;
2 |
3 | import android.graphics.RectF;
4 |
5 | /**
6 | * Interface definition for a callback to be invoked when the internal Matrix has changed for
7 | * this View.
8 | */
9 | public interface OnMatrixChangedListener {
10 |
11 | /**
12 | * Callback for when the Matrix displaying the Drawable has changed. This could be because
13 | * the View's bounds have changed, or the user has zoomed.
14 | *
15 | * @param rect - Rectangle displaying the Drawable's new bounds.
16 | */
17 | void onMatrixChanged(RectF rect);
18 | }
19 |
--------------------------------------------------------------------------------
/photoviewex/src/main/java/com/wuzy/photoviewex/OnOutsidePhotoTapListener.java:
--------------------------------------------------------------------------------
1 | package com.wuzy.photoviewex;
2 |
3 | import android.widget.ImageView;
4 |
5 | /**
6 | * Callback when the user tapped outside of the photo
7 | */
8 | public interface OnOutsidePhotoTapListener {
9 |
10 | /**
11 | * The outside of the photo has been tapped
12 | */
13 | void onOutsidePhotoTap(ImageView imageView);
14 | }
15 |
--------------------------------------------------------------------------------
/photoviewex/src/main/java/com/wuzy/photoviewex/OnPhotoTapListener.java:
--------------------------------------------------------------------------------
1 | package com.wuzy.photoviewex;
2 |
3 | import android.widget.ImageView;
4 |
5 | /**
6 | * A callback to be invoked when the Photo is tapped with a single
7 | * tap.
8 | */
9 | public interface OnPhotoTapListener {
10 |
11 | /**
12 | * A callback to receive where the user taps on a photo. You will only receive a callback if
13 | * the user taps on the actual photo, tapping on 'whitespace' will be ignored.
14 | *
15 | * @param view ImageView the user tapped.
16 | * @param x where the user tapped from the of the Drawable, as percentage of the
17 | * Drawable width.
18 | * @param y where the user tapped from the top of the Drawable, as percentage of the
19 | * Drawable height.
20 | */
21 | void onPhotoTap(ImageView view, float x, float y);
22 | }
23 |
--------------------------------------------------------------------------------
/photoviewex/src/main/java/com/wuzy/photoviewex/OnRotateListener.java:
--------------------------------------------------------------------------------
1 | package com.wuzy.photoviewex;
2 |
3 | /**
4 | * @author wuzy
5 | * @date 2019/4/29
6 | * @description A callback to be invoked when the ImageView is rotated.
7 | **/
8 | public interface OnRotateListener {
9 |
10 | void onRotate(int degrees);
11 | }
12 |
--------------------------------------------------------------------------------
/photoviewex/src/main/java/com/wuzy/photoviewex/OnScaleChangedListener.java:
--------------------------------------------------------------------------------
1 | package com.wuzy.photoviewex;
2 |
3 |
4 | /**
5 | * Interface definition for callback to be invoked when attached ImageView scale changes
6 | */
7 | public interface OnScaleChangedListener {
8 |
9 | /**
10 | * Callback for when the scale changes
11 | *
12 | * @param scaleFactor the scale factor (less than 1 for zoom out, greater than 1 for zoom in)
13 | * @param focusX focal point X position
14 | * @param focusY focal point Y position
15 | */
16 | void onScaleChange(float scaleFactor, float focusX, float focusY);
17 | }
18 |
--------------------------------------------------------------------------------
/photoviewex/src/main/java/com/wuzy/photoviewex/OnSingleFlingListener.java:
--------------------------------------------------------------------------------
1 | package com.wuzy.photoviewex;
2 |
3 | import android.view.MotionEvent;
4 |
5 | /**
6 | * A callback to be invoked when the ImageView is flung with a single
7 | * touch
8 | */
9 | public interface OnSingleFlingListener {
10 |
11 | /**
12 | * A callback to receive where the user flings on a ImageView. You will receive a callback if
13 | * the user flings anywhere on the view.
14 | *
15 | * @param e1 MotionEvent the user first touch.
16 | * @param e2 MotionEvent the user last touch.
17 | * @param velocityX distance of user's horizontal fling.
18 | * @param velocityY distance of user's vertical fling.
19 | */
20 | boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY);
21 | }
22 |
--------------------------------------------------------------------------------
/photoviewex/src/main/java/com/wuzy/photoviewex/OnViewDragListener.java:
--------------------------------------------------------------------------------
1 | package com.wuzy.photoviewex;
2 |
3 | /**
4 | * Interface definition for a callback to be invoked when the photo is experiencing a drag event
5 | */
6 | public interface OnViewDragListener {
7 |
8 | /**
9 | * Callback for when the photo is experiencing a drag event. This cannot be invoked when the
10 | * user is scaling.
11 | *
12 | * @param dx The change of the coordinates in the x-direction
13 | * @param dy The change of the coordinates in the y-direction
14 | */
15 | void onDrag(float dx, float dy);
16 | }
17 |
--------------------------------------------------------------------------------
/photoviewex/src/main/java/com/wuzy/photoviewex/OnViewTapListener.java:
--------------------------------------------------------------------------------
1 | package com.wuzy.photoviewex;
2 |
3 | import android.view.View;
4 |
5 | public interface OnViewTapListener {
6 |
7 | /**
8 | * A callback to receive where the user taps on a ImageView. You will receive a callback if
9 | * the user taps anywhere on the view, tapping on 'whitespace' will not be ignored.
10 | *
11 | * @param view - View the user tapped.
12 | * @param x - where the user tapped from the left of the View.
13 | * @param y - where the user tapped from the top of the View.
14 | */
15 | void onViewTap(View view, float x, float y);
16 | }
17 |
--------------------------------------------------------------------------------
/photoviewex/src/main/java/com/wuzy/photoviewex/PhotoView.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2011, 2012 Chris Banes.
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.wuzy.photoviewex;
17 |
18 | import android.content.Context;
19 | import android.graphics.Matrix;
20 | import android.graphics.RectF;
21 | import android.graphics.drawable.Drawable;
22 | import android.net.Uri;
23 | import android.util.AttributeSet;
24 | import android.view.GestureDetector;
25 | import android.view.View;
26 | import android.widget.ImageView;
27 |
28 | import androidx.appcompat.widget.AppCompatImageView;
29 |
30 | /**
31 | * A zoomable ImageView. See {@link PhotoViewAttacher} for most of the details on how the zooming
32 | * is accomplished
33 | */
34 | @SuppressWarnings("unused")
35 | public class PhotoView extends AppCompatImageView {
36 |
37 | private PhotoViewAttacher attacher;
38 | private ImageView.ScaleType pendingScaleType;
39 |
40 | public PhotoView(Context context) {
41 | this(context, null);
42 | }
43 |
44 | public PhotoView(Context context, AttributeSet attr) {
45 | this(context, attr, 0);
46 | }
47 |
48 | public PhotoView(Context context, AttributeSet attr, int defStyle) {
49 | super(context, attr, defStyle);
50 | init();
51 | }
52 |
53 | private void init() {
54 | attacher = new PhotoViewAttacher(this);
55 | //We always pose as a Matrix scale type, though we can change to another scale type
56 | //via the attacher
57 | super.setScaleType(ImageView.ScaleType.MATRIX);
58 | //apply the previously applied scale type
59 | if (pendingScaleType != null) {
60 | setScaleType(pendingScaleType);
61 | pendingScaleType = null;
62 | }
63 | }
64 |
65 | /**
66 | * Get the current {@link PhotoViewAttacher} for this view. Be wary of holding on to references
67 | * to this attacher, as it has a reference to this view, which, if a reference is held in the
68 | * wrong place, can cause memory leaks.
69 | *
70 | * @return the attacher.
71 | */
72 | public PhotoViewAttacher getAttacher() {
73 | return attacher;
74 | }
75 |
76 | @Override
77 | public ImageView.ScaleType getScaleType() {
78 | return attacher.getScaleType();
79 | }
80 |
81 | @Override
82 | public Matrix getImageMatrix() {
83 | return attacher.getImageMatrix();
84 | }
85 |
86 | @Override
87 | public void setOnLongClickListener(View.OnLongClickListener l) {
88 | attacher.setOnLongClickListener(l);
89 | }
90 |
91 | @Override
92 | public void setOnClickListener(View.OnClickListener l) {
93 | attacher.setOnClickListener(l);
94 | }
95 |
96 | @Override
97 | public void setScaleType(ImageView.ScaleType scaleType) {
98 | if (attacher == null) {
99 | pendingScaleType = scaleType;
100 | } else {
101 | attacher.setScaleType(scaleType);
102 | }
103 | }
104 |
105 | @Override
106 | public void setImageDrawable(Drawable drawable) {
107 | super.setImageDrawable(drawable);
108 | // setImageBitmap calls through to this method
109 | if (attacher != null) {
110 | attacher.update();
111 | }
112 | }
113 |
114 | @Override
115 | public void setImageResource(int resId) {
116 | super.setImageResource(resId);
117 | if (attacher != null) {
118 | attacher.update();
119 | }
120 | }
121 |
122 | @Override
123 | public void setImageURI(Uri uri) {
124 | super.setImageURI(uri);
125 | if (attacher != null) {
126 | attacher.update();
127 | }
128 | }
129 |
130 | @Override
131 | protected boolean setFrame(int l, int t, int r, int b) {
132 | boolean changed = super.setFrame(l, t, r, b);
133 | if (changed) {
134 | attacher.update();
135 | }
136 | return changed;
137 | }
138 |
139 | public void setRotationTo(float rotationDegree) {
140 | attacher.setRotationTo(rotationDegree);
141 | }
142 |
143 | public void setRotationBy(float rotationDegree) {
144 | attacher.setRotationBy(rotationDegree);
145 | }
146 |
147 | public boolean isZoomable() {
148 | return attacher.isZoomable();
149 | }
150 |
151 | public void setZoomable(boolean zoomable) {
152 | attacher.setZoomable(zoomable);
153 | }
154 |
155 | public void setRotatable(boolean rotatable) {
156 | attacher.setRotatable(rotatable);
157 | }
158 |
159 | public boolean isRotatable() {
160 | return attacher.isRotatable();
161 | }
162 |
163 | public RectF getDisplayRect() {
164 | return attacher.getDisplayRect();
165 | }
166 |
167 | public void getDisplayMatrix(Matrix matrix) {
168 | attacher.getDisplayMatrix(matrix);
169 | }
170 |
171 | @SuppressWarnings("UnusedReturnValue") public boolean setDisplayMatrix(Matrix finalRectangle) {
172 | return attacher.setDisplayMatrix(finalRectangle);
173 | }
174 |
175 | public void getSuppMatrix(Matrix matrix) {
176 | attacher.getSuppMatrix(matrix);
177 | }
178 |
179 | public boolean setSuppMatrix(Matrix matrix) {
180 | return attacher.setDisplayMatrix(matrix);
181 | }
182 |
183 | public float getMinimumScale() {
184 | return attacher.getMinimumScale();
185 | }
186 |
187 | public float getMediumScale() {
188 | return attacher.getMediumScale();
189 | }
190 |
191 | public float getMaximumScale() {
192 | return attacher.getMaximumScale();
193 | }
194 |
195 | public float getScale() {
196 | return attacher.getScale();
197 | }
198 |
199 | public void setAllowParentInterceptOnEdge(boolean allow) {
200 | attacher.setAllowParentInterceptOnEdge(allow);
201 | }
202 |
203 | public void setMinimumScale(float minimumScale) {
204 | attacher.setMinimumScale(minimumScale);
205 | }
206 |
207 | public void setMediumScale(float mediumScale) {
208 | attacher.setMediumScale(mediumScale);
209 | }
210 |
211 | public void setMaximumScale(float maximumScale) {
212 | attacher.setMaximumScale(maximumScale);
213 | }
214 |
215 | public void setScaleLevels(float minimumScale, float mediumScale, float maximumScale) {
216 | attacher.setScaleLevels(minimumScale, mediumScale, maximumScale);
217 | }
218 |
219 | public void setOnMatrixChangeListener(OnMatrixChangedListener listener) {
220 | attacher.setOnMatrixChangeListener(listener);
221 | }
222 |
223 | public void setOnPhotoTapListener(OnPhotoTapListener listener) {
224 | attacher.setOnPhotoTapListener(listener);
225 | }
226 |
227 | public void setOnOutsidePhotoTapListener(OnOutsidePhotoTapListener listener) {
228 | attacher.setOnOutsidePhotoTapListener(listener);
229 | }
230 |
231 | public void setOnViewTapListener(OnViewTapListener listener) {
232 | attacher.setOnViewTapListener(listener);
233 | }
234 |
235 | public void setOnViewDragListener(OnViewDragListener listener) {
236 | attacher.setOnViewDragListener(listener);
237 | }
238 |
239 | public void setScale(float scale) {
240 | attacher.setScale(scale);
241 | }
242 |
243 | public void setScale(float scale, boolean animate) {
244 | attacher.setScale(scale, animate);
245 | }
246 |
247 | public void setScale(float scale, float focalX, float focalY, boolean animate) {
248 | attacher.setScale(scale, focalX, focalY, animate);
249 | }
250 |
251 | public void setZoomTransitionDuration(int milliseconds) {
252 | attacher.setZoomTransitionDuration(milliseconds);
253 | }
254 |
255 | public void setOnDoubleTapListener(GestureDetector.OnDoubleTapListener onDoubleTapListener) {
256 | attacher.setOnDoubleTapListener(onDoubleTapListener);
257 | }
258 |
259 | public void setOnScaleChangeListener(OnScaleChangedListener onScaleChangedListener) {
260 | attacher.setOnScaleChangeListener(onScaleChangedListener);
261 | }
262 |
263 | public void setOnSingleFlingListener(OnSingleFlingListener onSingleFlingListener) {
264 | attacher.setOnSingleFlingListener(onSingleFlingListener);
265 | }
266 |
267 | public void setOnRotateListener(OnRotateListener onRotateListener) {
268 | attacher.setOnRotateListener(onRotateListener);
269 | }
270 |
271 |
272 | }
273 |
--------------------------------------------------------------------------------
/photoviewex/src/main/java/com/wuzy/photoviewex/PhotoViewAttacher.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2011, 2012 Chris Banes.
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |