Skip to content

Commit ff90580

Browse files
committed
2 parents a536482 + 0a74d40 commit ff90580

File tree

7 files changed

+138
-52
lines changed

7 files changed

+138
-52
lines changed

README.md

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Android View Animations [![Build Status](https://travis-ci.org/daimajia/AndroidViewAnimations.svg)](https://travis-ci.org/daimajia/AndroidViewAnimations)
22

3-
[![Gitter](https://badges.gitter.im/Join Chat.svg)](https://gitter.im/daimajia/AndroidViewAnimations?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
3+
[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/daimajia/AndroidViewAnimations?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
4+
5+
6+
[![Insight.io](https://insight.io/repoBadge/github.com/daimajia/AndroidViewAnimations)](https://insight.io/github.com/daimajia/AndroidViewAnimations)
47

58
One day, I saw [an iOS library](https://github.com/ArtFeel/AFViewShaker), which is a view shaker, it's very beautiful. I think Android also need one, and should be better.
69

@@ -24,7 +27,7 @@ For making animations more real, I created another project named [Android Easing
2427
dependencies {
2528
compile 'com.android.support:support-compat:25.1.1'
2629
compile 'com.daimajia.easing:library:2.0@aar'
27-
compile 'com.daimajia.androidanimations:library:2.0@aar'
30+
compile 'com.daimajia.androidanimations:library:2.3@aar'
2831
}
2932
```
3033
#### Maven
@@ -38,7 +41,7 @@ dependencies {
3841
<dependency>
3942
<groupId>com.daimajia.androidanimation</groupId>
4043
<artifactId>library</artifactId>
41-
<version>2.0</version>
44+
<version>2.3</version>
4245
</dependency>
4346
<dependency>
4447
<groupId>com.daimajia.easing</groupId>
@@ -47,20 +50,14 @@ dependencies {
4750
</dependency>
4851
```
4952

50-
#### Eclipse
51-
52-
Download the following jars, and copy them into your `libs` directory.
53-
54-
- [`AndroidEasingFunctions-1.0.0`](https://github.com/daimajia/AndroidViewAnimations/releases/download/v1.0.6/AndroidEasingFunctions-1.0.0.jar)
55-
- [`AndroidViewAnimations-1.1.3`](https://github.com/daimajia/AndroidViewAnimations/releases/download/v1.1.3/AndroidViewAnimations-1.1.3.jar)
56-
5753
## Step 2
5854

5955
Just like play Yo-yo.
6056

6157
```java
6258
YoYo.with(Techniques.Tada)
6359
.duration(700)
60+
.repeat(5)
6461
.playOn(findViewById(R.id.edit_area));
6562
```
6663

@@ -99,12 +96,12 @@ YoYo.with(Techniques.Tada)
9996

10097
Welcome contribute your amazing animation effect. :-D
10198

102-
#Thanks
99+
# Thanks
103100

104101
- [AFViewShaker](https://github.com/ArtFeel/AFViewShaker)
105102
- [Animate.css](https://github.com/daneden/animate.css)
106103

107-
#About me
104+
# About me
108105

109106
A student in mainland China.
110107

demo/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ android {
77
applicationId "com.daimajia.androidanimations"
88
minSdkVersion 14
99
targetSdkVersion 25
10-
versionCode 2
11-
versionName "2.0"
10+
versionCode 3
11+
versionName "3.0"
1212
}
1313
buildTypes {
1414
release {

demo/src/main/java/com/daimajia/androidanimations/MyActivity.java

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ protected void onCreate(Bundle savedInstanceState) {
2727
super.onCreate(savedInstanceState);
2828
setContentView(R.layout.activity_my);
2929

30-
mListView = (ListView)findViewById(R.id.list_items);
30+
mListView = (ListView) findViewById(R.id.list_items);
3131
mTarget = findViewById(R.id.hello_world);
3232

3333
mAdapter = new EffectAdapter(this);
@@ -36,47 +36,51 @@ protected void onCreate(Bundle savedInstanceState) {
3636
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
3737
@Override
3838
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
39-
Techniques technique = (Techniques)view.getTag();
40-
rope = YoYo.with(technique)
41-
.duration(1200)
42-
.interpolate(new AccelerateDecelerateInterpolator())
43-
.withListener(new Animator.AnimatorListener() {
44-
@Override
45-
public void onAnimationStart(Animator animation) {
46-
47-
}
48-
49-
@Override
50-
public void onAnimationEnd(Animator animation) {
51-
52-
}
53-
54-
@Override
55-
public void onAnimationCancel(Animator animation) {
56-
Toast.makeText(MyActivity.this, "canceled", Toast.LENGTH_SHORT).show();
57-
}
58-
59-
@Override
60-
public void onAnimationRepeat(Animator animation) {
61-
62-
}
63-
})
64-
.playOn(mTarget);
39+
if (rope != null) {
40+
rope.stop(true);
41+
}
42+
Techniques technique = (Techniques) view.getTag();
43+
rope = YoYo.with(technique)
44+
.duration(1200)
45+
.repeat(YoYo.INFINITE)
46+
.pivot(YoYo.CENTER_PIVOT, YoYo.CENTER_PIVOT)
47+
.interpolate(new AccelerateDecelerateInterpolator())
48+
.withListener(new Animator.AnimatorListener() {
49+
@Override
50+
public void onAnimationStart(Animator animation) {
51+
52+
}
53+
54+
@Override
55+
public void onAnimationEnd(Animator animation) {
56+
}
57+
58+
@Override
59+
public void onAnimationCancel(Animator animation) {
60+
Toast.makeText(MyActivity.this, "canceled previous animation", Toast.LENGTH_SHORT).show();
61+
}
62+
63+
@Override
64+
public void onAnimationRepeat(Animator animation) {
65+
66+
}
67+
})
68+
.playOn(mTarget);
6569
}
6670
});
6771
findViewById(R.id.hello_world).setOnClickListener(new View.OnClickListener() {
6872
@Override
6973
public void onClick(View v) {
7074
if (rope != null) {
71-
rope.stop(true);
75+
rope.stop(true);
7276
}
7377
}
7478
});
7579
}
7680

7781
@Override
7882
public void onWindowFocusChanged(boolean hasFocus) {
79-
if(hasFocus){
83+
if (hasFocus) {
8084
rope = YoYo.with(Techniques.FadeIn).duration(1000).playOn(mTarget);// after start,just click mTarget view, rope is not init
8185
}
8286
}
@@ -96,7 +100,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
96100
// as you specify a parent activity in AndroidManifest.xml.
97101
int id = item.getItemId();
98102
if (id == R.id.action_settings) {
99-
startActivity(new Intent(this,ExampleActivity.class));
103+
startActivity(new Intent(this, ExampleActivity.class));
100104
return true;
101105
}
102106
return super.onOptionsItemSelected(item);

gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
# org.gradle.parallel=true
1919

2020

21-
VERSION_NAME=2.0
22-
VERSION_CODE=20
21+
VERSION_NAME=2.3
22+
VERSION_CODE=31
2323
GROUP=com.daimajia.androidanimations
2424

2525
POM_DESCRIPTION=Collect android animations

library/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ android {
66
defaultConfig {
77
minSdkVersion 14
88
targetSdkVersion 25
9-
versionCode 20
10-
versionName "2.0"
9+
versionCode 31
10+
versionName "2.3"
1111
}
1212
buildTypes {
1313
release {

library/src/main/java/com/daimajia/androidanimations/library/BaseViewAnimator.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import android.animation.Animator;
2828
import android.animation.AnimatorSet;
29+
import android.animation.ValueAnimator;
2930
import android.support.v4.view.ViewCompat;
3031
import android.view.View;
3132
import android.view.animation.Interpolator;
@@ -36,7 +37,10 @@ public abstract class BaseViewAnimator {
3637
public static final long DURATION = 1000;
3738

3839
private AnimatorSet mAnimatorSet;
40+
3941
private long mDuration = DURATION;
42+
private int mRepeatTimes = 0;
43+
private int mRepeatMode = ValueAnimator.RESTART;
4044

4145
{
4246
mAnimatorSet = new AnimatorSet();
@@ -55,6 +59,11 @@ public void animate() {
5559
start();
5660
}
5761

62+
public void restart() {
63+
mAnimatorSet = mAnimatorSet.clone();
64+
start();
65+
}
66+
5867
/**
5968
* reset the view to default status
6069
*
@@ -69,14 +78,18 @@ public void reset(View target) {
6978
ViewCompat.setRotation(target, 0);
7079
ViewCompat.setRotationY(target, 0);
7180
ViewCompat.setRotationX(target, 0);
72-
ViewCompat.setPivotX(target, target.getMeasuredWidth() / 2.0f);
73-
ViewCompat.setPivotY(target, target.getMeasuredHeight() / 2.0f);
7481
}
7582

7683
/**
7784
* start to animate
7885
*/
7986
public void start() {
87+
for (Animator animator : mAnimatorSet.getChildAnimations()) {
88+
if (animator instanceof ValueAnimator) {
89+
((ValueAnimator) animator).setRepeatCount(mRepeatTimes);
90+
((ValueAnimator) animator).setRepeatMode(mRepeatMode);
91+
}
92+
}
8093
mAnimatorSet.setDuration(mDuration);
8194
mAnimatorSet.start();
8295
}
@@ -133,4 +146,13 @@ public AnimatorSet getAnimatorAgent() {
133146
return mAnimatorSet;
134147
}
135148

149+
public BaseViewAnimator setRepeatTimes(int repeatTimes) {
150+
mRepeatTimes = repeatTimes;
151+
return this;
152+
}
153+
154+
public BaseViewAnimator setRepeatMode(int repeatMode) {
155+
mRepeatMode = repeatMode;
156+
return this;
157+
}
136158
}

library/src/main/java/com/daimajia/androidanimations/library/YoYo.java

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
package com.daimajia.androidanimations.library;
2727

2828
import android.animation.Animator;
29+
import android.animation.ValueAnimator;
30+
import android.support.v4.view.ViewCompat;
2931
import android.view.View;
3032
import android.view.animation.Interpolator;
3133

@@ -37,19 +39,30 @@ public class YoYo {
3739

3840
private static final long DURATION = BaseViewAnimator.DURATION;
3941
private static final long NO_DELAY = 0;
42+
public static final int INFINITE = -1;
43+
public static final float CENTER_PIVOT = Float.MAX_VALUE;
4044

4145
private BaseViewAnimator animator;
4246
private long duration;
4347
private long delay;
48+
private boolean repeat;
49+
private int repeatTimes;
50+
private int repeatMode;
4451
private Interpolator interpolator;
52+
private float pivotX, pivotY;
4553
private List<Animator.AnimatorListener> callbacks;
4654
private View target;
4755

4856
private YoYo(AnimationComposer animationComposer) {
4957
animator = animationComposer.animator;
5058
duration = animationComposer.duration;
5159
delay = animationComposer.delay;
60+
repeat = animationComposer.repeat;
61+
repeatTimes = animationComposer.repeatTimes;
62+
repeatMode = animationComposer.repeatMode;
5263
interpolator = animationComposer.interpolator;
64+
pivotX = animationComposer.pivotX;
65+
pivotY = animationComposer.pivotY;
5366
callbacks = animationComposer.callbacks;
5467
target = animationComposer.target;
5568
}
@@ -90,7 +103,12 @@ public static final class AnimationComposer {
90103

91104
private BaseViewAnimator animator;
92105
private long duration = DURATION;
106+
93107
private long delay = NO_DELAY;
108+
private boolean repeat = false;
109+
private int repeatTimes = 0;
110+
private int repeatMode = ValueAnimator.RESTART;
111+
private float pivotX = YoYo.CENTER_PIVOT, pivotY = YoYo.CENTER_PIVOT;
94112
private Interpolator interpolator;
95113
private View target;
96114

@@ -117,6 +135,35 @@ public AnimationComposer interpolate(Interpolator interpolator) {
117135
return this;
118136
}
119137

138+
public AnimationComposer pivot(float pivotX, float pivotY) {
139+
this.pivotX = pivotX;
140+
this.pivotY = pivotY;
141+
return this;
142+
}
143+
144+
public AnimationComposer pivotX(float pivotX) {
145+
this.pivotX = pivotX;
146+
return this;
147+
}
148+
149+
public AnimationComposer pivotY(float pivotY) {
150+
this.pivotY = pivotY;
151+
return this;
152+
}
153+
154+
public AnimationComposer repeat(int times) {
155+
if (times < INFINITE) {
156+
throw new RuntimeException("Can not be less than -1, -1 is infinite loop");
157+
}
158+
repeat = times != 0;
159+
repeatTimes = times;
160+
return this;
161+
}
162+
163+
public AnimationComposer repeatMode(int mode) {
164+
repeatMode = mode;
165+
return this;
166+
}
120167

121168
public AnimationComposer withListener(Animator.AnimatorListener listener) {
122169
callbacks.add(listener);
@@ -191,18 +238,35 @@ public boolean isRunning() {
191238
return animator.isRunning();
192239
}
193240

241+
public void stop() {
242+
stop(true);
243+
}
244+
194245
public void stop(boolean reset) {
195246
animator.cancel();
196247

197248
if (reset)
198249
animator.reset(target);
199250
}
200-
201251
}
202252

203253
private BaseViewAnimator play() {
204254
animator.setTarget(target);
255+
256+
if (pivotX == YoYo.CENTER_PIVOT) {
257+
ViewCompat.setPivotX(target, target.getMeasuredWidth() / 2.0f);
258+
} else {
259+
target.setPivotX(pivotX);
260+
}
261+
if (pivotY == YoYo.CENTER_PIVOT) {
262+
ViewCompat.setPivotY(target, target.getMeasuredHeight() / 2.0f);
263+
} else {
264+
target.setPivotY(pivotY);
265+
}
266+
205267
animator.setDuration(duration)
268+
.setRepeatTimes(repeatTimes)
269+
.setRepeatMode(repeatMode)
206270
.setInterpolator(interpolator)
207271
.setStartDelay(delay);
208272

@@ -211,7 +275,6 @@ private BaseViewAnimator play() {
211275
animator.addAnimatorListener(callback);
212276
}
213277
}
214-
215278
animator.animate();
216279
return animator;
217280
}

0 commit comments

Comments
 (0)