Applying animation more than once in android - android

I have a problem with applying an animation
Given the following animation code
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromXDelta="0"
android:interpolator="#anim/cycle_10"
android:toXDelta="10" />
after starting the application this animation can be applied only once
public void onClick{
button.setAnimation(shake);
}
how to apply this animation more than once?

View.startAnimation(Animation) ?

add following lines in animation xml-
android:repeatMode="reverse"
android:repeatCount="infinite"

Related

android animation add Easings effect

I have one LinearLayout. My layout first time is gone and on button click i visible it with left to right animation.this is my source:
public void ShowSlideMenu(LinearLayout layout)
{
if(layout.getVisibility()==View.GONE)
{
if(LeftSwipe==null)
LeftSwipe=AnimationUtils.loadAnimation(getActivity(),R.anim.slide_menu_lefttoright);
layout.startAnimation(LeftSwipe);
layout.setVisibility(View.VISIBLE);
}
}
this is xml animation code:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="true">
<translate
android:duration="#android:integer/config_mediumAnimTime"
android:fromXDelta="100%"
android:fromYDelta="0%"
android:toXDelta="0%"
android:toYDelta="0%" />
animation working perfect but now i want to add Easing effect. i read material design animation and i try to recive like this easing effect
https://www.google.com/design/spec/animation/authentic-motion.html#authentic-motion-mass-weight
and also like this
http://api.jqueryui.com/easings/
if anyone knows solution please help me thanks everyone
A solution to this should be adding an interpolator to your animation set.
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:interpolator="#android:anim/decelerate_interpolator"
android:shareInterpolator="true">
<translate
android:duration="#android:integer/config_mediumAnimTime"
android:fromXDelta="100%"
android:fromYDelta="0%"
android:toXDelta="0%"
android:toYDelta="0%" />
</set>
The interpolator will take your animation and change its speed over the duration of the animation. In the example I chose a decelerate interpolator that makes the animation starting fast and then getting slower and slower during the time of the animation.

Multiple animations on 1 imageview android

I have 2 animations which are already working,
i want to fade my train + tween my train on the same time.
If I execute 1 of these lines it works.
But if I try to execute both it, only 1 will work..
I really can't find a solution here.
Maybe you can help?
final ImageView mytrain = (ImageView) findViewById(R.id.train);
final Animation traintween = AnimationUtils.loadAnimation(this,R.anim.treinanimation);
final Animation trainfade = AnimationUtils.loadAnimation(this,R.anim.trainfade);
mytrain.startAnimation(trainfade);
mytrain.startAnimation(trainntween);
I want mytrain to execute both animations..
Thank you for the help!
Use the AnimationSet class:
AnimationSet s = new AnimationSet(false);//false means don't share interpolators
s.addAnimation(traintween);
s.addAnimation(trainfad);
mytrain.startAnimation(s);
You need to use an AnimationSet, check out the docs. Just call addAnimation() for each Animation you want to play.
Can be done programatically using AnimatorSet class of android :
final AnimatorSet mAnimatorSet = new AnimatorSet();
mAnimatorSet.playTogether(
ObjectAnimator.ofFloat(img_view,"scaleX",1,0.9f,0.9f,1.1f,1.1f,1),
ObjectAnimator.ofFloat(img_view,"scaleY",1,0.9f,0.9f,1.1f,1.1f,1),
ObjectAnimator.ofFloat(img_view,"rotation",0 ,-3 , -3, 3, -3, 3, -3,3,-3,3,-3,0)
);
//define any animation you want,like above
mAnimatorSet.setDuration(2000); //set duration for animations
mAnimatorSet.start();
this example will start all the 3 animation on the target view(imgView) at same time ,you can also use playSequentially .....
For complete example check this out..
here is the example of all animation in a single xml file...
this will help you but first you should read the docs of AnimationSet
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale android:fromXScale="1.0" android:toXScale="3.0"
android:fromYScale="1.0" android:toYScale="3.0" android:pivotX="50%"
android:pivotY="50%" android:duration="5000" />
<set>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0.2" android:toAlpha="1.0" android:duration="3000" />
<rotate android:fromDegrees="0" android:toDegrees="-360"
android:toYScale="0.0" android:pivotX="50%" android:pivotY="50%"
android:startOffset="700" android:duration="4000" />
<!-- <translate android:fromXDelta="0%" android:toXDelta="0%" -->
<!-- android:fromYDelta="0%" android:toYDelta="100%" android:duration="3000" -->
</set>
</set>
you also can use the ImageSwitcher, i think this is better then AnimationSet in your case

Fade animation - not constant behavior Android

I want to make a Fade Out and In Animation every time I click a button.
I started to check only the fade out and on the first click it seems the Fade Out works just fine. But when I click again the animation fade from top to bottom, making it look bad and cut.
The Animation:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="1.0"
android:toAlpha="0.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:duration="1000" />
</set>
The Code:
Animation fadeOut = AnimationUtils.loadAnimation(myActivity.this, R.anim.fade_out);
LinearLayout myBackground=(LinearLayout)findViewById(R.id.myBackground);
myBackground.setAnimation(fadeOut);
What am I missing? Thanks!
Make anim in following folder ris->anim->fade_anim.xml
Add below xml
<alpha
android:duration="2000"
android:fromAlpha="1"
android:toAlpha="0.0"
android:repeatCount="infinite"/>
Got to java class
//make animation object
final Animation myAnim = AnimationUtils.loadAnimation(getContext(), R.anim.bounce_anim);
myAnim.setRepeatMode(Animation.INFINITE);
//get View on which on you want to apply animation I am applying on imageView
mLayout.findViewById(R.id.imageView).startAnimation(myAnim);

