以编程方式为ViewSwitcher设置动画

3
我将动画设置在布局中,如下所示:

我在布局中设置了动画:

<ViewSwitcher
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:inAnimation="@android:anim/slide_in_left"
    android:outAnimation="@android:anim/slide_out_right" >

我该如何以编程的方式完成同样的操作?
3个回答

10
请阅读 ViewSwitcher 类的文档,它有两个可用于设置进入/退出动画的方法。
// load the two animations  
Animation animIn = AnimationUtils.loadAnimation(context, android.R.anim.slide_in_left);
Animation animOut = AnimationUtils.loadAnimation(context, android.R.anim.slide_out_right);
// set them on the ViewSwitcher
vs.setInAnimation(animIn);
vs.setOutAnimation(animOut);

1
viewswitcher.setInAnimation(AnimationUtils.loadAnimation(context, R.anim.fade_in));
viewswitcher.setOutAnimation(AnimationUtils.loadAnimation(context, R.anim.fade_out));

0

您可以跳过使用AnimationUtils加载动画的步骤,而是直接传递动画资源:

switcher.setInAnimation(this, android.R.anim.slide_in_left);
switcher.setOutAnimation(this, android.R.anim.slide_out_right);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接