您的位置:首页 > 移动开发 > Android开发

Android 轻量级轮播组件 CycleViewPager

2016-03-26 12:09 561 查看
最近抽空将项目中最常用的一个轮播组件做了简单的封装,支持无限轮播、设置各种属性。可以略微提高开发效率。

代码:https://github.com/heshiweij/CycleViewPager



特点

CycleViewPager 是一个轻量级的无限轮播图展示控件

支持自由选择加载网络图片和本地图片

优化了 bitmap 的显示,防止 oom

三级缓存优化网络图片,节省流量

支持各种属性的设置,可定制

Activity 生命周期联动

使用线程池请求网络

使用方法

权限

<uses-permission  android:name="android.permission.INTERNET"/>
<uses-permission  android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission  android:name="android.permission.READ_EXTERNAL_STORAGE"/>


布局

<cn.ifavor.cycleviewpager.view.CycleViewPager
android:id="@+id/cvp_main"
android:layout_width="match_parent"
android:layout_height="150dip" />


代码

设置图片 ResId 方式

mCycleViewPager = (CycleViewPager) findViewById(R.id.cvp_main);

Map<String, Integer> map = new TreeMap<String, Integer>();
LinkedHashMap<String, Integer> map = new LinkedHashMap<String, Integer>();
map.put("1", R.drawable.a);
map.put("2", R.drawable.b);
map.put("3", R.drawable.c);
map.put("4", R.drawable.d);
map.put("5", R.drawable.e);

mCycleViewPager.setResIdMap(map).setDuration(1000).start();


设置图片 URL 方式

mCycleViewPager = (CycleViewPager) findViewById(R.id.cvp_main);

LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
map.put("1", "http://www.2cto.com/meinv/uploads/131124/1-1311242143022C.jpg");
map.put("2", "http://www.2cto.com/meinv/uploads/131124/1-131124214242c7.jpg");
map.put("3", "http://www.2cto.com/meinv/uploads/131124/1-131124214135a6.jpg");
map.put("4", "http://www.2cto.com/meinv/uploads/131124/1-1311242141135E.jpg");
map.put("5", "http://www.2cto.com/meinv/uploads/131124/1-13112421404R17.jpg");

mCycleViewPager.setURLMap(map).setDuration(1000).start();


方法汇总

方法含义建议/默认
setBottomBackgroundColor设置底部背景颜色Color.argb(54, 0, 0, 0)
setIndicatorRadius设置指示器的圆角值8(px)
setUnSelectedColor设置指示器未选中的颜色Color.rgb(61, 59, 59)
setSelectedColor设置指示器选中的颜色Color.rgb(255, 0, 0)
setDuration设置轮播器执行时间间隔3000(ms)
setIndicatorDirection设置指示器显示位置IndicatorDirection.CENTER
setIndicatorPointSize设置指示器点的尺寸16(px)
setIndicatorPointMargin设置指示器间隔距离8(px)
setShowTitle设置是否显示标题true
setTitleTextSize设置标题字体大小12(px)
setTitleTextColor设置标题字体颜色Color.rgb(255,0,0)
setHandler设置 handler建议传入全局唯一 handler,否则新建
注意

建议使用全局 handler,如:

setHandler(BaseApplication.getHanlder())

建议在 onDestory 取消轮播,如:

@Override
protected void onDestroy() {
super.onDestroy();
if (mCycleViewPager != null){
// 取消轮播定时器
mCycleViewPager.cancel();
}
}


附录

代码:https://github.com/heshiweij/CycleViewPager
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: