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到图片的总长度

  View Code

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

  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.完整的实现代码:

  点击我

下载就不用了吧。。。

© Copyright by allen


http://www.cnblogs.com/Jaylong/archive/2013/07/03/3168984.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值