Android使用ViewFlipper实现左右滑动效果面

在我的博客中,上次是使用ViewPager实现左右滑动的效果的,请看文章:Android使用ViewPager实现左右滑动效果 

 

这次我来使用ViewFlipper实现这种效果,好了,先看看效果吧:

 

 

 

 

效果看完了就来实现这个效果。

1.布局文件

主界面使用下面的布局:

[html] view plain copy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <ViewFlipper android:id="@+id/ViewFlipper1"  
  8.         android:layout_width="fill_parent"   
  9.         android:layout_height="fill_parent">  
  10.     </ViewFlipper>  
  11.       
  12.       
  13.       
  14.     <LinearLayout  
  15.         android:orientation="horizontal"  
  16.         android:layout_width="wrap_content"  
  17.           
  18.         android:layout_gravity="bottom|center_horizontal"  
  19.         android:layout_height="wrap_content"  
  20.           
  21.         >  
  22.         <ImageView  
  23.             android:layout_width="wrap_content"  
  24.             android:layout_height="wrap_content"  
  25.             android:src="@drawable/da"  
  26.             
  27.             android:id="@+id/imageview1"  
  28.             />  
  29.         <ImageView  
  30.             android:layout_width="wrap_content"  
  31.             android:layout_height="wrap_content"  
  32.             android:src="@drawable/xiao"  
  33.             android:id="@+id/imageview2"  
  34.             />  
  35.         <ImageView  
  36.             android:layout_width="wrap_content"  
  37.             android:layout_height="wrap_content"  
  38.             android:src="@drawable/xiao"  
  39.             android:id="@+id/imageview3"  
  40.             />  
  41.         <ImageView  
  42.             android:layout_width="wrap_content"  
  43.             android:layout_height="wrap_content"  
  44.             android:src="@drawable/xiao"  
  45.             android:id="@+id/imageview4"  
  46.             />  
  47.               
  48.         </LinearLayout>  
  49.   
  50.   
  51. </FrameLayout>  


简单的介绍一下布局文件:最外层是一个FrameLayout,使用FrameLayout就是为了是的下面的四个点在ViewFlipper上面。LayoutLayout在FrameLayout的下面和水平居中。

2.ViewFlipper的使用

[java] view plain copy
 
  1. flipper = (ViewFlipper) this.findViewById(R.id.ViewFlipper1);  
  2.         flipper.addView(addImageView(R.drawable.png1o));  
  3.         flipper.addView(addImageView(R.drawable.png2o));  
  4.         flipper.addView(addImageView(R.drawable.png3o));  
  5.   
  6.         flipper.addView(addView());  


 

在Activity中声明一个ViewFlipper的对象,在布局中找到。将三张图片加到ViewFlipper中,另外再加一个View,这个View是从XML布局文件中得到的。布局文件如下:

[html] view plain copy
 
  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.     >  
  8. <Button  
  9.         android:id="@+id/button"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_marginBottom="45dp"        
  13.         android:text="进入程序"  
  14.         android:textColor="#3E3E3E"   
  15.         android:layout_gravity="center_horizontal"  
  16.         />  
  17. </LinearLayout>   


在这个布局文件中有一个Button,用于跳转Activity用。

 

在Activity中声明一个GestureDetector对象,在onCreate方法中分配内存。

detector = new GestureDetector(this);

使用this为参数,那么就要使得activity类impllements  OnGestureListener接口。重写几个方法。覆盖父类的onTouchEvent方法,在这个方法中如下写:

[java] view plain copy
 
  1. @Override  
  2.     public boolean onTouchEvent(MotionEvent event) {  
  3.         // TODO Auto-generated method stub  
  4.         return this.detector.onTouchEvent(event);   
  5.     }  


这样就使得detector能接受消息响应了。

 

在实现OnGestureListener的方法中判断用户的滑动来切换界面:

[java] view plain copy
 
  1. @Override  
  2. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
  3.         float velocityY) {  
  4.     System.out.println("in------------>>>>>>>");  
  5.     if (e1.getX() - e2.getX() > 120) {  
  6.         if (i < 3) {  
  7.             i++;  
  8.             setImage(i);  
  9.             this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,  
  10.                     R.anim.animation_right_in));  
  11.             this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,  
  12.                     R.anim.animation_left_out));  
  13.             this.flipper.showNext();  
  14.         }  
  15.         return true;  
  16.     }   
  17.     else if (e1.getX() - e2.getX() < -120) {  
  18.         if (i > 0) {  
  19.             i--;  
  20.             setImage(i);  
  21.             this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,  
  22.                     R.anim.animation_left_in));  
  23.             this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,  
  24.                     R.anim.animation_right_out));  
  25.             this.flipper.showPrevious();  
  26.         }  
  27.         return true;  
  28.     }  
  29.     return false;  
  30.       
  31. }  
  32.   
  33. void setImage(int i)  
  34. {  
  35.     for(int j=0;j<4;j++)  
  36.     {  
  37.         if(j!=i)  
  38.         iamges[j].setImageResource(R.drawable.xiao);  
  39.         else  
  40.             iamges[j].setImageResource(R.drawable.da);  
  41.     }  
  42. }  


界面切换的时候改变下面的四个小ImageView的图片。切换的动画在res/anim文件夹中,这里就不多说了。

 

整个工程的下载:Android使用ViewFlipper实现左右滑动效果面。。

posted @ 2016-12-06 11:59  天涯海角路  阅读(132)  评论(0)    收藏  举报