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
Related
I am adding a infinite animation to my ImageView.
Below is my animation code:
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="70%"
android:pivotY="70%"
android:repeatCount="infinite"
android:startOffset="2000"
android:repeatMode="reverse"
android:toXScale="0.8"
android:toYScale="0.8" />
and java code is:
Animation pulse = AnimationUtils.loadAnimation(this, R.anim.pulse);
reviewImage.startAnimation(pulse);
the problem is that it make delay after each animation like expand+delay+collapse+delay both i want to give delay only after one cycle means after expand+collapse+delay
You cannot do that with one animation, because the way it works is that it performs animation and will delay the next animation by 2000ms (as specified in xml). From framework's point of view those animations are not connected to each other and it cannot assess them as one animation, so those are two separate animations.
You have to create 2 animations and play them sequentially.
See here how to do that with AnimatorSets, but you may as well do it with xml.
<set>
<scale
android:duration="500"
.../>
<scale
android:startOffset="500"
.../>
</set>
I am using animation for item click of GridView.
My animation file is expand_then_contract.xml as below
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
android:duration="500"
android:fromXScale="1"
android:fromYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.1"
android:toYScale="1.1" />
<scale
android:duration="500"
android:fromXScale="1.1"
android:fromYScale="1.1"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="500"
android:toXScale="1"
android:toYScale="1" />
</set>
And I'm applying it in the onItemClickListener of GridView as below
onItemClick(... View view ..)
{
Animation expand_contract = AnimationUtils.loadAnimation(this,
R.anim.expand_then_contract);
view.bringToFront();
view.clearAnimation();
view.setAnimation(expand_contract);
view.startAnimation(expand_contract);
}
It is working as expected. But it gets stuck in between. At some point when it is contracting back to normal position, for a moment it freezes. Why does this happen. Why can't animation in my code work as smoothly as they make it in GoLauncher Animations. Do they use any external libraries.
The problem is with interpolator , your job is done in two diffrent steps
So , Better you merge them both in one step or Use linear interpolator so that there is no halt between executoion
I've a simple view animation, but I can't see to get "rid" of the animation "unwinding" (and I can't seem to find a solution online).
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator"
android:shareInterpolator="true" >
<scale
android:duration="250"
android:fromXScale="1"
android:fromYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="0"
android:toXScale="1.1"
android:toYScale="1.1" />
</set>
What this does is, simply, inflates the View by 10% proportionally, from the middle.
But, when it executes, it inflates and, when it reaches the end, it deflates back. I want to avoid that -- the "unwinding" effect -- when it scales back from 110% to 100%.
How can that be done?
Edit:
I'm starting it simply with this:
Animation animation1 = AnimationUtils.loadAnimation(this, R.anim.<name>);
v.startAnimation(animation1);
The correct answer is found here: How can I animate a view in Android and have it stay in the new position/size?
It is putting this:
android:fillAfter="true"
android:fillEnabled="true"
inside the <set tag.
Is there a tutorial or a code example of various kinds of View Animations available in Android. Basically what I am trying to do is say if start a new Activity, I am trying to get that activity start out like zoom in till it fills the screen or Fade out when I am going to finish the activity. Is there a way i can do this ?
Any help will be greatly appreciated.
Thanks
-Chandu
So you need to use OverridePendingTransition from the activity: http://developer.android.com/reference/android/app/Activity.html
Some examples with Fade in/out can be found here: http://www.anddev.org/novice-tutorials-f8/splash-fade-activity-animations-overridependingtransition-t9464.html
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/decelerate_interpolator"
android:zAdjustment="top"
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="1000" />
Zoom:
<scale
android:pivotX="50%"
android:pivotY="50%"
android:fromXScale=".1"
android:fromYScale=".1"
android:toXScale="1.0"
android:toYScale="1.0"
android:duration="2000" />
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.