Reputation: 1
I'm trying to animate image starting bellow screen, going up, and then back down (doing app for Galaxy tab). Both of these animations work separately, but when I try to create AnimationSet, I can't get them to work. I even tried creating 2 AsyncTasks, and call 2nd animation in onPostExecute of the 1st AsyncTask, but still won't work.
this is my XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/rel_layout"
>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ad200"
android:id="@+id/image"
/>
</RelativeLayout>
And this is my code I'm trying to use to animate image:
AnimationSet set = new AnimationSet(true);
Animation anim1 = new TranslateAnimation(0, 0, 1024, 824);
anim1.setDuration(3000);
anim1.setFillAfter(true);
set.addAnimation(anim1);
Animation anim2 = new TranslateAnimation(0, 0, 824, 1024);
anim2.setDuration(3000);
anim2.setFillAfter(true);
set.addAnimation(anim2);
imageView.clearAnimation();
set.setFillAfter(true);
imageView.startAnimation(set);
Upvotes: 0
Views: 3825
Reputation: 589
All of the above answers are too complicated and can be written in simpler manner.
Kotlin solution looks like this:
val animation: Animation = TranslateAnimation(0F, 0F, 0F, -100F)
animation.duration = 3000
animation.fillAfter = true
animation.repeatCount = Animation.INFINITE
animation.repeatMode = Animation.REVERSE
Main variables set here are:
animation.repeatCount = Animation.INFINITE
animation.repeatMode = Animation.REVERSE
I'm guessing that you want to animate something to go up and down (or left and right) all the time, so you can use animation.repeatCount = Animation.INFINITE
and if you want the animation to return to the previous position instead of going from up to down, then resetting its position, use animation.repeatMode = Animation.REVERSE
Upvotes: 0
Reputation: 1852
Here it is a solution...try this below code.. i use this code so many times .. and it works fine..======>
Animation zoomin =new TranslateAnimation(1, 1, 0, -50);
zoomin.setDuration(1000);
zoomin.setFillEnabled(true);
zoomin.setFillAfter(true);
Animation zoomout = new TranslateAnimation(1, 1, -50, 0);
zoomout.setDuration(1000);
zoomout.setFillEnabled(true);
zoomout.setFillAfter(true);
imageView.startAnimation(zoomin);
zoomin.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation arg0) {
imageView.startAnimation(zoomout);
}
});
zoomout.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation arg0) {
imageView.startAnimation(zoomin);
}
});
Upvotes: 0
Reputation: 101
Well WendiKidd,
You are very close to your solution. You have just missed the Animation.AnimationListener() in your Code.
Animation anim1 = new TranslateAnimation(0, 0, 1024, 824);
anim1.setDuration(3000);
anim1.setFillAfter(true);
anim1.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
Animation anim2 = new TranslateAnimation(0, 0, 824, 1024);
anim2.setDuration(3000);
anim2.setFillAfter(true);
imageView.clearAnimation();
imageView.startAnimation(anim2);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
imageView.startAnimation(anim1);
The sample above will start anim2 after anim1 ends.
Upvotes: 1
Reputation: 4784
Looks like you are starting both animations at the same time. Add anim2.setStartOffset(3000) to your second animation. That will cause the second animation to start 3000 ms after the first one.
BR, Christoffer
Upvotes: 0