android中无限循环滑动的gallery实例

android中无限循环滑动的gallery实例

1.点击图片有变暗的效果,使用imageview.setAlpha(),并且添加ontouchListener

 1 public void init() {
 2         dots = new ImageView[len];
 3         for (int j = 0; j < len; j++) {
 4             // 添加图片
 5             final ImageView iv = new ImageView(this);
 6             iv.setImageResource(res[j]);
 7             views.add(iv);
 8             iv.setOnTouchListener(new OnTouchListener() {
 9 
10                 @Override
11                 public boolean onTouch(View v, MotionEvent event) {
12                     if (event.getAction() == MotionEvent.ACTION_DOWN) {
13                         iv.setAlpha(70);
14                         slideHandler.removeCallbacks(slideRun);
15                     }
16                     return false;
17                 }
18             });
19             // 添加索引
20             ImageView imageView = new ImageView(this);
21             imageView.setLayoutParams(new LayoutParams(15, 15));
22             dots[j] = imageView;
23             TextView tv = new TextView(this);
24             tv.setText("  ");
25             if (j == 0) {
26                 // 默认进入程序后第一张图片被选中;
27                 dots[j].setBackgroundResource(R.drawable.yst_i_pageon);
28             } else {
29                 dots[j].setBackgroundResource(R.drawable.yst_i_pageoff);
30             }
31 
32             layout_point.addView(tv);
33             layout_point.addView(imageView);
34         }
35         gallery.setAdapter(new ImageAdapter());
36         gallery.setSelection((Integer.MAX_VALUE / 2) - (Integer.MAX_VALUE / 2)
37                 % len);
38         gallery.setOnItemSelectedListener(selectListener);
39         ((MyGallery) gallery).setViews(views);
40         // TODO 定时滑动
41         startSlide();
42     }

2.无限滑动,自定义adapter,设置count为最大值,图片总数循环从0到图片的总长度

 1 public class ImageAdapter extends BaseAdapter {
 2 
 3         public int getCount() {
 4             if (views != null) {
 5                 return Integer.MAX_VALUE;
 6             } else {
 7                 return 0;
 8             }
 9         }
10 
11         public Object getItem(int position) {
12             if (position >= len) {
13                 position = position % len;
14             }
15 
16             return position;
17         }
18 
19         public long getItemId(int position) {
20             if (position >= len) {
21                 position = position % len;
22             }
23             return position;
24         }
25 
26         public View getView(int position, View convertView, ViewGroup parent) {
27             View view = convertView;
28             if (position >= len) {
29                 position = position % len;
30             }
31             if (view == null) {
32                 view = views.get(position);
33             }
34 
35             return view;
36         }
37     }
View Code

3.自动滑动,自定义gallery,重写onFling方法,触摸时,停止滑动,弹起时继续滑动

 1 public class MyGallery extends Gallery {
 2 
 3     private List<ImageView> views;
 4     /** 左边 */
 5     public static final int LEFT = 0;
 6     /** 右边 */
 7     public static final int RIGHT = 1;
 8 
 9     public MyGallery(Context context) {
10         super(context);
11         // TODO Auto-generated constructor stub
12     }
13 
14     public MyGallery(Context context, AttributeSet attrs, int defStyle) {
15         super(context, attrs, defStyle);
16         // TODO Auto-generated constructor stub
17     }
18 
19     public MyGallery(Context context, AttributeSet attrs) {
20         super(context, attrs);
21         // TODO Auto-generated constructor stub
22     }
23 
24     /** 设置视图 */
25     public void setViews(List<ImageView> views) {
26         this.views = views;
27     }
28 
29     @Override
30     public boolean onTouchEvent(MotionEvent event) {
31         // TODO Auto-generated method stub
32         if (event.getAction() == MotionEvent.ACTION_UP) {
33             // 遍历views 设置alpha
34             if (views != null && !views.isEmpty()) {
35                 for (ImageView iv : views) {
36                     iv.setAlpha(255);
37                 }
38             }
39             Context context = this.getContext();
40             if (context != null && context instanceof MainActivity) {
41                    MainActivity main=(MainActivity) context;
42                    main.startSlide();
43             }
44         }
45         return super.onTouchEvent(event);
46     }
47 
48     /** 滑动 direction=1向右,-1向左 */
49     public void slide(int direction) {
50         // MotionEvent e1 = MotionEvent.obtain(SystemClock.uptimeMillis(),
51         // SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN,
52         // 89.333336f, 265.33334f, 0);
53         // MotionEvent e2 = MotionEvent.obtain(SystemClock.uptimeMillis(),
54         // SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, 300.0f,
55         // 238.00003f, 0);
56         if (direction == this.RIGHT)
57             this.onFling(null, null, -1200, 0);
58         else {
59             this.onFling(null, null, 1200, 0);
60         }
61     }
62 }
View Code

下面贴出完整代码:
布局xml:

<RelativeLayout 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:background="@android:color/background_light"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:orientation="vertical" >

        <com.allen.gallery.MyGallery
            android:id="@+id/mygallery"
            android:layout_width="fill_parent"
            android:layout_height="160dp"
            android:fadingEdge="horizontal|vertical"
            android:fadingEdgeLength="20dp" />

        <LinearLayout
            android:id="@+id/layout_dots"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="horizontal" >
        </LinearLayout>
    </LinearLayout>

</RelativeLayout>

