先上效果图
本文在https://github.com/Trinea/android-auto-scroll-view-pager的基础上,又添加了标记点和点击事件,并对滑动过程中的自动播放进行了控制。
下边是主要代码:
package com.fjl.viewpagerautoplay;
import java.util.LinkedList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.fjl.widget.AutoScrollViewPager;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
public class MainActivity extends Activity {
@ViewInject(R.id.poster_pager)
private AutoScrollViewPager mPosterPager;
@ViewInject(R.id.points)
private LinearLayout pointsLayout;
private Context context;
private BitmapUtils bitmapUtils = null;
private int index = 0;
/**
* 广告图片
*/
private List<String> posterImage = null;
/**
* 标记点集合
*/
private List<ImageView> points = null;
/**
* 广告个数
*/
private int count = 4;
/**
* 循环间隔
*/
private int interval = 4000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
initPoints();
initPoster();
}
private void init() {
ViewUtils.inject(this);
context = this;
points = new LinkedList<ImageView>();
posterImage = new LinkedList<String>();
bitmapUtils = new BitmapUtils(context, getCacheDir().getPath(), 1024, 1024);
posterImage
.add("http://c.hiphotos.baidu.com/image/h%3D800%3Bcrop%3D0%2C0%2C1280%2C800/sign=4d9580fd7b310a55db24d3f4877e20db/43a7d933c895d14317e8fe2d71f082025aaf0725.jpg");
posterImage
.add("http://f.hiphotos.baidu.com/image/h%3D800%3Bcrop%3D0%2C0%2C1280%2C800/sign=0664044d9045d688bc02bfa494f91e6c/7e3e6709c93d70cffe5e6773fadcd100baa12b2c.jpg");
posterImage
.add("http://c.hiphotos.baidu.com/image/h%3D800%3Bcrop%3D0%2C0%2C1280%2C800/sign=35fce30599504fc2bd5fbd05d5e68463/caef76094b36acaf340ff9f17ed98d1000e99ccb.jpg");
posterImage
.add("http://e.hiphotos.baidu.com/image/h%3D800%3Bcrop%3D0%2C0%2C1280%2C800/sign=35418a60544e9258b9348beeacb9b22a/b58f8c5494eef01f064b7ac1e2fe9925bc317d52.jpg");
}
private void initPoints() {
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(8, 15, 8, 15);
for (int i = 0; i < count; i++) {
// 添加标记点
ImageView point = new ImageView(context);
if (i == index % count) {
point.setBackgroundResource(R.drawable.feature_point_cur);
} else {
point.setBackgroundResource(R.drawable.feature_point);
}
point.setLayoutParams(lp);
points.add(point);
pointsLayout.addView(point);
}
}
private void initPoster() {
// 设置 ViewPager的高度为屏幕宽度的1/5
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, (int) (getScreen(this).widthPixels / 5));
mPosterPager.setLayoutParams(params);
mPosterPager.setAdapter(new PosterPagerAdapter());
mPosterPager.setCurrentItem(count * 500);
mPosterPager.setInterval(interval);
mPosterPager.setOnPageChangeListener(new PosterPageChange());
mPosterPager.setSlideBorderMode(AutoScrollViewPager.SLIDE_BORDER_MODE_CYCLE);
mPosterPager.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mPosterPager.stopAutoScroll();
break;
case MotionEvent.ACTION_MOVE:
mPosterPager.startAutoScroll();
break;
case MotionEvent.ACTION_UP:
mPosterPager.startAutoScroll();
break;
default:
break;
}
return false;
}
});
}
/**
* 获取屏幕宽高
*
* @param activity
* @return
*/
public DisplayMetrics getScreen(Activity activity) {
DisplayMetrics outMetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
return outMetrics;
}
@Override
protected void onResume() {
super.onResume();
mPosterPager.startAutoScroll();
}
@Override
protected void onPause() {
super.onPause();
mPosterPager.stopAutoScroll();
}
class PosterPagerAdapter extends PagerAdapter {
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
imageView.setAdjustViewBounds(true);
// TODO 调整图片大小
imageView.setScaleType(ScaleType.CENTER_CROP);
android.view.ViewGroup.LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(params);
bitmapUtils.display(imageView, posterImage.get(position % count));
((ViewPager) container).addView(imageView);
imageView.setOnClickListener(new PosterClickListener(position % count));
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
}
class PosterPageChange implements OnPageChangeListener {
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageSelected(int position) {
index = position;
for (int i = 0; i < count; i++) {
points.get(i).setBackgroundResource(R.drawable.feature_point);
}
points.get(position % count).setBackgroundResource(R.drawable.feature_point_cur);
}
}
class PosterClickListener implements OnClickListener {
private int position;
public PosterClickListener(int position) {
this.position = position;
}
@Override
public void onClick(View v) {
mPosterPager.stopAutoScroll();
Toast.makeText(context, "position---->" + position, 0).show();
}
}
}
项目下载地址:
http://download.csdn.net/detail/fjliang/7340505