7

here I am use this code for make scale animation

Animation anim = new ScaleAnimation(1f, 0f, 1f, 0f, b, a);
anim.setDuration(130);
anim.setFillAfter(false);
view.startAnimation(anim);   
anim.start(); 

now my view animation without problem but when i add another animation to it its didn't animate any one and this is my code for make two animation its scale and translate

Animation anim = new ScaleAnimation(1f, 0f, 1f, 0f, b, a);
Animation animT = new TranslateAnimation(0f,b,0f,a);
anim.setDuration(130);
animT.setDuration(130);
anim.setFillAfter(false);
animT.setFillAfter(false);
view.startAnimation(anim);   
view.startAnimation(animT);   
anim.start(); 
animT.start();

as we can see i cant use both of the animation as same time how can i solve it without use xml animataion because my variable was changed every time

medo
  • 479
  • 3
  • 9
  • 24

3 Answers3

19

Use AnimationSet as follows:

AnimationSet set = new AnimationSet(true);

Animation anim = new ScaleAnimation(1f, 0f, 1f, 0f, b, a);
Animation animT = new TranslateAnimation(0f, b, 0f, a);

set.addAnimation(anim);
set.addAnimation(animT);
set.setDuration(130);

view.startAnimation(set);
Shaishav
  • 5,282
  • 2
  • 22
  • 41
  • can I change `new ScaleAnimation(1f, 0f, 1f, 0f, b, a);` values while animate like change `(1f, 0f, 1f, 0f, b, a)` ? – medo Aug 02 '16 at 08:42
  • @medo yeah you can. In fact if you're on Android Studio, click on `ScaleAnimation` text in your code while pressing Ctrl key. This should open up the doc and it will tell you about other constructors of the class too. – Shaishav Aug 02 '16 at 08:54
  • i didnt see any method for change the values while animate did u have an example – medo Aug 02 '16 at 09:07
  • @medo I might be mistaken here but, what exactly do you want to change? The values? As in `1f` to `0.5f`? – Shaishav Aug 02 '16 at 09:08
  • `pivotX` and `pivotY` == _(a ,b)_ if i can change them while animate i will remove translate animation – medo Aug 02 '16 at 09:11
  • @medo They are float values so yeah, they are changeable. Also look at `pivotXType` and `pivotYType` – Shaishav Aug 02 '16 at 09:14
  • I was searching a way to start two animations at once as `ObjectAnimator` was failing under API 24 and this helped me a lot. Thanks, +1. – Lalit Fauzdar Mar 08 '18 at 17:10
5

you need to use AnimationSet and add whatever animation type you want to it here is an example

Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setDuration(1000);
Animation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setStartOffset(1000);
fadeOut.setDuration(1000);
AnimationSet animation = new AnimationSet(true);
animation.addAnimation(fadeIn);
animation.addAnimation(fadeOut);
view.startAnimation(animation);
Antwan
  • 3,837
  • 9
  • 41
  • 62
2

You can use AnimationSet to add multiple animation for a View. Check out this link: Animation with animationSet() in android

Community
  • 1
  • 1
xxx
  • 3,315
  • 5
  • 21
  • 40