1、屏幕切换指的是在同一个Activity内屏幕见的切换,最长见的情况就是在一个FrameLayout内有多个页面,比如一个系统设置页面;一个个性化设置页面。
2、介绍ViewFilpper类
ViewFlipper
Class Overview
Simple ViewAnimator
that will animate between two or more views that have been added to it. Only one child
is shown at a time. If requested, can automatically flip between each child at a regular interval.
意思是:简单的ViewAnimator之间,两个或两个以上的view加上动画效果。只有一个小孩会显示在一个时间。如果需要,每个孩子能自动翻转之间在固定的时间间隔。
该类继承了Framelayout类,ViewAnimator类的作用是为FrameLayout里面的View切换提供动画效果。
该类有如下几个和动画相关的函数:
setInAnimation:设置View进入屏幕时候使用的动画,该函数有两个版本,一个接受单个参数,类型为android.view.animation.Animation;一个接受两个参数,类型为Context和int,分别为Context对象和定义Animation的resourceID。
setOutAnimation: 设置View退出屏幕时候使用的动画,参数setInAnimation函数一样。
showNext: 调用该函数来显示FrameLayout里面的下一个View。
showPrevious: 调用该函数来显示FrameLayout里面的上一个View。
3、首选看一下定义四个动画的xml文件:
in_leftright.xml——从左到右进入屏幕
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="3000"
android:fromXDelta="-100%p"
android:toXDelta="0" />
</set>
out_leftright.xml——从左到右出去屏幕
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="3000"
android:fromXDelta="0"
android:toXDelta="100%p" />
</set>
in_rightleft.xml——从右到左进入屏幕
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="3000"
android:fromXDelta="100%p"
android:toXDelta="0" />
</set>
out_rightleft.xml——从右到左出去屏幕
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="3000"
android:fromXDelta="100%p"
android:toXDelta="0" />
</set>
4、定义main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<ViewFlipper android:id="@+id/viewFlipper"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<!-- 第一个页面 -->
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="center">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/a1" />
</LinearLayout>
<!-- 第二个页面 -->
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="center">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/a2"
android:gravity="center" />
</LinearLayout>
<!-- 第三个页面 -->
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="center">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/a3"
android:gravity="center" />
</LinearLayout>
<!-- 第四个页面 -->
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="center">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/a4"
android:gravity="center" />
</LinearLayout>
</ViewFlipper>
</LinearLayout>
5、java代码实现:
public class SwitchTest2Activity extends Activity {
ViewFlipper viewFlipper = null;
float startX;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init() {
viewFlipper = (ViewFlipper) this.findViewById(R.id.viewFlipper);
}
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_leftright);
viewFlipper.setOutAnimation(this, R.anim.out_leftright);
viewFlipper.showNext();
} else if (event.getX() < startX) { // 向左滑动
viewFlipper.setInAnimation(this, R.anim.in_rightleft);
viewFlipper.setOutAnimation(this, R.anim.out_rightleft);
viewFlipper.showPrevious();
}
break;
}
return super.onTouchEvent(event);
}
}
6、效果图:
在这里看不出效果图,我贴几张图片吧!!
分享到:
相关推荐
android 利用ViewFlipper来实现滑动切换
Android使用ViewFlipper实现Switch。开关部分通过自定义控件继承ViewFlipper来实现,开关动画通过anim文件来实现。
利用viwflipper来将多个activity封装在一个view中,通过实现OnGestureListener接口的onTouchEvent()方法和onFling() 方法,即可通过手持滑动切换activity。
Android使用ViewFlipper实现左右滑动效果面.
Android系统自带有一个多页面管理的控件:ViewFlipper. 它可以简单实现子页面的切换,,, 它只需使用addView方法添加几个View,每个View对应的是一个页面,即可完成对于多页面的管理,,, 在android上实现手势...
android中的ViewFlipper实现左右切换源代码。UI设计。
Android ActivtiyGroup和ViewFlipper实现Activity滑屏切换,绝对好用!
主要介绍了Android中利用viewflipper动画切换屏幕效果的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
ViewFlipper,不妨把它看做一个容器吧,你可以把许多的View放在这个容器中,让它展示给...这个demo是一个手动滑动屏幕查看图片的粒子,共有10张图片,左右滑动可以查看图片,效果不是很好。其实用ViewPager会更好些。
Android 滑动效果ViewFlipper Android 滑动效果ViewFlipper
利用ViewFlipper实现图片的轮播效果
Android应用源码之Gallery+ImageSwitcher+ViewFlipper实现手机查看壁纸效果.zip
ViewFlipper实现滑屏切换View
NULL 博文链接:https://topmanopensource.iteye.com/blog/1596067
使用ViewFlipper实现屏幕切换动画效果
主要为大家详细介绍了Android使用ViewFlipper实现图片切换功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
使用ViewFlipper控件以及anim动画文件实现屏幕切换效果。
Android Gallery+ImageSwitcher+ViewFlipper实现手机查看壁纸效果.zip