Im trying to do an alpha and translate in a RelativeLayout. I define both:
AlphaAnimation alpha;
alpha = new AlphaAnimation(0.0f, 1.0f);
alpha.setDuration(1500);
alpha.setFillAfter(true);
TranslateAnimation translate;
translate = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 1,
Animation.RELATIVE_TO_SELF, 0);
translate.setDuration(1000);
So I start the animation in my RelativeLayout
RelativeLayout.startAnimation(translate);
RelativeLayout.startAnimation(alpha);
The problem is that in this case, only the alpha animation start and not the translation. Can someone help me? The question is how can I start two different animations at the same time in the same object(Relative Layout in my case)
I resolve the question. I added it:
AnimationSet animationSet = new AnimationSet(true);
animationSet.addAnimation(alpha);
animationSet.addAnimation(translate);
RelativeLayout.startAnimation(animationSet);
You can use animation set ıf you want to run two animation in the same time:
http://developer.android.com/reference/android/view/animation/AnimationSet.html
For exeample;
as = new AnimationSet(true);
as.setFillEnabled(true);
as.setInterpolator(new BounceInterpolator());
TranslateAnimation ta = new TranslateAnimation(-300, 100, 0, 0);
ta.setDuration(2000);
as.addAnimation(ta);
TranslateAnimation ta2 = new TranslateAnimation(100, 0, 0, 0);
ta2.setDuration(2000);
ta2.setStartOffset(2000); // allowing 2000 milliseconds for ta to finish
as.addAnimation(ta2);
Your current code won't work, because as soon as the first animation starts, the second one ends it and starts itself. So you need to wait till it's done.
Try this :
translate.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
RelativeLayout.startAnimation(alpha);
}
});
If you want to execute them simultaneously, I'd suggest you create an animation.xml file in your res/anim/ folder.
Example:
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale=".75"
android:toYScale=".75"
android:duration="1500"/>
<rotate
android:fromDegrees="0"
android:toDegrees="360"
android:duration="1500"
android:pivotX="50%"
android:pivotY="50%" />
<scale
android:fromXScale=".75"
android:fromYScale=".75"
android:toXScale="1"
android:toYScale="1"
android:duration="1500"/>
</set>
Java Code:
Animation multiAnim = AnimationUtils.loadAnimation(this, R.anim.animation);
RelativeLayout.startAnimation(multiAnim);
Related
On touch apple "falls" (translates) down from a tree and should rotate around itself and translate.
Animation apple3_anim = AnimationUtils.loadAnimation(this,
R.anim.apple3_animation);
apple3.startAnimation(apple3_anim);
RotateAnimation r = new RotateAnimation(0.0f,
720.0f, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
r.setDuration(2000);
apple3.startAnimation(r);
the xml code for apple3_animation.xml file:
<?xml version="1.0" encoding="utf-8"?>
<translate
android:fromXDelta="0"
android:toXDelta="0"
android:fromYDelta="0"
android:toYDelta="450"
android:duration="1200"
>
</translate>
The issue is that the animations don't happen sequentially,only the last 'r' animation gets executed - the apple rotates around itself 720degrees.
How do I make it go sequentially? even if i put all things in xml file, in the xml , only the last animation gets executed!
using animator can solve it quikely
apple3.animate().translationY(450).setDuration(1200).setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
iv.animate().rotation(720).setDuration(2000);
}
}).start();
I have an application where i spawn a new enemy every 4 seconds. the enemy goes from right to left using anTranslateAnimation. This animation takes 7 seconds total.
The problem that I'm facing now is that when the animation is running, every time that i spawn a new enemy, the animation stops for the old one and starts animating the new enemy.
Is there any way to animate two different objects using the same TranslateAnimation?
Just in case, here is my animation
translate= new TranslateAnimation(
Animation.ABSOLUTE, (float) 1.0,
Animation.ABSOLUTE, (float) -4.0,
Animation.ABSOLUTE,0,
Animation.ABSOLUTE,0);
translate.setDuration(10000);
translate.setFillAfter(true);
//newIV is the enemy's ImageView
newIV.startAnimation(translate);
You can apply animation like this
make an animation xml in anim folder
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="100%" android:toXDelta="0%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="500"
/>
</set>
Then code in class
final Animation RightToLeft = AnimationUtils.loadAnimation(context,
R.anim.right_to_left);
((ImageView)findViewById(R.id.yourImage))
.startAnimation(RightToLeft);
Hope it will help.
You can set a same animation to Different views in same position.try to use animationLisener
translate.setAnimationListener(new TranslateAnimation.AnimationListener() { #Override
public void onAnimationStart(Animation animation) { }
#Override
public void onAnimationRepeat(Animation animation) { }
#Override
public void onAnimationEnd(Animation animation)
{
animation.setFillAfter(false);
RelativeLayout.LayoutParams params =(RelativeLayout.LayoutParams)mLogo.getLayoutParams();
//Set your firest view layout or hide the previous newIV
//This ll avoid filckering
animation = new TranslateAnimation(0.0f, 0.0f, 0.0f, 0.0f);
animation.setDuration(1);
ImageView mLogo = new ImageView(activity);// Second enemy as u said
animation = new TranslateAnimation(
Animation.ABSOLUTE, (float) 1.0,
Animation.ABSOLUTE, (float) -4.0,
Animation.ABSOLUTE,0,
Animation.ABSOLUTE,0);
mLogo.startAnimation(animation);mLogo.setLayoutParams(params);
mLogo.clearAnimation();
mLogo.startAnimation(animation);
}
});
This ll animate When first enemy going to end create a new instance of animation and use.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.vitesse);
gpsManager = new GPSManager();
gpsManager.startListening(getApplicationContext());
gpsManager.setGPSCallback(this);
Typeface tf = Typeface.createFromAsset(getAssets(),
"font/DS-DIGI.TTF");
TextView loding =(TextView)findViewById(R.id.info_message);
loding.setTypeface(tf);
AlphaAnimation fadeIn = new AlphaAnimation(0.0f , 1.0f ) ;
AlphaAnimation fadeOut = new AlphaAnimation( 1.0f , 0.0f ) ;
loding.startAnimation(fadeIn);
loding.startAnimation(fadeOut);
fadeIn.setDuration(500);
fadeOut.setDuration(1200);
fadeOut.setStartOffset(1200+fadeIn.getStartOffset()+1200);
measurement_index = AppSettings.getMeasureUnit(this);
}
I want to repeat this of textview loding animation until gotting an inforamtion from GPS
Like this.
animation.setRepeatCount(Animation.INFINITE);
Android gives you elegant mechanisms to represent the loading process. You could use an indeterminate ProgressBar, or an ImageView with a scale/alpha animation, instead of animating the TextView itself.
Maybe you might find this animation useful, for animating alpha and scale at the same time. Vary the parameters for your preference:
file res/anim/alpha_scale_animation.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
android:fromXScale="0.7"
android:toXScale="1.0"
android:fromYScale="0.7"
android:toYScale="1.0"
android:pivotX="50%p"
android:pivotY="50%p"
android:duration="4000"
android:repeatCount="infinite"
/>
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="2000"
android:repeatMode="reverse"
android:repeatCount="infinite"
/>
</set>
then to launch it in your code:
Animation connectingAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.alpha_scale_animation);
myView.startAnimation(connectingAnimation);
And to stop it:
myView.clearAnimation();
connectingAnimation.cancel();
connectingAnimation.reset();
You can also use libraries like ProgressButtonView to have both user interaction and loading process supported in the same widget.
Hope that any of these solutions result useful to someone :)
You can use these methods to control the repeat behavior:
fadeIn.setRepeatCount(int count) // or Animation.INFINITE
fadeIn.setRepeatMode(Animation.REPEAT) // or any other repeat mode, such as Animation.REVERSE
implement these listeners, if needed:
// anim = your animation
anim.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation arg0)
{
// TODO Auto-generated method stub
}
public void onAnimationRepeat(Animation arg0)
{
// TODO Auto-generated method stub
}
public void onAnimationEnd(Animation arg0)
{
// TODO Auto-generated method stub
}
});
if you wanna stop your animation suddenly, use yourView.clearAnimation() I hope this helps.
to repeat animation just add the code in xml file which located in anim folder file in which we create a animation object.
android:repeatCount="infinite"
android:repeatMode="restart"
Set repeat count to -1 then it will animate infinite
I'm trying to make simple animation that would repeat several times (or infinitely).
It seems that android:repeatCount does not work!
Here is my animation resource from /res/anim/first_animation.xml :
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false"
android:repeatCount="infinite"
>
<scale
android:interpolator="#android:anim/decelerate_interpolator"
android:duration="500"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="1.2"
android:toYScale="1.2"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="false" />
<scale
android:interpolator="#android:anim/accelerate_interpolator"
android:startOffset="500"
android:duration="500"
android:fromXScale="1.2"
android:fromYScale="1.2"
android:toXScale="1.0"
android:toYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="false" />
</set>
First it should scale image from 1.0 to 1.2 size in 500 ms.
And then scale it back to 1.0 in 500 ms.
Here is how I'm using it:
Animation firstAnimation = AnimationUtils.loadAnimation(this, R.anim.first_animation);
imgView.startAnimation(firstAnimation);
It makes one cycle and then finishes.
It scales up, then scales down ans then stops.
How can I make this work as intended?
Update: Back in Sep, 2011 an Android engineer fixed this issue for the most part. The attributes that were ignored in XML now work, with the exception of repeatCount and fillEnabled which are still ignored (on purpose for some reason). This means it still isn't easy to repeat an AnimationSet unfortunately.
For details please see the overview in the updated docs (explains which attributes are ignored, which work, and which are passed onto children). And for a deeper understanding of what fillAfter, fillBefore, and fillEnabled actually do, see the engineer's (Chet Haase) blog post about it here.
Original Answer
To expand upon answers by Pavel and others: it is true that the <set> tag is ridiculously buggy. It can't deal correctly with repeatCount and a number of other attributes.
I spent a few hours figuring out what it can and can't deal with and have submitted a bug report/issue here: Issue 17662
In summary (this concerns AnimationSets):
setRepeatCount() / android:repeatCount
This attribute (as well as repeatMode) does not work in code or XML. This makes repeating an entire set of animations difficult.
setDuration() / android:duration
Setting this on an AnimationSet in code WORKS (overrides all durations of children animations), but not when included in the tag in XML
setFillAfter() / android:fillAfter
This works in both code and XML for the tag. Strangely I have gotten it to also work without the need to set fillEnabled to true.
setFillBefore() / android:fillBefore
Seems to have no effect/ignored in both code and XML
setFillEnabled() / android:fillEnabled
Seems to have no effect/ignored in both code and XML. I can still get fillAfter to work even without including fillEnabled or setting fillEnabled to false.
setStartOffset() / android:startOffset
This works only in code and not XML.
I've found that <set> tag has buggy implementation in class AnimationSet.
It can't deal correctly with repeatCount.
What we can do - is to set repeatCount directly in <scale> tag.
This XML resource is working well:
<?xml version="1.0" encoding="utf-8"?>
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:duration="200"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="1.05"
android:toYScale="1.05"
android:pivotX="50%"
android:pivotY="50%"
android:repeatMode="reverse"
android:fillAfter="false"
android:repeatCount="24"
/>
Unfortunately, this is limited to only one animation at once.
We can not define a sequence of animations this way...
You should include the attribute
android:repeatCount="infinite"
But in your "scale" animation not in "set"
To get a repeating animation I utilized the animation listener, and called the animation again when it ended. This does a camera reticule focusing like animation with brackets.
Here is the animation layout xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromXScale="1.0"
android:toXScale=".7"
android:fromYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:toYScale=".7"
android:duration="1000"/>
<scale
android:duration="1000"
android:fromXScale=".7"
android:toXScale="1.0"
android:fromYScale=".7"
android:pivotX="50%"
android:pivotY="50%"
android:toYScale="1.0"
android:startOffset="1000"/>
</set>
Here is the java code
public void startAnimation() {
View brackets = findViewById(R.id.brackets);
brackets.setVisibility(View.VISIBLE);
Animation anim = AnimationUtils.loadAnimation(BuzzFinderActivity.this, R.anim.crosshair_focusing);
anim.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationEnd(Animation arg0) {
Animation anim = AnimationUtils.loadAnimation(BuzzFinderActivity.this, R.anim.crosshair_focusing);
anim.setAnimationListener(this);
brackets.startAnimation(anim);
}
#Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
});
brackets.startAnimation(anim);
}
I was also facing the same problem..
i included android:repeatCount="infinite" in XMl file..now its working fine...
<translate
android:fromXDelta="0"
android:toXDelta="80"
android:duration="1000"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="true"/>
you can try this code.
In your code just add,
firstAnimation.setRepeatCount(5);
This will repeat the animation for a definite time
firstAnimation.setRepeatCount(Animation.INFINITE);
firstAnimation.setRepeatMode(Animation.INFINITE);
This will repeat the animation indefinitely.
I tried to use Daniel's code to show animation exact number of times and had a problem: animation was shown approximatily n / 2 times, when n times expected.
So I have modified Daniel's code:
//...
#Override
public void onAnimationEnd(Animation arg0) {
mCurrentCount++;
if (mCurrentCount < REPEAT_COUNT) {
Animation anim = AnimationUtils.loadAnimation(BuzzFinderActivity.this, R.anim.crosshair_focusing);
anim.setAnimationListener(this);
brackets.post(new Runnable() {
#Override
public void run() {
brackets.startAnimation(anim);
}
}
}
}
//...
Using variant, shown above, animation is shown exectly REPEAT_COUNT times, because View.post() method gives an ability to start new animation after finishing all actions, related with previous animation.
you have to add just one line in your xml code that i suggested below .
<scale
android:duration="500"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="1.2"
android:toYScale="1.2"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite" // just add this one line
android:fillAfter="false"
/>
</set>
I solved this problem using android:repeatMode="reverse" before in my project.
<scale
android:interpolator="#android:anim/decelerate_interpolator"
android:duration="500"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="1.2"
android:toYScale="1.2"
android:pivotX="50%"
android:pivotY="50%"
android:repeatMode="reverse"
android:repeatCount="infinite" />
With android sdk version 4.0.3:
In the given animation elements:
android:repeatCount="-1"
makes it an infinite animation.
Add the following class to your project:
import android.view.View;
import android.view.animation.Animation;
public class AnimationRepeater implements Animation.AnimationListener
{
private View view;
private Animation animation;
private int count;
public AnimationRepeater(View view, Animation animation)
{
this.view = view;
this.animation = animation;
this.count = -1;
}
public AnimationRepeater(View view, Animation animation, int count)
{
this.view = view;
this.animation = animation;
this.count = count;
}
public void start()
{
this.view.startAnimation(this.animation);
this.animation.setAnimationListener(this);
}
#Override
public void onAnimationStart(Animation animation) { }
#Override
public void onAnimationEnd(Animation animation)
{
if (this.count == -1)
this.view.startAnimation(animation);
else
{
if (count - 1 >= 0)
{
this.animation.start();
count --;
}
}
}
#Override
public void onAnimationRepeat(Animation animation) { }
}
For infinite loop of your view, do the following:
Animation a = AnimationUtils(Context, R.anim.animation);
new AnimationRepeater(View, a).start();
If you want to repeat the animation for N-times only, do the following:
Animation a = AnimationUtils(Context, R.anim.animation);
new AnimationRepeater(View, a, int N).start();
N stands for number of repetitions.
I do most of my stuff programmatically and I may be late or inefficient on this one but this but I completed the repeat animationset goal (I a even have 2 alternating animation sets). All this code does is simply fade in one image,pause, then fade out, fade in another image, pause, fade out, and bring back the first one (rinse and repeat). I first defined my Imageviews:
final ImageView purple = (ImageView)findViewById(R.id.purp);
final ImageView yellow = (ImageView)findViewById(R.id.yell);
purple.setVisibility(View.INVISIBLE);
yellow.setVisibility(View.INVISIBLE);
Then I made two timers, task timers and handlers to deal with when to start and stop each animation:
Timer p = new Timer();
TimerTask pu = new TimerTask() {
public void run() {
handler1.post(new Runnable() {
public void run()
{
fadein(purple);
}
});
}};
p.schedule(pu, 6000, 12000);
final Handler handler2 = new Handler();
Timer y = new Timer();
TimerTask ye = new TimerTask() {
public void run() {
handler2.post(new Runnable() {
public void run()
{
fadein(yellow);
}
});
}};
y.schedule(ye, 0, 12000);
Finally, rather than creating animation sets by adding animations, I just animations listeners to to determine when to start each animation:
public void fadein (final ImageView image)
{
Animation anim = new AlphaAnimation(0, 1);
anim.setDuration(2000);
image.startAnimation(anim);
anim.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation animation)
{
image.clearAnimation();
image.invalidate();
pause(image);
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
});
}
public void pause (final ImageView image)
{
Animation anim = new AlphaAnimation(1, 1);
anim.setDuration(2000);
image.startAnimation(anim);
anim.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation animation)
{
image.clearAnimation();
image.invalidate();
fadeout(image);
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
});
}
public void fadeout (final ImageView image)
{
Animation anim = new AlphaAnimation(1,0);
anim.setDuration(2000);
image.startAnimation(anim);
anim.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation animation)
{
image.clearAnimation();
image.invalidate();
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
});
}
The clearanimation and invalidate where just previous attempts and getting this thing to work right. I don't know if they are required or not.
Hope this helps someone.
Ryan
i got this to go ...i was trying to get a view to rotate in a circle continuously .
previous i was using rotation.setRepeatMode(-1) but that didn't work. switched to setrepeatcount and it works. This is on jelly bean 4.2.2
ObjectAnimator rotation = ObjectAnimator.ofFloat(myview,
"rotation", 360).setDuration(2000);
rotation.setRepeatMode(-1);
rotation.setRepeatCount(Animation.INFINITE);
rotation.start();
I've faced the same problem, but didn't want to do any timing things in Java because of the point that the UI thread may be very busy sometimes.
The INFINITE flag doesn't work for the set tag. So I resolved the issue with a little piece of code:
mAnimation = (AnimationSet) AnimationUtils.loadAnimation(myContext, R.anim.blink);
mIcon.startAnimation(mAnimation);
mAnimation.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation animation) {}
public void onAnimationRepeat(Animation animation) {}
public void onAnimationEnd(Animation animation) {
mIcon.startAnimation(mAnimation);
}
});
with the following XML:
<alpha
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
<alpha
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromAlpha="0.9"
android:startOffset="1000"
android:toAlpha="0.0" />
Where mIcon is an ImageView from my layout.
I've solved this problem. This is my version of the fix:
public class HelloAndroidActivity extends Activity {
private static String TAG = "animTest";
private Animation scaleAnimation;
private int currentCover = 0;
private List<ImageView> imageViews = new ArrayList<ImageView>(3);
private Button btn;
private ImageView img;
/**
* Called when the activity is first created.
* #param savedInstanceState If the activity is being re-initialized after
* previously being shut down then this Bundle contains the data it most
* recently supplied in onSaveInstanceState(Bundle). <b>Note: Otherwise it is null.</b>
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "onCreate");
setContentView(R.layout.test);
img = (ImageView)findViewById(R.id.testpict);
imageViews.add(img);
img = (ImageView)findViewById(R.id.testpictTwo);
imageViews.add(img);
img = (ImageView)findViewById(R.id.testpict3);
imageViews.add(img);
scaleAnimation = AnimationUtils.loadAnimation(this, R.anim.photo_scale);
scaleAnimation.setAnimationListener(new CyclicAnimationListener());
btn = (Button)findViewById(R.id.startBtn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
imageViews.get(0).startAnimation(scaleAnimation);
}
});
}
private class CyclicAnimationListener implements AnimationListener{
#Override
public void onAnimationEnd(Animation animation) {
currentCover += 1;
if(currentCover >= imageViews.size()){
currentCover = 0;
}
img = imageViews.get(currentCover);
scaleAnimation = AnimationUtils.loadAnimation(HelloAndroidActivity.this, R.anim.photo_scale);
scaleAnimation.setAnimationListener(new CyclicAnimationListener());
img.startAnimation(scaleAnimation);
}
#Override
public void onAnimationRepeat(Animation animation) {
Log.d("Animation", "Repeat");
}
#Override
public void onAnimationStart(Animation animation) {
}
}
}
i just came across this issue while working on a backwards compatible app. so frustrating! i ended up coding a nice workaround class that can be called from onCreate and will kickoff any animation resource into an indefinite loop.
the class, AnimationLooper, is available here:
https://gist.github.com/2018678
After researching through the answers from internet, I found a solutions which works perfectly for me. (And yes, the repeatCount and repeatMode is extremely buggy when used together with animationSet).
anim_rotate_fade.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:ordering="together" >
<objectAnimator
android:duration="3000"
android:propertyName="rotation"
android:repeatCount="1"
android:valueTo="360"
android:valueType="floatType" />
<objectAnimator
android:duration="3000"
android:propertyName="alpha"
android:repeatCount="1"
android:repeatMode="reverse"
android:valueFrom="0.0"
android:valueTo="0.3"
android:valueType="floatType" />
<objectAnimator
android:duration="3000"
android:propertyName="y"
android:repeatCount="1"
android:repeatMode="reverse"
android:valueFrom="380"
android:valueTo="430"
android:valueType="floatType" />
</set>
In activity:
(Solve it by introducing a slight delay after animation ended).
ImageView starlightImageView = new ImageView(this);
starlightImageView.setImageResource(R.drawable.starlight);
final AnimatorSet animate = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.anim.anim_rotate_fade);
AnimatorListenerAdapter animatorListener = new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
new Handler().postDelayed(new Runnable() {
#Override public void run() {
animate.start();
}
}, 1000);
}
};
animate.setTarget(starlightImageView);
animate.addListener(animatorListener);
There are a lot of classes you would like to research on, but currently I'm using objectAnimator which is highly flexible. I wouldn't recommend to use Animation or AnimationUtils:
Animation
AnimationUtils
Animator
AnimatorInflater
AnimatorListener
AnimatorListenerAdapter
One need to listen for completion of first animation then re-start the animation in onStopAnimation call back, give a try to this link
Little tweak to #Danufr answer to save resources from loading again.
operator = (ImageView) findViewById(R.id.operator_loading);
final Animation ani = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.finding_operator);
ani.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
operator.startAnimation(ani);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
operator.setAnimation(ani);
I solved this problem using thread.
Button btn = (Button) findViewById(R.id.buttonpush);
final TextView textview = (TextView) findViewById(R.id.hello);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textview.setText("...................");
final Animation animationtest = AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.slide_in_left);
animationtest.setDuration(1000);
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
public void run() {
handler.postDelayed(this, 1500);
textview.startAnimation(animationtest);
}
};
handler.postDelayed(runnable, 500); // start
handler.removeCallbacks(runnable); //STOP Timer
}
});
it's working fine
GifDrawable gifDrawable = (GifDrawable) gifImageView.getDrawable();
gifDrawable.setLoopCount(0);
None of the above solutions worked in my case. The solution by Danuofr did work for animation set but when I was doing unit testing, my tests used to get stuck in this infinite loop. Finally, specific to my case, I needed to repeat this animation specific number of times. So, I manually added copies of my animation in anim_rot.xml in a cascaded manner adding the offset value. I know it's bad and won't work for many but it was the only workaround for my case.
anim_rot.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:duration="2000"
android:fromDegrees="20"
android:pivotX="29%"
android:pivotY="50%"
android:toDegrees="-20" />
<rotate
android:duration="2000"
android:fromDegrees="-20"
android:pivotX="29%"
android:pivotY="53%"
android:startOffset="2000"
android:toDegrees="20" />
<rotate
android:startOffset="4000"
android:duration="2000"
android:fromDegrees="20"
android:pivotX="29%"
android:pivotY="56%"
android:toDegrees="-20" />
<rotate
android:duration="2000"
android:fromDegrees="-20"
android:pivotX="29%"
android:pivotY="59%"
android:startOffset="6000"
android:toDegrees="20" />
<rotate
android:startOffset="8000"
android:duration="2000"
android:fromDegrees="20"
android:pivotX="29%"
android:pivotY="62%"
android:toDegrees="-20" />
<rotate
android:duration="2000"
android:fromDegrees="-20"
android:pivotX="29%"
android:pivotY="65%"
android:startOffset="10000"
android:toDegrees="20" />
</set>
I did this to repeat the animation 3 times. You can add more copies to repeat it specific times by adding offset values.
Try to add the code to a looping thread or a while/for statement
I have two TranslateAnimations on a TextView and I want them to execute one after other. However, by using the code below, only the second one is executed.
How can I solve this?
TranslateAnimation animation = new TranslateAnimation(
Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, 0.0f,
Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, -150.0f);
animation.setDuration(200);
wave.startAnimation(animation);
TranslateAnimation animation1 = new TranslateAnimation(
Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, 0.0f,
Animation.ABSOLUTE, 150.0f, Animation.ABSOLUTE, 0.0f);
animation1.setDuration(200);
wave.startAnimation(animation1);
Link them together with
Animation Set
AnimationSet as = new AnimationSet(true)
TranslateAnimation animation = new TranslateAnimation(
Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, 0.0f,
Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, -150.0f);
animation.setDuration(200);
as.addAnimation(animation);
TranslateAnimation animation1 = new TranslateAnimation(
Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, 0.0f,
Animation.ABSOLUTE, 150.0f, Animation.ABSOLUTE, 0.0f);
animation1.setDuration(200);
animation1.setStartOffset(200);
as.addAnimation(animation1);
wave.startAnimation(as);
EDIT: Andy Boots answer below is the better answer imo.
Just set your first one like this and it'll start the other one, once the animation finishes:
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
wave.startAnimation(animation1);
}
});
edit: The reason only your second animation is executed with your current code, is because it overrides the playing of the first animation (both actually are played, but you only see the latest one to start). If you do like I wrote, they will play sequentially instead of in parallel.
also you can do this by XML itself using android:startOffset attribute , and there is an examble:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="300"
android:fromXScale="0%"
android:fromYScale="0%"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="100%"
android:toYScale="100%" />
<alpha
android:duration="300"
android:fromAlpha="0"
android:toAlpha=".5" />
<alpha
android:duration="300"
android:fromAlpha=".5"
android:startOffset="300"
android:toAlpha="1" />
</set>
There is one more approach to reach this goal which can be useful when you need to animate a lot of views one after another. You can use setStartOffset method to set a delay before animation begins. So, if you know, how much time will take for your first animation to end, you can set this as a delay for your second animation. This is an example where I animated six ImageButtons and six TextViews below them one after another:
public void animateButtons() {
// An array of buttons
int[] imageButtonIds = {R.id.searchButton, R.id.favoriteButton, R.id.responseButton, R.id.articleButton, R.id.resumeButton, R.id.subscribeButton};
// Array of textViews
int[] textViewIds = {R.id.searchTextView, R.id.favoriteTextView, R.id.responseTextView, R.id.articleTextView, R.id.resumeTextView, R.id.subscribeTextView};
int i = 1;
for (int viewId : imageButtonIds) {
ImageButton imageButton = (ImageButton) findViewById(viewId);
// Animation from a file fade.xml in folder res/anim
Animation fadeAnimation = AnimationUtils.loadAnimation(this, R.anim.fade);
// Delay for each animation is 100 ms bigger than for previous one
fadeAnimation.setStartOffset(i * 100);
imageButton.startAnimation(fadeAnimation);
// The same animation is for textViews
int textViewId = textViewIds[i-1];
TextView textView = (TextView) findViewById(textViewId);
textView.startAnimation(fadeAnimation);
i ++;
}
}
In my res/anim folder I have a file, called fade.xml with these contents:
<?xml version="1.0" encoding="utf-8"?>
<!-- Fade animation with 500 ms duration -->
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="500" />
Create an animation array and use method for creating AnimationSet.
Animation[] animations = {
getScaleAnimation(0.4f, 1.3f, 2000),
getScaleAnimation(1.3f, 1.0f, 500),
getScaleAnimation(0.4f, 1.3f, 1000),
getScaleAnimation(1.3f, 1.0f, 3000),
getScaleAnimation(0.4f, 1.3f, 500),
getScaleAnimation(1.3f, 1.0f, 1700),
getScaleAnimation(0.4f, 1.3f, 2100),
getScaleAnimation(1.3f, 1.0f, 3400)
};
AnimationSet animationSet = addAnimationAr(animations);
view.startAnimation(animationSet);
Method:
public static AnimationSet addAnimationAr(Animation[] animations) {
AnimationSet animationSet = new AnimationSet(false);
long totalAnimationDuration = 0;
for (int i = 0; i < animations.length; i++) {
Animation a = animations[i];
a.setStartOffset(totalAnimationDuration);
totalAnimationDuration += a.getDuration();
animationSet.addAnimation(a);
}
return animationSet;
}
If you use code, you can call
Animation.setStartOffset()
to delay the second animation.
if you use xml you can android:ordering="sequentially" property to make the two animations perform sequentially.
For simple animations, you can achieve this by using the animate() and withEndAction() functions, like so:
ImageView img = findViewById(R.id.imageView);
img.animate().rotation(180).alpha(0).setDuration(3000).withEndAction(new Runnable() {
#Override
public void run() {
ImageView img = findViewById(R.id.imageView);
img.animate().rotation(0).alpha(1).setDuration(2000);
}
});
AnimationDrawable
Running such animation is pretty straightforward in Android. In fact, there is a class in Android API just for this specific task called AnimationDrwable. AnimationDrawable is a set of images together.
Step1: Create AnimationDrwable
Step2: Load Animation Drawable into ImageView
step3: start the animation
Step 1:
Create an XML and add all images like this.
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="#drawable/cat_image_1" android:duration="200" />
<item android:drawable="#drawable/cat_image_2" android:duration="200" />
<item android:drawable="#drawable/cat_image_3" android:duration="200" />
</animation-list>
Step 2:
public void onCreate(Bundle savedInstanceState) {
//Step 2
ImageView myimage = (ImageView) findViewById(R.id.my_image);
myimage.setBackgroundResource(R.drawable.rocket_thrust);
//Step 3
AnimationDrawable catAnimation = (AnimationDrawable) rocketImage.getBackground();
catAnimation.start();
}