android中无限循环滑动的gallery实例
1.点击图片有变暗的效果,使用imageview.setAlpha(),并且添加ontouchListener
public void init() {
dots = new ImageView[len];
for (int j = 0; j < len; j++) {
// 添加图片
final ImageView iv = new ImageView(this);
iv.setImageResource(res[j]);
views.add(iv);
iv.setOnTouchListener(new OnTouchListener() { @Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
iv.setAlpha(70);
slideHandler.removeCallbacks(slideRun);
}
return false;
}
});
// 添加索引
ImageView imageView = new ImageView(this);
imageView.setLayoutParams(new LayoutParams(15, 15));
dots[j] = imageView;
TextView tv = new TextView(this);
tv.setText(" ");
if (j == 0) {
// 默认进入程序后第一张图片被选中;
dots[j].setBackgroundResource(R.drawable.yst_i_pageon);
} else {
dots[j].setBackgroundResource(R.drawable.yst_i_pageoff);
} layout_point.addView(tv);
layout_point.addView(imageView);
}
gallery.setAdapter(new ImageAdapter());
gallery.setSelection((Integer.MAX_VALUE / 2) - (Integer.MAX_VALUE / 2)
% len);
gallery.setOnItemSelectedListener(selectListener);
((MyGallery) gallery).setViews(views);
// TODO 定时滑动
startSlide();
}
2.无限滑动,自定义adapter,设置count为最大值,图片总数循环从0到图片的总长度
public class ImageAdapter extends BaseAdapter { public int getCount() {
if (views != null) {
return Integer.MAX_VALUE;
} else {
return 0;
}
} public Object getItem(int position) {
if (position >= len) {
position = position % len;
} return position;
} public long getItemId(int position) {
if (position >= len) {
position = position % len;
}
return position;
} public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (position >= len) {
position = position % len;
}
if (view == null) {
view = views.get(position);
} return view;
}
}
3.自动滑动,自定义gallery,重写onFling方法,触摸时,停止滑动,弹起时继续滑动
public class MyGallery extends Gallery { private List<ImageView> views;
/** 左边 */
public static final int LEFT = 0;
/** 右边 */
public static final int RIGHT = 1; public MyGallery(Context context) {
super(context);
// TODO Auto-generated constructor stub
} public MyGallery(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
} public MyGallery(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
} /** 设置视图 */
public void setViews(List<ImageView> views) {
this.views = views;
} @Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_UP) {
// 遍历views 设置alpha
if (views != null && !views.isEmpty()) {
for (ImageView iv : views) {
iv.setAlpha(255);
}
}
Context context = this.getContext();
if (context != null && context instanceof MainActivity) {
MainActivity main=(MainActivity) context;
main.startSlide();
}
}
return super.onTouchEvent(event);
} /** 滑动 direction=1向右,-1向左 */
public void slide(int direction) {
// MotionEvent e1 = MotionEvent.obtain(SystemClock.uptimeMillis(),
// SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN,
// 89.333336f, 265.33334f, 0);
// MotionEvent e2 = MotionEvent.obtain(SystemClock.uptimeMillis(),
// SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, 300.0f,
// 238.00003f, 0);
if (direction == this.RIGHT)
this.onFling(null, null, -1200, 0);
else {
this.onFling(null, null, 1200, 0);
}
}
}
下面贴出完整代码:
布局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.完整的实现代码:
package com.allen.gallery; import java.util.ArrayList;
import java.util.List; import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView; public class MainActivity extends Activity {
private List<ImageView> views = new ArrayList<ImageView>();
/** 图片资源 */
private int[] res = new int[] { R.drawable.image1, R.drawable.image2,
R.drawable.image3, R.drawable.image4 };
private ImageView[] dots;
int len = 4;
int currentItem = 0;// 当前选中的viewPager的item
private Gallery gallery;
private LinearLayout layout_point;
/** 定时滑动 */
private final Handler slideHandler = new Handler();
/** 滑动 */
private final Runnable slideRun = new Runnable() { @Override
public void run() {
// TODO Auto-generated method stub
currentItem++;
currentItem = checkPosition(currentItem);
((MyGallery) gallery).slide(MyGallery.RIGHT);
slideHandler.postDelayed(this, 2000);
}
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化中间
layout_point = (LinearLayout) this.findViewById(R.id.layout_dots);
gallery = (Gallery) findViewById(R.id.mygallery);
gallery.setSpacing(20);
init();
} public void init() {
dots = new ImageView[len];
for (int j = 0; j < len; j++) {
// 添加图片
final ImageView iv = new ImageView(this);
iv.setImageResource(res[j]);
views.add(iv);
iv.setOnTouchListener(new OnTouchListener() { @Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
iv.setAlpha(70);
slideHandler.removeCallbacks(slideRun);
}
return false;
}
});
// 添加索引
ImageView imageView = new ImageView(this);
imageView.setLayoutParams(new LayoutParams(15, 15));
dots[j] = imageView;
TextView tv = new TextView(this);
tv.setText(" ");
if (j == 0) {
// 默认进入程序后第一张图片被选中;
dots[j].setBackgroundResource(R.drawable.yst_i_pageon);
} else {
dots[j].setBackgroundResource(R.drawable.yst_i_pageoff);
} layout_point.addView(tv);
layout_point.addView(imageView);
}
gallery.setAdapter(new ImageAdapter());
gallery.setSelection((Integer.MAX_VALUE / 2) - (Integer.MAX_VALUE / 2)
% len);
gallery.setOnItemSelectedListener(selectListener);
((MyGallery) gallery).setViews(views);
// TODO 定时滑动
startSlide();
} private AdapterView.OnItemSelectedListener selectListener = new AdapterView.OnItemSelectedListener() { @Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
if (position >= len) {
position = position % len;
currentItem = position;
}
selectPage();
} @Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
}; public class ImageAdapter extends BaseAdapter { public int getCount() {
if (views != null) {
return Integer.MAX_VALUE;
} else {
return 0;
}
} public Object getItem(int position) {
if (position >= len) {
position = position % len;
} return position;
} public long getItemId(int position) {
if (position >= len) {
position = position % len;
}
return position;
} public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (position >= len) {
position = position % len;
}
if (view == null) {
view = views.get(position);
} return view;
}
} /** 开始滑动 */
public void startSlide() {
slideHandler.postDelayed(slideRun, 2000);
} public int checkPosition(int position) {
if (position >= len) {
position = position % len;
} return position;
} /** 设置当前选中页 */
private void selectPage() {
/** 设置当前显示的页码 */
for (int i = 0; i < len; i++) {
dots[currentItem].setBackgroundResource(R.drawable.yst_i_pageon);
if (currentItem != i) {
dots[i].setBackgroundResource(R.drawable.yst_i_pageoff);
}
}
}
}
点击我
下载就不用了吧。。。
© Copyright by allen