屏幕切换指的是在同一个Activity内屏幕见的切换,最长见的情况就是在一个FrameLayout内有多个页面,比如一个系统设置页面;一个个性化设置页面。
现在我就来写一个使用ViewFlipper来实现页面切换的功能 新建一个项目,如下图:
step1:设计程序的UI界面 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: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>
说明:
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
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();
}
}
step4:AndroidManifest.xml
<?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>
step5:运行效果
附注:通过手势来切换
package cn.roco.gesture; import android.app.Activity; import android.os.Bundle; import android.view.GestureDetector; import android.view.GestureDetector.OnGestureListener; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.view.MotionEvent; import android.widget.ViewFlipper; /** * 通过手势切换图片 */ public class GestureFlipActivity extends Activity implements OnGestureListener{ private ViewFlipper viewFlipper; //定义手势检测器实例 GestureDetector detector; //定义动画 private Animation enter_left_to_right; private Animation exit_left_to_right; private Animation enter_right_to_left; private Animation exit_right_to_left; //定义手势动作两点之间的最小距离 final int FLIP_DISTANCE=50; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //创建手势检测器 detector=new GestureDetector(this); //获得ViewFlipper实例 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 onDown(MotionEvent e) { return false; } @Override public void onShowPress(MotionEvent e) { } @Override public boolean onSingleTapUp(MotionEvent e) { return false; } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { return false; } @Override public void onLongPress(MotionEvent e) { } @Override public boolean onTouchEvent(MotionEvent event) { // 将该Activity上的触碰时间交个 GestureDetector处理 return detector.onTouchEvent(event); } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { /** * 如果第1个触点事件的x坐标大于第2个触点事件的x坐标超过 FLIP_DISTANCE * 也就是手势从右向左滑动 */ if (e1.getX()-e2.getX()>FLIP_DISTANCE) { showPreviousView(); return true; } /** * 如果第2个触点事件的x坐标大于第1个触点事件的x坐标超过 FLIP_DISTANCE * 也就是手势从左向右滑动 */ else if (e2.getX()-e1.getX()>FLIP_DISTANCE) { showNextView(); return true; } return false; } /** * 显示前一页 */ 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(); } }
==================================================================================================
作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!
转载请保留原文地址:http://blog.csdn.net/ouyang_peng
==================================================================================================