How to give animation to the ViewSwitcher

I have created one advertisement control which consists of ViewSwitcher....
in that control i have ImageView and TextView because advertisement are of either text or images..
Now i have to give animation to the advetisements..
I have tried following
Animation inAnimation = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
inAnimation.setDuration(1500);
Animation outAnimation = AnimationUtils.loadAnimation(this,
android.R.anim.slide_out_right); outAnimation.setDuration(1500);
And i set it to the switcher as
ViewSwitcher switcher;
switcher.setInAnimation(inAnimation);
switcher.setOutAnimation(outAnimation);
but it won't work..
Please give me any other alternative.. Or if use of above code is wrong then how to use it??
Try setting animation inside xml as
<ViewSwitcher
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:inAnimation="#android:anim/slide_in_left"
android:outAnimation="#android:anim/slide_out_right" >
In addition to this :
Take care of switcher.showNext(); or switcher.showPrevious();
If you set an animation to the switcher, both action will result in the same animation.
I am giving an alternative as mentioned in the question. Using this you will achieve the same animation that ViewPager have.
Instead of showNext, you can set displayedChild to 1.
switcherView?.inAnimation = AnimationUtils.loadAnimation(baseActivity, R.anim.slide_in_right)
switcherView?.outAnimation = AnimationUtils.loadAnimation(baseActivity, R.anim.slide_out_left)
switcherView?.displayedChild = 1
Instead of showPrevious, you can set displayedChild to 0.
switcherView?.inAnimation = AnimationUtils.loadAnimation(baseActivity, R.anim.slide_in_left)
switcherView?.outAnimation = AnimationUtils.loadAnimation(baseActivity, R.anim.slide_out_right)
switcherView?.displayedChild = 0
Animation Files:
R.anim.slide_in_right:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="#android:integer/config_shortAnimTime"
android:fromXDelta="100%p"
android:toXDelta="0" />
</set>
R.anim.slide_out_left
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="#android:integer/config_shortAnimTime"
android:fromXDelta="0"
android:toXDelta="-100%p" />
</set>
R.anim.slide_in_left
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="#android:integer/config_shortAnimTime"
android:fromXDelta="-100%p"
android:toXDelta="0" />
</set>
R.anim.slide_out_right
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="#android:integer/config_shortAnimTime"
android:fromXDelta="0"
android:toXDelta="100%p" />
</set>
Each time you set displayedChild, you need to set the animation. If you don't want the animation, you can simply neglect it.That's all. Happy Coding!
PS: Kotlin code
Nothing happen or you got some error? What u mean by it won't work?
Did you start animation with switcher.showNext(); or switcher.showPrevious();
Hope it will help.. Cheers ;)
A "switcher.showNext();" from the last layout and a "switcher.showPrevious();" from the first layout gives error. It must be similar to the stackoverflow and stackunderflow situation in a stack.so before you call "showNext()" check its not the last layout that you are currently in and also that you are not in the first layout when calling "showPrevious()". I came across this simple mistake.
Sorry for making this a post, I am (a rookie) not yet authorised to comment on posts

Android animation startOffset makes the target view blink and not animate

I'm trying to create an animation which will slide a textview out to the left and slide in again from the right. Essentially, this would be the same text effect used in the Stopwatch & Timer app (sportstracklive is the developer).
I can use either of these animation sets exclusive of the other and it works fine, does exactly what I want. But as soon as I try using them together, the TextView just blinks over the course about around 1 second. Removing the startOffset works as expected. Both animation sets run simultaneously.
Here's the XML:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0%"
android:toXDelta="-25%"
android:fromYDelta="0%"
android:toYDelta="0%"
android:duration="#android:integer/config_shortAnimTime"
/>
<alpha
android:interpolator="#android:anim/decelerate_interpolator"
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="#android:integer/config_shortAnimTime"
/>
<set>
<translate
android:fromXDelta="25%"
android:toXDelta="0%"
android:fromYDelta="0%"
android:toYDelta="0%"
android:startOffset="#android:integer/config_shortAnimTime"
android:duration="#android:integer/config_shortAnimTime"
/>
<alpha
android:interpolator="#android:anim/decelerate_interpolator"
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:startOffset="#android:integer/config_shortAnimTime"
android:duration="#android:integer/config_shortAnimTime"
/>
</set>
</set>
And here's the lengthy Java code that runs it:
AnimationSet mSlideRightToLeft =
(AnimationSet) AnimationUtils.loadAnimation(this, R.anim.slide_right_to_left);
mMyTextView.startAnimation(mSlideRightToLeft);
Justinl's comment is correct. I had the exact same problem a couple months ago. Remove the set tags around the other animations, and keep the startOffsets.
Next remove the animation set in your code and just do a normal load animation:
Animation a = AnimationUtils.loadAnimation(this, R.anim.slide_right_to_left);
mMyTextView.startAnimation(a);
Edit: Yea, it looks like Android simply doesn't like this setup when there are multiple animations at the same time. I think you might have to create separate files for each set of animations and then configure them via an AnimationSet inside your program.

Categories

Resources