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

来源:互联网 发布:ps读取复合数据怎么办 编辑:程序博客网 时间:2024/04/30 10:18

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                 @Override11                 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

0 0
原创粉丝点击