ViewSwitcher,可以在两个View之间切换,并且可以带一些简单的切换动画,它只能有两个子View。
可以用来当splashscreen,在显示图片的时候加载数据,数据加载完后显示另一个View。
xml文件
<ViewSwitcher
android:id="@+id/container"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:inAnimation="@android:anim/slide_in_left"
android:outAnimation="@android:anim/slide_out_right"
tools:context=".ViewSwitcher" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#66ccff"
android:text="@string/hello_world" />
</LinearLayout>
</ViewSwitcher>
activity文件
private android.widget.ViewSwitcher vs = null;
private int resid[] = {R.drawable.ic_1, R.drawable.ic_2, R.drawable.ic_3, R.drawable.ic_4, R.drawable.ic_5};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_switcher);
vs = (android.widget.ViewSwitcher)findViewById(R.id.container);
vs.getChildAt(0).setBackgroundResource(resid[(int) (Math.random()*4)]);
mHandler.sendEmptyMessageDelayed(1, 1000);
}
Handler mHandler = new Handler(){
public void handleMessage(Message msg){
vs.showNext();
}
};
之前使用runnable来延迟切换,却没反应,一直在第一个子View。不知道是不是和其他线程里不能更新UI有关系,但是后来用了handler,把sendMessage放在单独线程里,还是不能切换,不知道什么原因,网上资料还没翻着。
参考资料:Android 中文API (61) —— ViewSwitcher
http://www.cnblogs.com/over140/archive/2011/09/03/2121526.html
http://blog.youkuaiyun.com/hitlion2008/article/details/7969016#
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1112/545.html