android ViewFlipper的使用
来源:互联网 发布:光线cms影视系统 编辑:程序博客网 时间:2024/04/30 07:58
android ViewFlipper的使用?
屏幕切换指的是在同一个Activity内屏幕内的切换,最常见的情况就是在一个FrameLayout内有多个页面,比如一个系统设置页面;一个个性设置页面。
1.通过查看官方API文档可以发现,有个android.widget.ViewAnimator类继承至FrameLayout,ViewAnimator类的作用是为FrameLayout里面的View切换提供动画效果。该类有如下几个和动画相关的函数:
1)setInAnimation:设置View进入屏幕时候使用的动画,该函数有两个版本,一个接受单个参数,类型为android.view.animation; 一个接受两个参数,类型为Context和int,分别为Context对象和定义Animation的resourceID。
×setOutAnimation: 设置View退出屏幕时候使用的动画,参数setInAnimation函数一样。
×showNext:调用该函数来显示FrameLayout里面的下一个View。
×showPrevious:调用该函数来显示FrameLayout里面的上一个View。
2)一般不直接使用ViewAnimator而是使用它的两个子类ViewFlipper和ViewSwitcher。ViewFlipper可以用来指定FrameLayout内多个View之间的切换效果,可以一次指定也可以每次切换的时候都指定单独的效果。该类额外提供了如下几个函数:
×isFlipping:用来判断View切换是否正在进行。
×setFilpInterval:设置View之间切换的时间间隔。
×startFlipping:使用上面设置的时间间隔来开始切换所有的View,切换会循环进行,
×stopFlipping:停止View切换。
3)ViewSwitcher 顾名思义Switcher特指在两个View之间切换。可以通过该类指定一个ViewSwitcher.ViewFactory 工程类来创建这两个View。该类也具有两个子类ImageSwitcher、TextSwitcher分别用于图片和文本切换。
在教程中通过示例介绍ViewFlipper 的使用,其他的使用方式是类似的。详细信息可以参考文档:
http://androidappdocs-staging.appspot.com/reference/android/widget/ViewAnimator.html
2.ViewFlipper示例
ViewFlipper是继承至FrameLayout的,所以它是一个Layout里面可以放置多个View。在示例中定义一个ViewFlipper,里面包含三个ViewGroup作为示例的三个屏幕,每个ViewGroup中包含一个按钮和一张图片,点击按钮则显示下一个屏幕。代码如下
1)(res\layout\main.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ViewFlipper android:id="@+id/details"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:persistentDrawingCache="animation"
android:flipInterval="1000"
android:inAnimation="@layout/push_left_in"
android:outAnimation="@layout/push_left_out"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:text="Next"
android:id="@+id/Button_next1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<ImageView
android:id="@+id/image1"
android:src="@drawable/dell1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ImageView>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:text="Next"
android:id="@+id/Button_next2"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<ImageView
android:id="@+id/image2"
android:src="@drawable/lg"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ImageView>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:text="Next"
android:id="@+id/Button_next3"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<ImageView
android:id="@+id/image3"
android:src="@drawable/lenovo"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ImageView>
</LinearLayout>
</ViewFlipper>
</LinearLayout>
很简单,在Layout定义中指定动画的相关属性就可以了,通过persistentDrawingCache指定缓存策略;flipInterval指定每个View动画之间的时间间隔;inAnimation和outAnimation分别指定View进出使用的动画效果。
2)动画效果定义如下:
a)res\layout\push_left_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="100%p"
android:toXDelta="0"
android:duration="500"/>
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="500" />
</set>
b)res\layout\push_left_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0"
android:toXDelta="-100%p"
android:duration="500"/>
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="500" />
</set>
3)Activity代码如下 TestActivity.java:
public class TestActivity extends Activity {
private ViewFlipper mViewFlipper;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonNext1 = (Button) findViewById(R.id.Button_next1);
mViewFlipper = (ViewFlipper) findViewById(R.id.flipper);
buttonNext1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//在layout中定义的属性,也可以在代码中指定
// mViewFlipper.setInAnimation(getApplicationContext(), R.anim.push_left_in);
// mViewFlipper.setOutAnimation(getApplicationContext(), R.anim.push_left_out);
// mViewFlipper.setPersistentDrawingCache(ViewGroup.PERSISTENT_ALL_CACHES);
// mViewFlipper.setFlipInterval(1000);
mViewFlipper.showNext();
//调用下面的函数将会循环显示mViewFlipper内的所有View。
// mViewFlipper.startFlipping();
}
});
Button buttonNext2 = (Button) findViewById(R.id.Button_next2);
buttonNext2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mViewFlipper.showNext();
}
});
Button buttonNext3 = (Button) findViewById(R.id.Button_next3);
buttonNext3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mViewFlipper.showNext();
}
});
}
}
- android ViewFlipper的使用
- android ViewFlipper的使用
- android ViewFlipper的使用
- android ViewFlipper的使用
- Android ViewFlipper的使用
- android ViewFlipper的使用
- android ViewFlipper的使用
- android ViewFlipper的使用
- android ViewFlipper的使用
- android ViewFlipper的使用
- android ViewFlipper的使用
- android ViewFlipper的使用
- android ViewFlipper的使用
- Android ViewFlipper的使用
- android ViewFlipper的使用
- android ViewFlipper的使用
- android ViewFlipper的使用
- android ViewFlipper的使用
- IOS开发学习基础--------C语言(四)
- Redis使用总结之与Memcached异同
- java 二叉树的遍历
- Spring 依赖注入
- hdu2546饭卡 (01背包)
- android ViewFlipper的使用
- 旋转屏幕导致Activity重建问题的解决办法
- 微信点击图文消息链接 先跳空白页 再跳目标页android
- android view 覆写omMeasure进行measure操作
- Linux常用命令和vi,gdb的使用
- 文件直接转化为16进制
- 线性表合并
- Android Handle调用线程和Thread直接创建线程的区别小结
- a strange lift