效果图:
使用方法:
AutoScrollViewPager viewPager = (AutoScrollViewPager) findViewById(R.id.vp_photos);
viewPager.setDataList(mUrlList);
viewPager.updateView(0);
viewPager.setIsAutoScroll(true);
viewPager.setIsShowDot(true);
步骤一:在value的attrs.xml文件里添加自定义样式
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="AutoScrollViewPager">
<attr name="autoscroll" format="boolean" />
<attr name="show_navgation_dot" format="boolean" />
</declare-styleable>
</resources>
步骤二:
添加 Android-Universal-Image-Loader Jar包(当然也可以用其他的加载网络图片工具)
步骤三:自定义view
/**
* Created by pengkv on 15/12/5.
*/
public class AutoScrollViewPager extends FrameLayout implements View.OnClickListener {
private Context mContext;
private List<String> mDataList;//图片地址集合
private int mBofPosition;
private boolean isShowDot;//是否显示小点
private boolean isAutoScroll;//是否自动滚动
private List<View> mViewList;
private ViewPager mViewPager;
private LinearLayout mDotLayout;//放置小点的容器
private MyPagerAdapter mPagerAdapter;
private ViewPagerRollHelper mRollHelper;//控制滚动的工具类
private ViewPager.LayoutParams mImageParams;//图片的布局参数
private LinearLayout.LayoutParams mDotParams;//小点的布局参数
private FrameLayout.LayoutParams mViewPagerParams;//ViewPager的布局参数
private FrameLayout.LayoutParams mDotLayoutParams;//放置小点容器的布局参数
public AutoScrollViewPager(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public AutoScrollViewPager(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
TypedArray array = mContext.obtainStyledAttributes(attrs, R.styleable.AutoScrollViewPager, defStyleAttr, 0);
isAutoScroll = array.getBoolean(R.styleable.AutoScrollViewPager_autoscroll, true);
isShowDot = array.getBoolean(R.styleable.AutoScrollViewPager_show_navgation_dot, true);
array.recycle();
}
public void setIsAutoScroll(boolean isAutoScroll) {
this.isAutoScroll = isAutoScroll;
}
public void setIsShowDot(boolean isShowDot) {
this.isShowDot = isShowDot;
}
public void setDataList(List<String> mDataList) {
this.mDataList = mDataList;
initView();
}
private void initView() {
mViewList = new ArrayList<>();
//初始化图片布局参数
mImageParams = new ViewPager.LayoutParams();
mImageParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
mImageParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
//初始化小点布局参数
int size = mContext.getResources().getDimensionPixelSize(R.dimen.five_dp);
mDotParams = new LinearLayout.LayoutParams(size, size);
mDotParams.setMargins(5, 0, 5, 0);
//初始化ViewPager布局参数
mViewPagerParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
//初始化ViewPager
mPagerAdapter = new MyPagerAdapter(mViewList);
mViewPager = new ViewPager(mContext);
mViewPager.setLayoutParams(mViewPagerParams);
mViewPager.setAdapter(mPagerAdapter);
mViewPager.addOnPageChangeListener(new MyPagerOnScrollChangeListener());
//加载ViewPager
addView(mViewPager);
if (isShowDot) {
int height = mContext.getResources().getDimensionPixelOffset(R.dimen.twenty_dp);
mDotLayoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, height);
mDotLayoutParams.gravity = Gravity.BOTTOM | Gravity.CENTER;
mDotLayout = new LinearLayout(mContext);
mDotLayout.setLayoutParams(mDotLayoutParams);
mDotLayout.setOrientation(LinearLayout.HORIZONTAL);
mDotLayout.setGravity(Gravity.CENTER);
addView(mDotLayout);
}
}
public void updateView(int position) {
if (null != mDotLayout) {
mDotLayout.removeAllViews();
}
if (null != mViewList) {
int length = mDataList.size();
int viewLength = mViewList.size();
for (int i = 0; i < length; i++) {
String imgUrl = mDataList.get(i);
ImageView view;
if (i < viewLength) {//取以前的View
view = (ImageView) mViewList.get(i);
} else {//新增View
view = new ImageView(mContext);
view.setLayoutParams(mImageParams);
mViewList.add(view);
}
view.setScaleType(ImageView.ScaleType.CENTER_CROP);
view.setOnClickListener(this);
//初始化加载图片框架
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(mContext)
.threadPriority(Thread.NORM_PRIORITY - 2)// 线程优先级
.denyCacheImageMultipleSizesInMemory()
.tasksProcessingOrder(QueueProcessingType.LIFO)
.memoryCache(new LruMemoryCache(3 * 1024 * 1024))
.memoryCacheSize(3 * 1024 * 1024)
.build();
ImageLoader.getInstance().init(config);
//下载图片--》这里可以替换成自己想要的加载图片工具
ImageLoader.getInstance().displayImage(imgUrl, view);
//添加小点视图
if (isShowDot) {
ImageView dot = new ImageView(mContext);
dot.setImageResource(R.drawable.dot);
dot.setLayoutParams(mDotParams);
if (i == 0)
dot.setSelected(true);
mDotLayout.addView(dot);
}
}
//控制ViewPager的滚动
setCurrentItem(position);
mPagerAdapter.notifyDataSetChanged();
mRollHelper = new ViewPagerRollHelper(mViewPager, mPagerAdapter);
mRollHelper.setAutoScroll(isAutoScroll);
mRollHelper.autoScroll();
}
}
public void setCurrentItem(int position) {
mViewPager.setCurrentItem(position);
}
public int getCurrentItem() {
return mViewPager.getCurrentItem();
}
@Override
public void onClick(View v) {
}
//ViewPager适配器:加载图片
private class MyPagerAdapter extends PagerAdapter {
private List<View> viewList;
public MyPagerAdapter(List<View> viewList) {
this.viewList = viewList;
}
@Override
public int getCount() {
return viewList.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(viewList.get(position));
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View view = viewList.get(position);
container.addView(view);
return view;
}
}
//页面滚动监听器:主要是为了控制小点的显示
private class MyPagerOnScrollChangeListener implements ViewPager.OnPageChangeListener {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
mRollHelper.setPagerChangeEvent(position);
if (isShowDot) {
View view = mDotLayout.getChildAt(position);
View befView = mDotLayout.getChildAt(mBofPosition);
view.setSelected(true);
befView.setSelected(false);
mBofPosition = position;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
}
}
步骤四:添加辅助类
/**
* Created by pengkv on 15/12/5.
*/
public class ViewPagerRollHelper {
private ViewPager mViewPager = null;
private PagerAdapter mPagerAdapter = null;
private int newPosition, oldPosition, maxPosition;
private boolean isAutoScroll = true;
private static final int CHANGE_ADS = 0;//切换的参数
private Timer mTimer = null;
private MyTimertask myTimertask = null;
public ViewPagerRollHelper(ViewPager viewPager, PagerAdapter pagerAdapter) {
mViewPager = viewPager;
mPagerAdapter = pagerAdapter;
}
//控制ViewPager的当前位置
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case CHANGE_ADS:
newPosition = (Integer) msg.obj;
mViewPager.setCurrentItem(newPosition);
break;
}
}
};
//自动滚动
public void autoScroll() {
//获取页面个数
maxPosition = mPagerAdapter.getCount() - 1;
if (isAutoScroll && maxPosition > 0) {
if (mTimer != null) {
mTimer.cancel();
mTimer = null;
}
mTimer = new Timer();
if (myTimertask != null) {
myTimertask.cancel();
myTimertask = null;
}
if (null == myTimertask)
myTimertask = new MyTimertask();
mTimer.schedule(myTimertask, 3000, 3000);
}
}
//设置页面变更事件
public void setPagerChangeEvent(int curIndex) {
newPosition = curIndex;
oldPosition = newPosition - 1;
autoScroll();
}
//发送位置变更信息
class MyTimertask extends TimerTask {
@Override
public void run() {
if (newPosition >= 0 && newPosition < maxPosition) {//自动翻下一页
newPosition += 1;
} else if (newPosition == maxPosition) {//在最后一页
newPosition = 0;
}
Message message = handler.obtainMessage();
message.what = CHANGE_ADS;
message.obj = newPosition;
handler.sendMessage(message);
}
}
public void setAutoScroll(boolean isAutoScroll) {
this.isAutoScroll = isAutoScroll;
}
}
时间: 2024-10-07 22:38:43