职业IT人-IT人生活圈

 找回密码
 成为会员
搜索
查看: 1798|回复: 1

玩转Android--UI篇--ViewFlipper(实现切换屏幕效果)

[复制链接]
broken 发表于 2011-9-16 10:54 | 显示全部楼层 |阅读模式
ViewFlipper是ViewAnimator的子类,在ViewAnimator子类下,还有一个ViewSwitcher,这个ViewSwitcher又有两个子类:TextSwitcher和ImageSwitcher。不过今天还是说说ViewFlipper。

这个控件可以实现屏幕上下、左右的切换效果,而且可以加上动画特效,当然你可以点击就可以切换了,而用手指划屏其实还是点击操作(只针对这个控件)。该控件每次只能显示一屏

公共方法
public bool isAutoStart ()
  如果视图显示到窗口上时会自动调用 startFlipping()方法 ,则返回 true

public bool isFlipping()
如果子视图正在切换,则返回 true

public bool setAutoStart (bool autoStart)
设置视图显示到窗口上时是否会自动调用 startFlipping()方法

public bool setFlipInterval (int milliseconds)
视图间切换的时间间隔
参数
                    milliseconds    毫秒数

public bool startFlipping ()
开始在子视图间定时循环切换

public bool stopFlipping ()
   停止切换

下面实现一个小例子来看看这个控件到底是怎么用的
main.xml
Xml代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <ViewFlipper   
  8.     android:layout_height="wrap_content"  
  9.     android:layout_width="match_parent"  
  10.     android:id="@+id/viewFipper01">  
  11.     <include android:id="@+id/layout1" layout="@layout/layout1"/>  
  12.     <include android:id="@+id/layout2" layout="@layout/layout2"/>  
  13. </ViewFlipper>  
  14. </LinearLayout>  

  15. <?xml version="1.0" encoding="utf-8"?>
  16. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  17.     android:orientation="vertical"
  18.     android:layout_width="fill_parent"
  19.     android:layout_height="fill_parent"
  20.     >
  21. <ViewFlipper
  22.         android:layout_height="wrap_content"
  23.         android:layout_width="match_parent"
  24.         android:id="@+id/viewFipper01">
  25.         <include android:id="@+id/layout1" layout="@layout/layout1"/>
  26.         <include android:id="@+id/layout2" layout="@layout/layout2"/>
  27. </ViewFlipper>
  28. </LinearLayout>
复制代码
这里用到了include标签,学过java web的同学很容易理解,这和jsp页面里的include没啥区别。使用include我们可以把一个大的布局文件拆成多个小的文件,使用include,我们可以复用一些常用布局组合
layout1.xml
Xml代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:orientation="vertical"  
  5.   android:layout_width="match_parent"  
  6.   android:layout_height="match_parent">  
  7.     <ImageView  
  8.         android:layout_height="wrap_content"  
  9.         android:layout_width="fill_parent"  
  10.         android:src="@drawable/ni_png_0120"  
  11.     />  
  12.     <TextView  
  13.         android:layout_height="wrap_content"  
  14.         android:layout_width="fill_parent"  
  15.         android:gravity="center"  
  16.         android:text="乌贼"  
  17.         android:textSize="20pt"  
  18.     />  
  19. </LinearLayout>  

  20. <?xml version="1.0" encoding="utf-8"?>
  21. <LinearLayout
  22.   xmlns:android="http://schemas.android.com/apk/res/android"
  23.   android:orientation="vertical"
  24.   android:layout_width="match_parent"
  25.   android:layout_height="match_parent">
  26.     <ImageView
  27.             android:layout_height="wrap_content"
  28.             android:layout_width="fill_parent"
  29.             android:src="@drawable/ni_png_0120"
  30.     />
  31.     <TextView
  32.             android:layout_height="wrap_content"
  33.             android:layout_width="fill_parent"
  34.             android:gravity="center"
  35.             android:text="乌贼"
  36.             android:textSize="20pt"
  37.     />
  38. </LinearLayout>
