I am trying to animate the image of a ball entering the screen by moving the position on the screen and returning from the screen. I want to do this in the form of 3 animations; ball_in, ball_shift and ball_out, as well as the ability to decide when to switch from one animation to another.
This is the code that I have received so far;
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="@+id/ballImage" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="5px"
android:src="@drawable/red_ball"
/>
</RelativeLayout>
Primary activity
public class AnimationTest extends Activity
{
AnimationDrawable ballAnimation;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ImageView ballImage = (ImageView) findViewById(R.id.ballImage);
final Animation ballOutAnimation = AnimationUtils.loadAnimation(this, R.anim.ball_out);
final Animation ballShiftAnimation = AnimationUtils.loadAnimation(this, R.anim.ball_shift);
ballShiftAnimation.setAnimationListener( new AnimationListener()
{
@Override
public void onAnimationEnd(
Animation animation) {
ballImage.startAnimation(ballOutAnimation);
}
@Override
public void onAnimationRepeat(
Animation animation) {}
@Override
public void onAnimationStart(
Animation animation) {}
});
final Animation ballInAnimation = AnimationUtils.loadAnimation(this, R.anim.ball_in);
ballInAnimation.setAnimationListener( new AnimationListener()
{
@Override
public void onAnimationEnd(
Animation animation) {
ballImage.startAnimation(ballShiftAnimation);
}
@Override
public void onAnimationRepeat(
Animation animation) {}
@Override
public void onAnimationStart(
Animation animation) {}
});
ballImage.startAnimation(ballInAnimation);
}
}
ball_in.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">
<translate
android:fromXDelta="150"
android:toXDelta="0"
android:fromYDelta="0"
android:toYDelta="0"
android:duration="2000"
android:startOffset="0"
/>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_decelerate_interpolator">
<translate
android:fromXDelta="0"
android:toXDelta="0"
android:fromYDelta="-150"
android:toYDelta="0"
android:duration="1500"
android:startOffset="500"
/>
</set>
</set>
ball_shift.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">
<translate
android:fromXDelta="0"
android:toXDelta="130"
android:fromYDelta="0"
android:toYDelta="220"
android:duration="2000"
android:startOffset="0"
android:fillAfter="true"
/>
<scale
android:fromXScale="1.0"
android:toXScale="0.623"
android:fromYScale="1.0"
android:toYScale="0.623"
android:duration="2000"
android:startOffset="0"
android:fillAfter="true"
/>
</set>
ball_out.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">
<scale
android:fromXScale="0.623"
android:toXScale="0.623"
android:fromYScale="0.623"
android:toYScale="0.623"
/>
<translate
android:fromXDelta="130"
android:toXDelta="330"
android:duration="2000"
android:startOffset="0"
/>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_decelerate_interpolator">
<translate
android:fromYDelta="220"
android:toYDelta="370"
android:duration="1900"
android:startOffset="100"
/>
</set>
</set>
I initially had everything in one long animation, but I wanted to break the animation in order to be able to stop and continue at this point in time in the code. Having done this, I realized that the image position was reset back to its original position when the animation was finished, and this, of course, gave me strange results when splitting animations.
fillAfter/fillBefore, / , , , . - .
, , . .