viewFlipper的基本实现

 这个例子实现了在一个activity不同的View之间的切换

activity代码

 

package com.liudan.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.ViewFlipper;

public class PopupWindowDemo extends Activity {
 ViewFlipper viewFlipper;
 float startX;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  viewFlipper = (ViewFlipper) this.findViewById(R.id.viewFlipper);
 }

 /**
  * 处理用户滑屏事件
  */
 @Override
 public boolean onTouchEvent(MotionEvent event) {
  switch (event.getAction()) {
  case MotionEvent.ACTION_DOWN:
   startX = event.getX();
   break;

  case MotionEvent.ACTION_UP:
   if (event.getX() > startX) {
    viewFlipper.setInAnimation(this, R.anim.in_lefttoright);// 定义下一页进来时的动画
    viewFlipper.setOutAnimation(this, R.anim.out_lefttoright);// 定义当前页出去的动画
    viewFlipper.showNext();// 显示下一页
   } else if (event.getX() < startX) {
    viewFlipper.setInAnimation(this, R.anim.in_righttoleft);
    viewFlipper.setOutAnimation(this, R.anim.out_righttoleft);
    viewFlipper.showPrevious();// 显示上一页
   }
   break;
  }
  return super.onTouchEvent(event);
 }

}

 

xml 代码

1.main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 xmlns:android="http://schemas.android.com/apk/res/android">
 <ViewFlipper
  android:id="@+id/viewFlipper"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
 
  <!-- 这是当前页第一页 -->
  <LinearLayout
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content">
   <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="这是第一页"/>
  </LinearLayout>
 
  <!-- 这是第二页 -->
  <LinearLayout
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:background="#FF00FF00">
   <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="这是第二页"/>
  </LinearLayout>
 </ViewFlipper>
</LinearLayout> 

 2.in_lefttoright.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
 <!-- 定义从左向右滑动时,后一页进入的动画 -->
 <translate
 android:fromXDelta="-100%"
 android:toXDelta="0"
 android:duration="3000"/>
</set>

 

3.in_righttoleft.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
 <!-- 定义从右向左滑动时,后一页进入的动画 -->
 <translate
 android:fromXDelta="100%"
 android:toXDelta="0"
 android:duration="3000"/>
</set>

4.out_lefttoright.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%"
 android:duration="3000"/>
</set>

 

5.out_righttoleft.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%"
 android:duration="3000"/>
</set>

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值