复制代码
layout2.xml
Xml代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:orientation="vertical"  
  5.   android:layout_width="match_parent"  
  6.   android:layout_height="match_parent">  
  7.     <ImageView  
  8.         android:layout_height="wrap_content"  
  9.         android:layout_width="fill_parent"  
  10.         android:src="@drawable/ni_png_0115"  
  11.     />  
  12.     <TextView   
  13.         android:layout_height="wrap_content"   
  14.         android:layout_width="fill_parent"   
  15.         android:gravity="center"   
  16.         android:text="鱼"   
  17.         android:textSize="20pt">  
  18.     </TextView>  
  19. </LinearLayout>  

  20. <?xml version="1.0" encoding="utf-8"?>
  21. <LinearLayout
  22.   xmlns:android="http://schemas.android.com/apk/res/android"
  23.   android:orientation="vertical"
  24.   android:layout_width="match_parent"
  25.   android:layout_height="match_parent">
  26.     <ImageView
  27.             android:layout_height="wrap_content"
  28.             android:layout_width="fill_parent"
  29.             android:src="@drawable/ni_png_0115"
  30.     />
  31.     <TextView
  32.             android:layout_height="wrap_content"
  33.             android:layout_width="fill_parent"
  34.             android:gravity="center"
  35.             android:text="鱼"
  36.             android:textSize="20pt">
  37.         </TextView>
  38. </LinearLayout>
复制代码
ViewTestActivity.java
Java代码
  1. package com.view.ViewFlippertest;   
  2.   
  3. import android.app.Activity;   
  4. import android.os.Bundle;   
  5. import android.view.View;   
  6. import android.view.animation.AnimationUtils;   
  7. import android.widget.ViewFlipper;   
  8.   
  9. public class ViewTestActivity extends Activity {   
  10.     /** Called when the activity is first created. */  
  11.     @Override  
  12.     public void onCreate(Bundle savedInstanceState) {   
  13.         super.onCreate(savedInstanceState);   
  14.         setContentView(R.layout.main);   
  15.         final ViewFlipper vf = (ViewFlipper)findViewById(R.id.viewFipper01);   
  16.         vf.setOnClickListener(new View.OnClickListener() {   
  17.                
  18.             @Override  
  19.             public void onClick(View v) {   
  20.                 //点击所在区域即可滑动到下一屏幕   
  21.                 vf.showNext();   
  22.             }   
  23.         });   
  24.         //动画切入,从左边进入   
  25.         vf.setInAnimation(AnimationUtils.loadAnimation(getApplicationContext(),   
  26.                 android.R.anim.slide_in_left));   
  27.         //动画切出,从右边离开   
  28.         vf.setOutAnimation(AnimationUtils.loadAnimation(getApplicationContext(),   
  29.                 android.R.anim.slide_out_right));   
  30.     }   
  31. }  

  32. package com.view.ViewFlippertest;

  33. import android.app.Activity;
  34. import android.os.Bundle;
  35. import android.view.View;
  36. import android.view.animation.AnimationUtils;
  37. import android.widget.ViewFlipper;

  38. public class ViewTestActivity extends Activity {
  39.     /** Called when the activity is first created. */
  40.     @Override
  41.     public void onCreate(Bundle savedInstanceState) {
  42.         super.onCreate(savedInstanceState);
  43.         setContentView(R.layout.main);
  44.         final ViewFlipper vf = (ViewFlipper)findViewById(R.id.viewFipper01);
  45.         vf.setOnClickListener(new View.OnClickListener() {
  46.                        
  47.                         @Override
  48.                         public void onClick(View v) {
  49.                                 //点击所在区域即可滑动到下一屏幕
  50.                                 vf.showNext();
  51.                         }
  52.                 });
  53.         //动画切入,从左边进入
  54.         vf.setInAnimation(AnimationUtils.loadAnimation(getApplicationContext(),
  55.                         android.R.anim.slide_in_left));
  56.         //动画切出,从右边离开
  57.         vf.setOutAnimation(AnimationUtils.loadAnimation(getApplicationContext(),
  58.                         android.R.anim.slide_out_right));
  59.     }
  60. }  
复制代码
运行效果如下:

f5a53b0b-18ff-30a7-b4c6-a8059f0d58af.png

12be1b4b-00fc-3216-8beb-82f98604aa37.png

公共方法说明参考:http://www.cnblogs.com/over140/archive/2010/12/06/1897439.html
fl 发表于 2011-9-16 10:54 | 显示全部楼层
只能从左边进入, 从右边离开 ,不能左右双向划?
您需要登录后才可以回帖 登录 | 成为会员

本版积分规则

QQ|手机版|小黑屋|网站帮助|职业IT人-IT人生活圈 ( 粤ICP备12053935号-1 )|网站地图
本站文章版权归原发布者及原出处所有。内容为作者个人观点,并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是信息平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽造成漏登,请及时联系我们,我们将根据著作权人的要求立即更正或者删除有关内容。

GMT+8, 2024-4-19 10:27 , Processed in 0.167502 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表