我在布局中设置动画是这样的:

<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" >

我怎样才能以编程方式做同样的事情?

有帮助吗?

解决方案

请阅读有关文件 ViewSwitcher 类,它有两种设置in/out动画的方法:

// 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);

其他提示

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

可以跳过加载AnimationUtils的动画的步骤,然后直接传递Anim资源:

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top