Android的图片轮播和滑动

用ViewFipper来实现 

MainActivity

 1 package viewflipper.example.administrator.viewfipper;
 2 
 3 import android.app.Activity;
 4 import android.support.v7.app.AppCompatActivity;
 5 import android.os.Bundle;
 6 import android.view.MotionEvent;
 7 import android.widget.ImageView;
 8 import android.widget.ViewFlipper;
 9 
10 public class MainActivity extends Activity {
11     private ViewFlipper viewFlipper;
12     int[] res={R.drawable.pic1,R.drawable.pic2,R.drawable.pic3,R.drawable.pic4};
13     private float X;
14     @Override
15     protected void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         setContentView(R.layout.main);
18         viewFlipper= (ViewFlipper) findViewById(R.id.flipper);
19         for (int i=0;i<res.length;i++){
20             viewFlipper.addView(getImage(res[i]));
21         }
22 //        viewFlipper.setInAnimation(this,R.anim.left_in);/*设置动画从左边进*/
23 //        viewFlipper.setOutAnimation(this,R.anim.left_out);
24 //        viewFlipper.setFlipInterval(3000);/*设置轮播时间*/
25 //       viewFlipper.startFlipping();
26 
27     }
28 
29     public ImageView getImage(int res){
30         ImageView imageView=new ImageView(this);
31         imageView.setBackgroundResource(res);/*设置图片全屏*/
32 //        imageView.setImageResource(res);
33         return imageView;
34     }
35 
36     @Override
37     public boolean onTouchEvent(MotionEvent motionEvent) {
38 
39         switch (motionEvent.getAction()) {
40 //            手指落下
41             case MotionEvent.ACTION_DOWN:{
42                 X = motionEvent.getX();
43                 break;}
44 //            手指滑动
45             case MotionEvent.ACTION_UP: {/*向左滑动,向右滑动如果写在ACTION_MOVE,图片会重叠,因为会被多次满足*/
46 //                向右滑动
47                 if (motionEvent.getX() - X > 100) {
48                     viewFlipper.setInAnimation(this, R.anim.left_in);
49                     viewFlipper.setOutAnimation(this, R.anim.left_out);
50                     viewFlipper.showPrevious();
51                 }
52                 if (X - motionEvent.getX() > 100) {
53                     viewFlipper.setInAnimation(this, R.anim.right_in);
54                     viewFlipper.setOutAnimation(this, R.anim.right_out);
55                     viewFlipper.showNext();
56                 }
57                 break;
58             }
59 //            手指离开
60             case MotionEvent.ACTION_MOVE:/*不要写成ACTION_HOVER_MOVE*/
61                 break;
62         }
63 
64         return super.onTouchEvent(motionEvent);
65     }
66 }

main.xml

1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3     android:orientation="vertical" android:layout_width="match_parent"
4     android:layout_height="match_parent">
5 <ViewFlipper
6     android:layout_width="match_parent"
7     android:layout_height="match_parent"
8     android:id="@+id/flipper"></ViewFlipper>
9 </LinearLayout>

anim中left_in.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <set xmlns:android="http://schemas.android.com/apk/res/android" >
 3 
 4     <translate
 5         android:duration="2000"
 6         android:fromXDelta="-100%p"
 7         android:toXDelta="0" />
 8     <alpha 
 9         android:fromAlpha="0.5"
10         android:toAlpha="1"
11         android:duration="2000"
12         />
13 
14 </set>

right_in.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <set xmlns:android="http://schemas.android.com/apk/res/android" >
 3 
 4     <translate
 5         android:duration="2000"
 6         android:fromXDelta="100%p"
 7         android:toXDelta="0" />
 8 
 9     <alpha
10         android:duration="2000"
11         android:fromAlpha="0.5"
12         android:toAlpha="1" />
13 
14 </set>

left_out.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <set xmlns:android="http://schemas.android.com/apk/res/android" >
 3 
 4     <translate
 5         android:duration="2000"
 6         android:fromXDelta="0"
 7         android:toXDelta="100%p" />
 8 
 9     <alpha
10         android:duration="2000"
11         android:fromAlpha="0.5"
12         android:toAlpha="1" />
13 
14 </set>

right_out.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <set xmlns:android="http://schemas.android.com/apk/res/android" >
 3 
 4     <translate
 5         android:duration="2000"
 6         android:fromXDelta="0"
 7         android:toXDelta="-100%p" />
 8 
 9     <alpha
10         android:duration="2000"
11         android:fromAlpha="0.5"
12         android:toAlpha="1" />
13 
14 </set>

 

posted @ 2016-05-08 22:52  成功人土  阅读(4402)  评论(0编辑  收藏  举报