您的位置:首页 > 移动开发 > Android开发

android 动画(5)ViewSwitcher

2016-06-30 08:32 477 查看
[b]ViewSwitcher 的作用简单来说就是:在两个视图间转换时显示动画 [/b]

[b]它的两个子类应该很熟悉,[/b]

[b]    ImageSwitcher:转换图片时增加动画效果; [/b]

[b]    TextSwitcher: 转换文字时增加动画效果; [/b]

[b]布局:[/b]

<?xml version="1.0" encoding="utf-8"?>
[b]<ViewSwitcher [/b]xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/view_switcher"
android:layout_width="match_parent"
android:inAnimation="@android:anim/slide_in_left"
android:outAnimation="@android:anim/slide_out_right"
android:layout_height="match_parent" >

<!-- 第一个视图 -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/ne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="下一张"/>
</RelativeLayout>

<!-- 第二个视图 -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="上一张"/>
<ImageView
android:id="@+id/image"
android:layout_toRightOf="@id/up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:src="@drawable/ic_launcher"/>
</RelativeLayout>
</ViewSwitcher>


界面的切换

public class ViewSwitcherAct extends Activity{

private ViewSwitcher switcher;
private Button upper,next;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_switcher);
switcher = (ViewSwitcher) findViewById(R.id.view_switcher);
upper = (Button) findViewById(R.id.up);
next = (Button) findViewById(R.id.ne);
//切换到第一个view
switcher.setDisplayedChild(0);

upper.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
switcher.showPrevious();
}
});

next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
switcher.showNext();
}
});
}
}


[b]布局:[/b]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: