android的ViewFlipper

简介:

activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
     xmlns:tools= "http://schemas.android.com/tools"
     android:layout_width= "match_parent"
     android:layout_height= "match_parent"
     android:orientation= "vertical"
    >
     
     
     
     <ViewFlipper 
         android:id= "@+id/viewFlipper_1"
         android:layout_width= "match_parent"
         android:layout_height= "match_parent"
         >
         
         
         
          <LinearLayout 
         android:id= "@+id/linearLayout_1_1"
         android:layout_width= "fill_parent"
         android:layout_height= "fill_parent"
         >
         <ImageView 
             android:id= "@+id/imageView_1_1"
             android:layout_width= "wrap_content"
             android:layout_height= "wrap_content"
             android:src= "@drawable/kobe0"
             />
     </LinearLayout>
     <LinearLayout
         android:id= "@+id/linearayout_2_2" 
         android:layout_width= "fill_parent"
         android:layout_height= "fill_parent"
         
         >
           <ImageView 
             android:id= "@+id/imageView_2_2"
             android:layout_width= "wrap_content"
             android:layout_height= "wrap_content"
             android:src= "@drawable/kobe1"
             />
     </LinearLayout>
      <LinearLayout
         android:id= "@+id/linearayout_3_3" 
         android:layout_width= "fill_parent"
         android:layout_height= "fill_parent"
         
         >
           <ImageView 
             android:id= "@+id/imageView_3_3"
             android:layout_width= "wrap_content"
             android:layout_height= "wrap_content"
             android:src= "@drawable/kobe3"
             />
     </LinearLayout>
      <LinearLayout
         android:id= "@+id/linearayout_4_4" 
         android:layout_width= "fill_parent"
         android:layout_height= "fill_parent"
         
         >
           <ImageView 
             android:id= "@+id/imageView_4_4"
             android:layout_width= "wrap_content"
             android:layout_height= "wrap_content"
             android:src= "@drawable/kobe4"
             />
     </LinearLayout>
     
     </ViewFlipper>
    
    
 
</LinearLayout>

res/anim/in_leftright.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
<?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"
         >
     
 
     </translate>
     
</set>

res/anim/in_rightleft.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
<?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"
         >
     
 
     </translate>
     
</set>

res/anim/out_leftright.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
<?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"
         >
     
 
     </translate>
     
</set>

res/anim/out_rightleft.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
<?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"
         >
     
 
     </translate>
     
</set>

MainActivity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package  com.example.viewflipper;
 
import  android.R.integer;
import  android.app.Activity;
import  android.os.Bundle;
import  android.view.Menu;
import  android.view.MenuItem;
import  android.view.MotionEvent;
import  android.widget.ViewFlipper;
 
public  class  MainActivity  extends  Activity {
     private  ViewFlipper viewFilpper;
 
     @Override
     protected  void  onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         viewFilpper=(ViewFlipper) findViewById(R.id.viewFlipper_1);
     }
     //重写触屏事件监听方法
     float  startx= 0 .0F;
     float  endx= 0 .0F;
     @Override
     public  boolean  onTouchEvent(MotionEvent event) {
         // TODO Auto-generated method stub
         int  action=event.getAction();
         switch  (action) {
         case  MotionEvent.ACTION_DOWN:
             startx=event.getX();
             break ;
         case  MotionEvent.ACTION_UP:
             //向右滑动
             if (event.getX()-startx> 10 ){
                 viewFilpper.setInAnimation( this , R.anim.in_leftright);
                 viewFilpper.setOutAnimation( this , R.anim.out_leftright);
                 viewFilpper.showNext();
             } else  if (startx-event.getX()> 10 ){
                 viewFilpper.setInAnimation( this , R.anim.in_rightleft);
                 viewFilpper.setOutAnimation( this , R.anim.out_rightleft);
                 viewFilpper.showPrevious();
             }
             
             break ;
         default :
             break ;
         }
         return  super .onTouchEvent(event);
     }
}

wKiom1hRVeHiJjNTABC_NhHUwJU342.gif-wh_50



 本文转自 matengbing 51CTO博客,原文链接:http://blog.51cto.com/matengbing/1882840

相关文章
|
开发工具 Android开发 数据格式
|
Android开发
Android零基础入门第56节:翻转视图ViewFlipper打造引导页和轮播图
原文:Android零基础入门第56节:翻转视图ViewFlipper打造引导页和轮播图    前面两期学习了 ViewAnimator及其子类ViewSwitcher的使用,以及ViewSwitcher的子类ImageSwitcher和TextSwitcher的使用,你都掌握了吗?本期我们一起来学习ViewAnimator另一个子类 ViewFlipper组件的使用。
1669 0
|
XML Android开发 数据格式
我的Android进阶之旅------&gt;Android使用ViewFlipper实现滑动翻页
屏幕切换指的是在同一个Activity内屏幕见的切换,最长见的情况就是在一个FrameLayout内有多个页面,比如一个系统设置页面;一个个性化设置页面。 通过查看OPhone API文档可以发现,有个android.widget.ViewAnimator类继承至FrameLayout,ViewAnimator类的作用是为FrameLayout里面的View切换提供动画效果。
1098 0