Skip to content
This repository was archived by the owner on Feb 8, 2022. It is now read-only.

Commit 6014f1b

Browse files
author
florent champigny
committed
added multiple view animation
1 parent 4c50644 commit 6014f1b

7 files changed

Lines changed: 261 additions & 138 deletions

File tree

.idea/workspace.xml

Lines changed: 123 additions & 64 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Animate multiple view from one method
1212
```java
1313
ViewAnimator
1414
.animate(image)
15-
.translationY(1000, 0)
15+
.translationY(-1000, 0)
1616
.alpha(0,1)
1717
.andAnimate(text)
1818
.translationX(-200, 0)
@@ -25,14 +25,17 @@ ViewAnimator
2525
.duration(1000)
2626

2727
.start();
28+
2829
```
2930

31+
[![gif](https://j.gifs.com/jR2Pp4.gif)](https://youtu.be/WB_vuyGe9GI)
32+
3033
Without ViewAnimator
3134

3235
```java
3336
AnimatorSet animatorSet = new AnimatorSet();
3437
animatorSet.playTogether(
35-
ObjectAnimator.ofFloat(image,"translationY",1000,0),
38+
ObjectAnimator.ofFloat(image,"translationY",-1000,0),
3639
ObjectAnimator.ofFloat(image,"alpha",0,1);
3740
ObjectAnimator.ofFloat(text,"alpha",-200,0);
3841
);
@@ -57,6 +60,17 @@ animatorSet.start();
5760

5861
#More
5962

63+
[![gif](https://j.gifs.com/XD6R4V.gif)](https://youtu.be/Qlj40Y6ChSM)
64+
65+
Add same animation on multiples view
66+
```java
67+
ViewAnimator
68+
.animate(image,text)
69+
.scale(0,1)
70+
71+
.start();
72+
73+
6074
Add listeners
6175
```java
6276
ViewAnimator
@@ -70,7 +84,7 @@ ViewAnimator
7084

7185
```
7286

73-
Using DP values
87+
Use DP values
7488
```java
7589
ViewAnimator
7690
.animate(image)

sample/src/main/java/com/github/florent37/sample/viewanimator/MainActivity.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
public class MainActivity extends AppCompatActivity {
1414

1515
ImageView image;
16+
ImageView montain;
1617
TextView text;
1718
TextView percent;
1819

@@ -21,6 +22,7 @@ protected void onCreate(Bundle savedInstanceState) {
2122
super.onCreate(savedInstanceState);
2223
setContentView(R.layout.activity_main);
2324
image = (ImageView) findViewById(R.id.image);
25+
montain = (ImageView) findViewById(R.id.montain);
2426
text = (TextView) findViewById(R.id.text);
2527
percent = (TextView) findViewById(R.id.percent);
2628

@@ -37,14 +39,35 @@ protected void onCreate(Bundle savedInstanceState) {
3739
});
3840
}
3941

42+
protected void simpleAnimation(){
43+
ViewAnimator
44+
.animate(montain)
45+
.translationY(-1000, 0)
46+
.alpha(0,1)
47+
.andAnimate(text)
48+
.translationX(-200, 0)
49+
.descelerate()
50+
.duration(2000)
51+
52+
.thenAnimate(image)
53+
.scale(1f,0.5f)
54+
.accelerate()
55+
.duration(1000)
56+
57+
.start();
58+
}
59+
4060
protected void animateParallel() {
4161
ViewAnimator
42-
.animate(image)
43-
.dp().translationY(1000, 0)
44-
.dp().translationX(1000, 0)
62+
.animate(montain,image)
63+
.dp().translationY(-1000, 0)
64+
.alpha(0,1)
65+
66+
.andAnimate(percent)
67+
.scale(0,1)
4568

4669
.andAnimate(text)
47-
.dp().translationX(-200, 0)
70+
.dp().translationY(1000, 0)
4871
.textColor(Color.BLACK, Color.WHITE)
4972
.backgroundColor(Color.WHITE, Color.BLACK)
5073

135 KB
Loading

sample/src/main/res/layout/activity_main.xml

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,56 +4,68 @@
44
xmlns:tools="http://schemas.android.com/tools"
55
android:layout_width="match_parent"
66
android:layout_height="match_parent"
7-
android:paddingBottom="@dimen/activity_vertical_margin"
8-
android:paddingLeft="@dimen/activity_horizontal_margin"
9-
android:paddingRight="@dimen/activity_horizontal_margin"
10-
android:paddingTop="@dimen/activity_vertical_margin"
117
tools:context="com.github.florent37.viewanimator.MainActivity">
128

13-
<TextView
14-
android:id="@+id/percent"
15-
android:layout_width="wrap_content"
16-
android:layout_height="wrap_content"
17-
android:padding="10dp"
18-
android:layout_centerHorizontal="true"
19-
android:textStyle="bold"
20-
android:text="0.00%"/>
21-
22-
<TextView
23-
android:id="@+id/text"
24-
android:layout_width="wrap_content"
25-
android:layout_height="wrap_content"
26-
android:padding="10dp"
27-
android:layout_marginTop="20dp"
28-
android:layout_below="@+id/image"
29-
android:layout_centerHorizontal="true"
30-
android:textStyle="bold"
31-
android:textColor="@android:color/black"
32-
android:text="I Love Github"/>
33-
34-
<ImageView
35-
android:layout_centerInParent="true"
36-
android:id="@+id/image"
37-
android:layout_width="100dp"
38-
android:adjustViewBounds="true"
39-
android:layout_height="wrap_content"
40-
android:src="@drawable/github"/>
9+
<LinearLayout
10+
android:layout_width="match_parent"
11+
android:layout_height="match_parent"
12+
android:orientation="vertical"
13+
android:gravity="center_horizontal"
14+
>
15+
16+
<ImageView
17+
android:id="@+id/montain"
18+
android:layout_width="match_parent"
19+
android:layout_height="200dp"
20+
android:adjustViewBounds="true"
21+
android:scaleType="centerCrop"
22+
android:src="@drawable/montain"/>
23+
24+
<TextView
25+
android:id="@+id/percent"
26+
android:layout_width="wrap_content"
27+
android:layout_height="wrap_content"
28+
android:padding="10dp"
29+
android:text="0.00%"
30+
android:textStyle="bold"/>
31+
32+
<ImageView
33+
android:id="@+id/image"
34+
android:layout_width="100dp"
35+
android:layout_height="wrap_content"
36+
android:layout_centerInParent="true"
37+
android:adjustViewBounds="true"
38+
android:src="@drawable/github"/>
39+
40+
<TextView
41+
android:id="@+id/text"
42+
android:layout_width="wrap_content"
43+
android:layout_height="wrap_content"
44+
android:padding="10dp"
45+
android:text="I Love ViewAnimator"
46+
android:textColor="@android:color/black"
47+
android:textStyle="bold"/>
48+
49+
</LinearLayout>
4150

4251
<LinearLayout
4352
android:layout_width="match_parent"
4453
android:layout_height="wrap_content"
4554
android:layout_alignParentBottom="true"
55+
android:padding="5dp"
4656
android:orientation="vertical">
57+
4758
<Button
4859
android:id="@+id/parallel"
4960
android:layout_width="match_parent"
5061
android:layout_height="wrap_content"
51-
android:text="Parallel"/>
62+
android:text="Custom Animation"/>
63+
5264
<Button
5365
android:id="@+id/sequentially"
5466
android:layout_width="match_parent"
5567
android:layout_height="wrap_content"
56-
android:text="Sequentially"/>
68+
android:text="Width / Height"/>
5769
</LinearLayout>
5870

5971
</RelativeLayout>

viewanimator/src/main/java/com/github/florent37/viewanimator/AnimationBuilder.java

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@
2121
public class AnimationBuilder {
2222

2323
private final ViewAnimator viewAnimator;
24-
private final View view;
24+
private final View[] views;
2525
private final List<Animator> animatorList = new ArrayList<>();
2626

2727
protected boolean waitForHeight;
2828
protected boolean nextValueWillBeDp = false;
2929

30-
public AnimationBuilder(ViewAnimator viewAnimator, View view) {
30+
public AnimationBuilder(ViewAnimator viewAnimator, View...views) {
3131
this.viewAnimator = viewAnimator;
32-
this.view = view;
32+
this.views = views;
3333
}
3434

3535
public AnimationBuilder dp() {
@@ -43,11 +43,11 @@ protected AnimationBuilder add(Animator animator) {
4343
}
4444

4545
protected float toDp(final float px) {
46-
return px / view.getContext().getResources().getDisplayMetrics().density;
46+
return px / views[0].getContext().getResources().getDisplayMetrics().density;
4747
}
4848

4949
protected float toPx(final float dp) {
50-
return dp * view.getContext().getResources().getDisplayMetrics().density;
50+
return dp * views[0].getContext().getResources().getDisplayMetrics().density;
5151
}
5252

5353
protected float[] getValues(float...values) {
@@ -62,7 +62,9 @@ protected float[] getValues(float...values) {
6262
}
6363

6464
public AnimationBuilder property(String propertyName, float... values) {
65-
this.animatorList.add(ObjectAnimator.ofFloat(view, propertyName, getValues(values)));
65+
for(View view : views) {
66+
this.animatorList.add(ObjectAnimator.ofFloat(view, propertyName, getValues(values)));
67+
}
6668
return this;
6769
}
6870

@@ -93,12 +95,16 @@ public AnimationBuilder scale(float... scale) {
9395
}
9496

9597
public AnimationBuilder pivotX(float pivotX) {
96-
ViewHelper.setPivotX(view, pivotX);
98+
for(View view : views) {
99+
ViewHelper.setPivotX(view, pivotX);
100+
}
97101
return this;
98102
}
99103

100104
public AnimationBuilder pivotY(float pivotY) {
101-
ViewHelper.setPivotY(view, pivotY);
105+
for(View view : views) {
106+
ViewHelper.setPivotY(view, pivotY);
107+
}
102108
return this;
103109
}
104110

@@ -115,30 +121,36 @@ public AnimationBuilder rotation(float... rotation) {
115121
}
116122

117123
public AnimationBuilder backgroundColor(int... colors) {
118-
ObjectAnimator objectAnimator = ObjectAnimator.ofInt(view, "backgroundColor", colors);
119-
objectAnimator.setEvaluator(new ArgbEvaluator());
120-
this.animatorList.add(objectAnimator);
124+
for(View view : views) {
125+
ObjectAnimator objectAnimator = ObjectAnimator.ofInt(view, "backgroundColor", colors);
126+
objectAnimator.setEvaluator(new ArgbEvaluator());
127+
this.animatorList.add(objectAnimator);
128+
}
121129
return this;
122130
}
123131

124132
public AnimationBuilder textColor(int... colors) {
125-
if (view instanceof TextView) {
126-
ObjectAnimator objectAnimator = ObjectAnimator.ofInt(view, "textColor", colors);
127-
objectAnimator.setEvaluator(new ArgbEvaluator());
128-
this.animatorList.add(objectAnimator);
133+
for(View view : views) {
134+
if (view instanceof TextView) {
135+
ObjectAnimator objectAnimator = ObjectAnimator.ofInt(view, "textColor", colors);
136+
objectAnimator.setEvaluator(new ArgbEvaluator());
137+
this.animatorList.add(objectAnimator);
138+
}
129139
}
130140
return this;
131141
}
132142

133143
public AnimationBuilder custom(final AnimationListener.Update update, float... values) {
134-
ValueAnimator valueAnimator = ValueAnimator.ofFloat(getValues((values)));
135-
if (update != null)
136-
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
137-
@Override public void onAnimationUpdate(ValueAnimator animation) {
138-
update.update(view, (Float) animation.getAnimatedValue());
139-
}
140-
});
141-
add(valueAnimator);
144+
for(final View view : views) {
145+
ValueAnimator valueAnimator = ValueAnimator.ofFloat(getValues((values)));
146+
if (update != null)
147+
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
148+
@Override public void onAnimationUpdate(ValueAnimator animation) {
149+
update.update(view, (Float) animation.getAnimatedValue());
150+
}
151+
});
152+
add(valueAnimator);
153+
}
142154
return this;
143155
}
144156

@@ -170,12 +182,12 @@ protected List<Animator> createAnimators() {
170182
}
171183

172184
//region Animate New View
173-
public AnimationBuilder andAnimate(View view) {
174-
return viewAnimator.addAnimationBuilder(view);
185+
public AnimationBuilder andAnimate(View...views) {
186+
return viewAnimator.addAnimationBuilder(views);
175187
}
176188

177-
public AnimationBuilder thenAnimate(View view) {
178-
return viewAnimator.thenAnimate(view);
189+
public AnimationBuilder thenAnimate(View...views) {
190+
return viewAnimator.thenAnimate(views);
179191
}
180192
//endregion
181193

@@ -227,8 +239,11 @@ public ViewAnimator descelerate() {
227239

228240
//endregion
229241

242+
public View[] getViews() {
243+
return views;
244+
}
230245
public View getView() {
231-
return view;
246+
return views[0];
232247
}
233248

234249
public boolean isWaitForHeight() {

viewanimator/src/main/java/com/github/florent37/viewanimator/ViewAnimator.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,20 @@ public class ViewAnimator {
2929
ViewAnimator prev = null;
3030
ViewAnimator next = null;
3131

32-
public static AnimationBuilder animate(View view) {
32+
public static AnimationBuilder animate(View...view) {
3333
ViewAnimator viewAnimator = new ViewAnimator();
3434
return viewAnimator.addAnimationBuilder(view);
3535
}
3636

37-
public AnimationBuilder thenAnimate(View view) {
37+
public AnimationBuilder thenAnimate(View...views) {
3838
ViewAnimator nextViewAnimator = new ViewAnimator();
3939
this.next = nextViewAnimator;
4040
nextViewAnimator.prev = this;
41-
return nextViewAnimator.addAnimationBuilder(view);
41+
return nextViewAnimator.addAnimationBuilder(views);
4242
}
4343

44-
public AnimationBuilder addAnimationBuilder(View view) {
45-
AnimationBuilder animationBuilder = new AnimationBuilder(this, view);
44+
public AnimationBuilder addAnimationBuilder(View...views) {
45+
AnimationBuilder animationBuilder = new AnimationBuilder(this, views);
4646
animationList.add(animationBuilder);
4747
return animationBuilder;
4848
}

0 commit comments

Comments
 (0)