Android 实现滑动翻页---使用ViewFlipper

本文介绍了如何在Android中使用ViewFlipper实现滑动翻页功能。通过设置动画、切换视图的API以及触摸事件监听,实现在同一Activity内切换不同页面的效果。详细步骤包括XML布局、自定义动画和MainActivity的Java代码实现。
摘要由CSDN通过智能技术生成

屏幕切换指的是在同一个Activity内屏幕见的切换,最长见的情况就是在一个FrameLayout内有多个页面,比如一个系统设置页面;一个个性化设置页面。


通过查看OPhone API文档可以发现,有个android.widget.ViewAnimator类继承至FrameLayout,ViewAnimator类的作用是为FrameLayout里面的View切换提供动画效果。该类有如下几个和动画相关的函数:
l setInAnimation:设置View进入屏幕时候使用的动画,该函数有两个版本,一个接受单个参数,类型为android.view.animation.Animation;一个接受两个参数,类型为Context和int,分别为Context对象和定义Animation的resourceID。
  • setOutAnimation: 设置View退出屏幕时候使用的动画,参数setInAnimation函数一样。
  • showNext: 调用该函数来显示FrameLayout里面的下一个View。
  • showPrevious: 调用该函数来显示FrameLayout里面的上一个View。
一般不直接使用ViewAnimator而是使用它的两个子类ViewFlipper和ViewSwitcher。ViewFlipper可以用来指定FrameLayout内多个View之间的切换效果,可以一次指定也可以每次切换的时候都指定单独的效果。该类额外提供了如下几个函数:
  • isFlipping: 用来判断View切换是否正在进行
  • setFilpInterval:设置View之间切换的时间间隔
  • startFlipping:使用上面设置的时间间隔来开始切换所有的View,切换会循环进行
  • stopFlipping: 停止View切换
ViewSwitcher 顾名思义Switcher特指在两个View之间切换。可以通过该类指定一个ViewSwitcher.ViewFactory 工程类来创建这两个View。该类也具有两个子类ImageSwitcher、TextSwitcher分别用于图片和文本切换。
在教程中通过示例介绍ViewFlipper 的使用,其他的使用方式是类似的。详细信息可以参考文档:

 

现在我就来写一个使用ViewFlipper来实现页面切换的功能       新建一个项目,如下图:


step1:设计程序的UI界面    main.xml

<span style="font-size:12px;"><?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:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/viewFipper">
<!-- 第1页 -->
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="center">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="第1页"
android:textSize="60dip" android:gravity="center"/>
</LinearLayout>


<!-- 第2页 -->
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#FFFFFF"
android:gravity="center">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="第2页"
android:textSize="60dip" android:gravity="center"/>
</LinearLayout>


<!-- 第3页 -->
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#00FF00"
android:gravity="center">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="第3页"
android:textSize="60dip" android:gravity="center"/>
</LinearLayout>


<!-- 第4页 -->
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#FF0000"
android:gravity="center">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="第4页"
android:textSize="60dip" android:gravity="center"/>
</LinearLayout>


<!-- 第5页 -->
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#FFFF00"
android:gravity="center">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="第5页"
android:textSize="60dip" android:gravity="center"/>
</LinearLayout>


</ViewFlipper>
</LinearLayout>
</span>

说明:

  • 使用RelativeLayout做父容器,添加按钮在ViewFlipper顶部;
  • ViewFlipper的每个子页面并列于该View内;
  • ViewFlipper的每个子页面显示一个TextView;
step2:设置好页面切换的时候的动画效果,在切换动画/res/anim/目录下的动画文件

enter_left_to_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
<translate
android:fromXDelta="-100%p"
android:toXDelta="0"
android:duration="500"
/>
<alpha
android:duration="500"
android:fromAlpha="1.0"
android:toAlpha="0.1"
/>
</set>

enter_right_to_left.xmlenter_right_to_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
<translate
android:fromXDelta="100%p"
android:toXDelta="0"
android:duration="500"
/>
<alpha
android:duration="500"
android:fromAlpha="0.1"
android:toAlpha="1.0"
/>
</set>

exit_left_to_right.xmlexit_left_to_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
<translate
android:fromXDelta="0"
android:toXDelta="100%p"
android:duration="500"
/>
<alpha
android:duration="500"
android:fromAlpha="1.0"
android:toAlpha="0.1"
/>
</set>

exit_right_to_left.xmlexit_right_to_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate android:fromXDelta="0" android:toXDelta="-100%p"
android:duration="500" />
<alpha android:duration="500" android:fromAlpha="0.1"
android:toAlpha="1.0" />
</set>

 step3:MainActivity.java

<span style="font-size:12px;">package cn.roco.animation;


import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ViewFlipper;


public class MainActivity extends Activity {


private ViewFlipper viewFlipper;
/** 用于记录按下屏幕时刻的x坐标 */
private float startX;
/** 用于记录离开屏幕时刻的x坐标 */
private float endX;


private Animation enter_left_to_right;
private Animation exit_left_to_right;


private Animation enter_right_to_left;
private Animation exit_right_to_left;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN); //请求全屏
getWindow().requestFeature(Window.FEATURE_NO_TITLE); //请求没有标题栏


setContentView(R.layout.main);


viewFlipper = (ViewFlipper) this.findViewById(R.id.viewFipper);
/** 从左到右的两个动画 */
enter_left_to_right = AnimationUtils.loadAnimation(this,
R.anim.enter_left_to_right);
exit_left_to_right = AnimationUtils.loadAnimation(this,
R.anim.exit_left_to_right);
/** 从右到左的两个动画 */
enter_right_to_left = AnimationUtils.loadAnimation(this,
R.anim.enter_right_to_left);
exit_right_to_left = AnimationUtils.loadAnimation(this,
R.anim.exit_right_to_left);
}


@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
startX = event.getX();
break;
}
case MotionEvent.ACTION_UP: {
endX = event.getX();
if (endX > startX) {// 表示向右滑动
showNextView();
} else if (endX < startX) {// 表示向左滑动
showPreviousView();
}
break;
}
}
return super.onTouchEvent(event);
}


/**
* 显示前一页
*/
private void showPreviousView() {
// 动画效果
viewFlipper.setInAnimation(enter_right_to_left);
viewFlipper.setOutAnimation(exit_right_to_left);
viewFlipper.showPrevious();
}



/**
* 显示下一页
*/
private void showNextView() {
// 动画效果
viewFlipper.setInAnimation(enter_left_to_right);
viewFlipper.setOutAnimation(exit_left_to_right);
viewFlipper.showNext();
}
}</span>

 step4:AndroidManifest.xml

<span style="font-size:12px;"><?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.roco.animation" android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest></span>

 step5:运行效果



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值