├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── colors.xml
│ │ │ │ ├── dimens.xml
│ │ │ │ └── styles.xml
│ │ │ ├── drawable
│ │ │ │ └── alarm.png
│ │ │ ├── mipmap-hdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── values-w820dp
│ │ │ │ └── dimens.xml
│ │ │ └── layout
│ │ │ │ └── activity_main.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── yanzhenghui
│ │ │ └── shakeanimationdemo
│ │ │ └── MainActivity.java
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── yanzhenghui
│ │ │ └── shakeanimationdemo
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── yanzhenghui
│ │ └── shakeanimationdemo
│ │ └── ApplicationTest.java
├── proguard-rules.pro
└── build.gradle
├── .idea
├── .name
├── copyright
│ └── profiles_settings.xml
├── encodings.xml
├── modules.xml
├── runConfigurations.xml
├── compiler.xml
├── gradle.xml
└── misc.xml
├── settings.gradle
├── ShakeAnimationDemo.gif
├── pop_animation_shake.gif
├── view_animation_shake.gif
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── gradle.properties
├── gradlew.bat
├── gradlew
└── README.md
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/.idea/.name:
--------------------------------------------------------------------------------
1 | ShakeAnimationDemo
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/ShakeAnimationDemo.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhenghuiy/ShakeAnimationDemo/HEAD/ShakeAnimationDemo.gif
--------------------------------------------------------------------------------
/pop_animation_shake.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhenghuiy/ShakeAnimationDemo/HEAD/pop_animation_shake.gif
--------------------------------------------------------------------------------
/view_animation_shake.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhenghuiy/ShakeAnimationDemo/HEAD/view_animation_shake.gif
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | ShakeAnimationDemo
3 |
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhenghuiy/ShakeAnimationDemo/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/drawable/alarm.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhenghuiy/ShakeAnimationDemo/HEAD/app/src/main/res/drawable/alarm.png
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhenghuiy/ShakeAnimationDemo/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhenghuiy/ShakeAnimationDemo/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhenghuiy/ShakeAnimationDemo/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhenghuiy/ShakeAnimationDemo/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhenghuiy/ShakeAnimationDemo/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Dec 28 10:00:20 PST 2015
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-2.10-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 | 80dp
6 |
7 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/com/yanzhenghui/shakeanimationdemo/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.yanzhenghui.shakeanimationdemo;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * To work on unit tests, switch the Test Artifact in the Build Variants view.
9 | */
10 | public class ExampleUnitTest {
11 | @Test
12 | public void addition_isCorrect() throws Exception {
13 | assertEquals(4, 2 + 2);
14 | }
15 | }
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/yanzhenghui/shakeanimationdemo/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package com.yanzhenghui.shakeanimationdemo;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/yanzhenghui/Library/Android/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 23
5 | buildToolsVersion "23.0.3"
6 |
7 | defaultConfig {
8 | applicationId "com.yanzhenghui.shakeanimationdemo"
9 | minSdkVersion 14
10 | targetSdkVersion 23
11 | versionCode 1
12 | versionName "1.0"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile fileTree(dir: 'libs', include: ['*.jar'])
24 | testCompile 'junit:junit:4.12'
25 | compile 'com.android.support:appcompat-v7:23.4.0'
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14 |
15 | # When configured, Gradle will run in incubating parallel mode.
16 | # This option should only be used with decoupled projects. More details, visit
17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18 | # org.gradle.parallel=true
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
11 |
12 |
16 |
22 |
27 |
28 |
29 |
35 |
36 |
44 |
45 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/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 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
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 Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/app/src/main/java/com/yanzhenghui/shakeanimationdemo/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.yanzhenghui.shakeanimationdemo;
2 |
3 | import android.animation.Keyframe;
4 | import android.animation.ObjectAnimator;
5 | import android.animation.PropertyValuesHolder;
6 | import android.support.v7.app.AppCompatActivity;
7 | import android.os.Bundle;
8 | import android.support.v7.widget.AppCompatButton;
9 | import android.view.View;
10 | import android.view.animation.Animation;
11 | import android.view.animation.AnimationSet;
12 | import android.view.animation.RotateAnimation;
13 | import android.view.animation.ScaleAnimation;
14 | import android.widget.ImageView;
15 | import android.widget.RadioGroup;
16 |
17 | public class MainActivity extends AppCompatActivity {
18 | private ImageView mIvShake;
19 | private AppCompatButton mBtnStart;
20 | private RadioGroup mRgChooseAnim;
21 |
22 | private boolean mUsingViewAnim = true;
23 |
24 | @Override
25 | protected void onCreate(Bundle savedInstanceState) {
26 | super.onCreate(savedInstanceState);
27 | setContentView(R.layout.activity_main);
28 | initViews();
29 | }
30 |
31 | private void initViews() {
32 | mIvShake = (ImageView) findViewById(R.id.shake);
33 | mBtnStart = (AppCompatButton) findViewById(R.id.start);
34 | mRgChooseAnim = (RadioGroup) findViewById(R.id.choose_anim);
35 |
36 | mBtnStart.setOnClickListener(new View.OnClickListener() {
37 | @Override
38 | public void onClick(View v) {
39 | startShakeAnim();
40 | }
41 | });
42 | mRgChooseAnim.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
43 | @Override
44 | public void onCheckedChanged(RadioGroup group, int checkedId) {
45 | if (checkedId == R.id.view_anim) {
46 | mUsingViewAnim = true;
47 | } else {
48 | mUsingViewAnim = false;
49 | }
50 | }
51 | });
52 | }
53 |
54 | private void startShakeAnim() {
55 | if (mUsingViewAnim) {
56 | startShakeByViewAnim(mIvShake, 0.9f, 1.1f, 10f, 1000);
57 | } else {
58 | startShakeByPropertyAnim(mIvShake, 0.9f, 1.1f, 10f, 1000);
59 | }
60 | }
61 |
62 | private void startShakeByViewAnim(View view, float scaleSmall, float scaleLarge, float shakeDegrees, long duration) {
63 | if (view == null) {
64 | return;
65 | }
66 | //TODO 验证参数的有效性
67 |
68 | //由小变大
69 | Animation scaleAnim = new ScaleAnimation(scaleSmall, scaleLarge, scaleSmall, scaleLarge);
70 | //从左向右
71 | Animation rotateAnim = new RotateAnimation(-shakeDegrees, shakeDegrees, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
72 |
73 | scaleAnim.setDuration(duration);
74 | rotateAnim.setDuration(duration / 10);
75 | rotateAnim.setRepeatMode(Animation.REVERSE);
76 | rotateAnim.setRepeatCount(10);
77 |
78 | AnimationSet smallAnimationSet = new AnimationSet(false);
79 | smallAnimationSet.addAnimation(scaleAnim);
80 | smallAnimationSet.addAnimation(rotateAnim);
81 |
82 | view.startAnimation(smallAnimationSet);
83 | }
84 |
85 | private void startShakeByPropertyAnim(View view, float scaleSmall, float scaleLarge, float shakeDegrees, long duration) {
86 | if (view == null) {
87 | return;
88 | }
89 | //TODO 验证参数的有效性
90 |
91 | //先变小后变大
92 | PropertyValuesHolder scaleXValuesHolder = PropertyValuesHolder.ofKeyframe(View.SCALE_X,
93 | Keyframe.ofFloat(0f, 1.0f),
94 | Keyframe.ofFloat(0.25f, scaleSmall),
95 | Keyframe.ofFloat(0.5f, scaleLarge),
96 | Keyframe.ofFloat(0.75f, scaleLarge),
97 | Keyframe.ofFloat(1.0f, 1.0f)
98 | );
99 | PropertyValuesHolder scaleYValuesHolder = PropertyValuesHolder.ofKeyframe(View.SCALE_Y,
100 | Keyframe.ofFloat(0f, 1.0f),
101 | Keyframe.ofFloat(0.25f, scaleSmall),
102 | Keyframe.ofFloat(0.5f, scaleLarge),
103 | Keyframe.ofFloat(0.75f, scaleLarge),
104 | Keyframe.ofFloat(1.0f, 1.0f)
105 | );
106 |
107 | //先往左再往右
108 | PropertyValuesHolder rotateValuesHolder = PropertyValuesHolder.ofKeyframe(View.ROTATION,
109 | Keyframe.ofFloat(0f, 0f),
110 | Keyframe.ofFloat(0.1f, -shakeDegrees),
111 | Keyframe.ofFloat(0.2f, shakeDegrees),
112 | Keyframe.ofFloat(0.3f, -shakeDegrees),
113 | Keyframe.ofFloat(0.4f, shakeDegrees),
114 | Keyframe.ofFloat(0.5f, -shakeDegrees),
115 | Keyframe.ofFloat(0.6f, shakeDegrees),
116 | Keyframe.ofFloat(0.7f, -shakeDegrees),
117 | Keyframe.ofFloat(0.8f, shakeDegrees),
118 | Keyframe.ofFloat(0.9f, -shakeDegrees),
119 | Keyframe.ofFloat(1.0f, 0f)
120 | );
121 |
122 | ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(view, scaleXValuesHolder, scaleYValuesHolder, rotateValuesHolder);
123 | objectAnimator.setDuration(duration);
124 | objectAnimator.start();
125 | }
126 | }
127 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # ShakeAnimationDemo
2 | Android shake animation demo
3 |
4 | 大家好,我是[光源](http://www.jianshu.com/p/699ca079598f)。在日常使用app或者玩游戏的过程中,我们经常可以看到某个view通过抖动来吸引用户注意,今天就来说说怎么实现这个动画。
5 |
6 | 具体需求是,**实现一个抖动动画要求同时对大小、旋转角度进行更改且可定制**。
7 |
8 | 要实现动画,我们首先应该想到的是 Android 中动画相关的内容。
9 |
10 | Android 中一共有三类动画:
11 |
12 | - View Animation
13 | 又称补间动画,在 android.view.animation.Animation 类之下衍生了五个子类。
14 |
15 |
16 | | 类名| 作用|
17 | | ---| ---- |
18 | | [AlphaAnimation](https://developer.android.com/reference/android/view/animation/AlphaAnimation.html) | 渐变透明度 |
19 | | [RotateAnimation](https://developer.android.com/reference/android/view/animation/RotateAnimation.html) | 旋转 |
20 | | [ScaleAnimation](https://developer.android.com/reference/android/view/animation/ScaleAnimation.html) | 尺寸缩放 |
21 | | [TranslateAnimation](https://developer.android.com/reference/android/view/animation/TranslateAnimation.html) | 位置平移 |
22 | | [AnimationSet](https://developer.android.com/reference/android/view/animation/AnimationSet.html) | 动画集合 |
23 |
24 | 通过前四个类,基本可以解决大部分动画需求,再使用 AnimationSet 使动画具有组合的能力。
25 |
26 | - Drawable Animation
27 | 又称逐帧动画,通过设置多个帧在一定时间内不断进行帧的变换形成动画的效果,类似 gif 图。通过 xml 中的 animation-list 标签定义动画,再在 java 代码中用 AnimationDrawable 类来进行控制。
28 |
29 | - Property Animation
30 | View Animation 虽然可以解决大部分动画,但还是有些无法实现,而 Drawable Animation 则太过费时费力,所以在 Android 3.0(API 11)引入了属性动画,属性动画实现原理就是修改控件的属性值实现的动画。具体实现又分为 ValueAnimator 和 ObjectAnimator,这里不展开。
31 |
32 | 回到需求本身,从需求上看,三种方式都可以实现(其实对最接近动画本质的逐帧动画而言,还真没有不能实现的动画 ),这里不妨三种方式都尝试一下(**为方便代码展示,以下尽量使用java代码实现动画**)。
33 |
34 | # 方案一:使用 View Animation
35 | 看代码:
36 |
37 | ````java
38 | private void startShakeByViewAnim(View view, float scaleSmall, float scaleLarge, float shakeDegrees, long duration) {
39 | if (view == null) {
40 | return;
41 | }
42 | //TODO 验证参数的有效性
43 |
44 | //由小变大
45 | Animation scaleAnim = new ScaleAnimation(scaleSmall, scaleLarge, scaleSmall, scaleLarge);
46 | //从左向右
47 | Animation rotateAnim = new RotateAnimation(-shakeDegrees, shakeDegrees, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
48 |
49 | scaleAnim.setDuration(duration);
50 | rotateAnim.setDuration(duration / 10);
51 | rotateAnim.setRepeatMode(Animation.REVERSE);
52 | rotateAnim.setRepeatCount(10);
53 |
54 | AnimationSet smallAnimationSet = new AnimationSet(false);
55 | smallAnimationSet.addAnimation(scaleAnim);
56 | smallAnimationSet.addAnimation(rotateAnim);
57 |
58 | view.startAnimation(smallAnimationSet);
59 | }
60 | ````
61 |
62 | 使用 ScaleAnimation + RotateAnimation 形成一边先小后大一边摇晃的动画效果。为了效果,需要使 view的缩放效果速率远小于摇晃效果,这里采用10倍差距。效果如下:
63 |
64 | 
65 |
66 | 这个方案的优点是:
67 | 1. 使用了常见的动画方案,使用成本与代码阅读成本较低;
68 |
69 | 缺点是:
70 | 1. 为了实现左右连续抖动的效果,把两个动画的初始值都没有设置为 view 本身的属性大小,导致初始和结束时会有突变;
71 | 2. 缩放效果和摇晃效果的速率区别是通过 duration 的倍数 + 动画的 reverse repeat 来实现的,意味着 duration 必须作为初始化参数传入;
72 | 3. 在 view 变大时,摇晃的焦点还是以前的位置,导致摇晃的效果不大好。
73 | 4. 对动画的定制粒度太大,比如这里的需求是先小后大,实际上是有两个动画,但是 View Animation 中并没有串行执行动画的方法提供,这里是用取巧的方式实现,颇花费了一番心思。
74 |
75 | # 方案二:使用属性动画
76 |
77 | 看代码:
78 |
79 | ```java
80 | private void startShakeByPropertyAnim(View view, float scaleSmall, float scaleLarge, float shakeDegrees, long duration) {
81 | if (view == null) {
82 | return;
83 | }
84 | //TODO 验证参数的有效性
85 |
86 | //先变小后变大
87 | PropertyValuesHolder scaleXValuesHolder = PropertyValuesHolder.ofKeyframe(View.SCALE_X,
88 | Keyframe.ofFloat(0f, 1.0f),
89 | Keyframe.ofFloat(0.25f, scaleSmall),
90 | Keyframe.ofFloat(0.5f, scaleLarge),
91 | Keyframe.ofFloat(0.75f, scaleLarge),
92 | Keyframe.ofFloat(1.0f, 1.0f)
93 | );
94 | PropertyValuesHolder scaleYValuesHolder = PropertyValuesHolder.ofKeyframe(View.SCALE_Y,
95 | Keyframe.ofFloat(0f, 1.0f),
96 | Keyframe.ofFloat(0.25f, scaleSmall),
97 | Keyframe.ofFloat(0.5f, scaleLarge),
98 | Keyframe.ofFloat(0.75f, scaleLarge),
99 | Keyframe.ofFloat(1.0f, 1.0f)
100 | );
101 |
102 | //先往左再往右
103 | PropertyValuesHolder rotateValuesHolder = PropertyValuesHolder.ofKeyframe(View.ROTATION,
104 | Keyframe.ofFloat(0f, 0f),
105 | Keyframe.ofFloat(0.1f, -shakeDegrees),
106 | Keyframe.ofFloat(0.2f, shakeDegrees),
107 | Keyframe.ofFloat(0.3f, -shakeDegrees),
108 | Keyframe.ofFloat(0.4f, shakeDegrees),
109 | Keyframe.ofFloat(0.5f, -shakeDegrees),
110 | Keyframe.ofFloat(0.6f, shakeDegrees),
111 | Keyframe.ofFloat(0.7f, -shakeDegrees),
112 | Keyframe.ofFloat(0.8f, shakeDegrees),
113 | Keyframe.ofFloat(0.9f, -shakeDegrees),
114 | Keyframe.ofFloat(1.0f, 0f)
115 | );
116 |
117 | ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(view, scaleXValuesHolder, scaleYValuesHolder, rotateValuesHolder);
118 | objectAnimator.setDuration(duration);
119 | objectAnimator.start();
120 | }
121 | ```
122 |
123 | 为了能使多个动画同时进行,使用属性动画中的 PropertyValuesHolder 来实现。分别对 scaleX、scaleY、 rotation三个属性做属性动画处理。实现效果如下:
124 |
125 | 
126 |
127 | 从代码上看,这里实现缩放速率和摇晃速率不一致是通过 keyFrame 的设置实现的,与 duration 参数解耦。
128 |
129 | 这个方案的优点是:
130 |
131 | 1. 对动画的定制粒度较小,理论上可以对无限小的动画帧进行定制。
132 | 2. 基于第一点的缘故,实现效果较好,基本可以实现任何动画效果。
133 |
134 |
135 | # 总结
136 |
137 | 本文提出两种方案,上文也提到了,实际上任何动画都可以用帧动画来实现,只不过费时费力,所以方案三我就不多说了。**另外还有一个方案四,就是自定义Animation,在一个动画周期内对控件属性进行修改,这种方案虽然属于Animation的范畴,但是实质与属性动画一致,这里就不做扩展,感兴趣的读者可以留言讨论。**
138 |
139 | 从上面的比较可以看出,View Animation 比较适合于一些简单的动画效果,可以达到快速开发的目的。而一些复杂的、复合的动画效果,用属性动画则事半功倍,效果也会比较好。当然属性动画的局限性在于,如果对应的控件没有动画所需的属性,则“巧妇”也难以解决。
140 |
141 | 为了便于大家比较,我写了一个demo放在github上,在保证各种变量尽量一致的情况下可以看到两种方案的实现效果——当然如果你有更好的方案欢迎留言告知。
142 |
143 | 
144 |
145 | 代码地址:https://github.com/zhenghuiy/ShakeAnimationDemo
146 |
147 | # 参考资料
148 |
149 | http://blog.csdn.net/harvic880925/article/details/50752838
150 |
--------------------------------------------------------------------------------