Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android set two animation on view programmatically

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

like image 639
medo Avatar asked Aug 02 '16 08:08

medo


People also ask

Can be used to animate between two or more views?

In Android, ViewAnimator is a sub class of FrameLayout container that is used for switching between views. It is an element of transition widget which helps us to add transitions on the views ( like TextView, ImageView or any layout). It is mainly useful to animate the views on screen.

How to set animation on TextView in Android?

To start the animation we need to call the startAnimation() function on the UI element as shown in the snippet below: sampleTextView. startAnimation(animation); Here we perform the animation on a textview component by passing the type of Animation as the parameter.

What is Transition animation in Android?

Android's transition framework allows you to animate all kinds of motion in your UI by simply providing the starting layout and the ending layout.


2 Answers

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);
like image 67
Shaishav Avatar answered Sep 25 '22 08:09

Shaishav


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);
like image 36
Antwan Avatar answered Sep 26 '22 08:09

Antwan