2.完整的实现代码:

  1 package com.allen.gallery;
  2 
  3 import java.util.ArrayList;
  4 import java.util.List;
  5 
  6 import android.app.Activity;
  7 import android.os.Bundle;
  8 import android.os.Handler;
  9 import android.view.MotionEvent;
 10 import android.view.View;
 11 import android.view.View.OnTouchListener;
 12 import android.view.ViewGroup;
 13 import android.view.ViewGroup.LayoutParams;
 14 import android.widget.AdapterView;
 15 import android.widget.BaseAdapter;
 16 import android.widget.Gallery;
 17 import android.widget.ImageView;
 18 import android.widget.LinearLayout;
 19 import android.widget.TextView;
 20 
 21 public class MainActivity extends Activity {
 22     private List<ImageView> views = new ArrayList<ImageView>();
 23     /** 图片资源 */
 24     private int[] res = new int[] { R.drawable.image1, R.drawable.image2,
 25             R.drawable.image3, R.drawable.image4 };
 26     private ImageView[] dots;
 27     int len = 4;
 28     int currentItem = 0;// 当前选中的viewPager的item
 29     private Gallery gallery;
 30     private LinearLayout layout_point;
 31     /** 定时滑动 */
 32     private final Handler slideHandler = new Handler();
 33     /** 滑动 */
 34     private final Runnable slideRun = new Runnable() {
 35 
 36         @Override
 37         public void run() {
 38             // TODO Auto-generated method stub
 39             currentItem++;
 40             currentItem = checkPosition(currentItem);
 41             ((MyGallery) gallery).slide(MyGallery.RIGHT);
 42             slideHandler.postDelayed(this, 2000);
 43         }
 44     };
 45 
 46     @Override
 47     protected void onCreate(Bundle savedInstanceState) {
 48         super.onCreate(savedInstanceState);
 49         setContentView(R.layout.activity_main);
 50         // 初始化中间
 51         layout_point = (LinearLayout) this.findViewById(R.id.layout_dots);
 52         gallery = (Gallery) findViewById(R.id.mygallery);
 53         gallery.setSpacing(20);
 54         init();
 55     }
 56 
 57     public void init() {
 58         dots = new ImageView[len];
 59         for (int j = 0; j < len; j++) {
 60             // 添加图片
 61             final ImageView iv = new ImageView(this);
 62             iv.setImageResource(res[j]);
 63             views.add(iv);
 64             iv.setOnTouchListener(new OnTouchListener() {
 65 
 66                 @Override
 67                 public boolean onTouch(View v, MotionEvent event) {
 68                     if (event.getAction() == MotionEvent.ACTION_DOWN) {
 69                         iv.setAlpha(70);
 70                         slideHandler.removeCallbacks(slideRun);
 71                     }
 72                     return false;
 73                 }
 74             });
 75             // 添加索引
 76             ImageView imageView = new ImageView(this);
 77             imageView.setLayoutParams(new LayoutParams(15, 15));
 78             dots[j] = imageView;
 79             TextView tv = new TextView(this);
 80             tv.setText("  ");
 81             if (j == 0) {
 82                 // 默认进入程序后第一张图片被选中;
 83                 dots[j].setBackgroundResource(R.drawable.yst_i_pageon);
 84             } else {
 85                 dots[j].setBackgroundResource(R.drawable.yst_i_pageoff);
 86             }
 87 
 88             layout_point.addView(tv);
 89             layout_point.addView(imageView);
 90         }
 91         gallery.setAdapter(new ImageAdapter());
 92         gallery.setSelection((Integer.MAX_VALUE / 2) - (Integer.MAX_VALUE / 2)
 93                 % len);
 94         gallery.setOnItemSelectedListener(selectListener);
 95         ((MyGallery) gallery).setViews(views);
 96         // TODO 定时滑动
 97         startSlide();
 98     }
 99 
100     private AdapterView.OnItemSelectedListener selectListener = new AdapterView.OnItemSelectedListener() {
101 
102         @Override
103         public void onItemSelected(AdapterView<?> parent, View view,
104                 int position, long id) {
105             // TODO Auto-generated method stub
106             if (position >= len) {
107                 position = position % len;
108                 currentItem = position;
109             }
110             selectPage();
111         }
112 
113         @Override
114         public void onNothingSelected(AdapterView<?> parent) {
115             // TODO Auto-generated method stub
116         }
117     };
118 
119     public class ImageAdapter extends BaseAdapter {
120 
121         public int getCount() {
122             if (views != null) {
123                 return Integer.MAX_VALUE;
124             } else {
125                 return 0;
126             }
127         }
128 
129         public Object getItem(int position) {
130             if (position >= len) {
131                 position = position % len;
132             }
133 
134             return position;
135         }
136 
137         public long getItemId(int position) {
138             if (position >= len) {
139                 position = position % len;
140             }
141             return position;
142         }
143 
144         public View getView(int position, View convertView, ViewGroup parent) {
145             View view = convertView;
146             if (position >= len) {
147                 position = position % len;
148             }
149             if (view == null) {
150                 view = views.get(position);
151             }
152 
153             return view;
154         }
155     }
156 
157     /** 开始滑动 */
158     public void startSlide() {
159         slideHandler.postDelayed(slideRun, 2000);
160     }
161 
162     public int checkPosition(int position) {
163         if (position >= len) {
164             position = position % len;
165         }
166 
167         return position;
168     }
169 
170     /** 设置当前选中页 */
171     private void selectPage() {
172         /** 设置当前显示的页码 */
173         for (int i = 0; i < len; i++) {
174             dots[currentItem].setBackgroundResource(R.drawable.yst_i_pageon);
175             if (currentItem != i) {
176                 dots[i].setBackgroundResource(R.drawable.yst_i_pageoff);
177             }
178         }
179     }
180 }
点击我

下载就不用了吧。。。

© Copyright by allen

posted @ 2013-07-03 10:42  流星剑  阅读(11800)  评论(1编辑  收藏  举报