来自http://developerlife.com/tutorials/?p=343
这是一个从左滑动的动画(在视图宽度上从右向左平移),名为“/res/anim/slide_right.xml”:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator">
<translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="150" />
</set>
这是另一个使用上述动画序列的动画序列(@anim/slide_right.xml -> “/res/anim/slide_right.xml”):
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="10%"
android:order="reverse"
android:animation="@anim/slide_right" />
因此,您可以在 XML 中创建序列并将它们放在您的 Android 项目资源的“/res/anim/some_file.xml”中。您可以在此处获取有关如何创建此 XML 文件的更多详细信息。
您也可以通过代码执行此操作::
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(100);
set.addAnimation(animation);
animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f
);
animation.setDuration(500);
set.addAnimation(animation);
LayoutAnimationController controller =
new LayoutAnimationController(set, 0.25f);
button.setLayoutAnimation(controller);
进而:
public static Animation runSlideAnimationOn(Activity ctx, View target) {
Animation animation = AnimationUtils.loadAnimation(ctx,
android.R.anim.slide_right);
target.startAnimation(animation);
return animation;
}