Android滚动页面位置指示器:CircleIndicator

本文介绍如何对CircleIndicator开源组件进行二次开发以实现自定义颜色和大小一致的页面指示器。通过修改资源文件和动画设置,实现了不同颜色但大小一致的指示器。



Android滚动页面位置指示器:CircleIndicator

CircleIndicator是github上的一个开源的用于页面滚动时候的位置指示器,指示当前页面在总的页面中的位置和前后位置关系,github上的项目主页是:https://github.com/ongakuer/CircleIndicator

其效果如图所示:


可以看到,选中和未被选中时候的球的大小并不相同,且颜色在CircleIndicator中只有一个颜色,未选中的颜色只是选中时候颜色的半透明。但是在有些设计需求中,设计会把滚动的指示小圆球都要求一样的大小,且在滚动指示位置变化时候,需要用不同的颜色区分。这种情况下,就需要对CircleIndicator进行二次开发以适应自身项目的设计需要。针对这种情况,我对CircleIndicator进行了二次开发,实现的结果如图:



(1) 指示器颜色区分。
首先需要在res/drawable下面定义两个不同的shape资源文件。一个用于当前被选中的那个小球位置。另外一个用于未被选中的位置显示的shape资源。然后修改CircleIndicator.java代码中的部分节:

private int mIndicatorBackgroundResId = R.drawable.red_radius;
    private int mIndicatorUnselectedBackgroundResId = R.drawable.white_radius;

mIndicatorBackgroundResId =
                typedArray.getResourceId(R.styleable.CircleIndicator_ci_drawable,
                        R.drawable.red_radius);
        mIndicatorUnselectedBackgroundResId =
                typedArray.getResourceId(R.styleable.CircleIndicator_ci_drawable_unselected,
                        R.drawable.white_radius);


(2) CircleIndicator圆球一致相同。
CircleIndicator原先项目中之所以会出现选中与未选中的小球大小不一致,是因为CircleIndicator在项目中在位置变化 时候对小圆球进行动画缩放,修改res/animator下的scale_with_alpha.xml文件中关于缩放的设置,简单期间,缩放的值设置成1.0~1.0(即维持原大小不缩放)。

新的CircleIndicator库我已经修改好代码,运行结果如图中底部中间位置的三个不同颜色的小球:


我把心的库重新push到新的github主页,链接地址:https://github.com/zhangphil/CircleIndicator_Phil
使用时候直接作为库引入即可。


附录:
1,《ViewPager实现TabHost动态添加、删除Fragment,用红色小圆球指示当前页面》链接:http://blog.youkuaiyun.com/zhangphil/article/details/43668197
2,《ViewPager动态添加、删除Fragment,且提供红色小圆球指示当前位置》链接:http://blog.youkuaiyun.com/zhangphil/article/details/43671293
3,《ViewPager+Fragment实现Tabhost,动态添加删除Fragment,可供第三方使用的类库》链接:http://blog.youkuaiyun.com/zhangphil/article/details/43700807
4,《ViewPager+Fragment添加一行若干小红球滚动指示器指示当前ViewPager翻阅位置(可供第三方使用的类)》链接:http://blog.youkuaiyun.com/zhangphil/article/details/43732039


一个轻量级的viewpager指示器 ,类似于nexus5 启动器的效果。它可以自定义指示器上小圆点的样式和动画效果。可以用于引导页。项目地址:https://github.com/ongakuer/CircleIndicator 效果图:如何使用1. xml布局中创建CircleIndicator是配合ViewPager使用的,一般如下布局:<RelativeLayout         android:layout_width="match_parent"         android:layout_height="match_parent">         <android.support.v4.view.ViewPager             android:id="@ id/viewpager_default"             android:layout_width="match_parent"             android:layout_height="match_parent"/>         <me.relex.circleindicator.CircleIndicator             android:id="@ id/indicator_default"             android:layout_centerInParent="true"             android:layout_width="fill_parent"             android:layout_height="40dp"/>     </RelativeLayout>2. java代码中// DEFAULT ViewPager defaultViewpager = (ViewPager) findViewById(R.id.viewpager_default); CircleIndicator defaultIndicator = (CircleIndicator) findViewById(R.id.indicator_default); DemoPagerAdapter defaultPagerAdapter = new DemoPagerAdapter(getSupportFragmentManager()); defaultViewpager.setAdapter(defaultPagerAdapter);//为ViewPager设置适配器 defaultIndicator.setViewPager(defaultViewpager);//将ViewPager添加到CircleIndicator中,以便监听ViewPager的滚动等事件DemoPagerAdapter是ViewPager的适配器,这个需要你去实现。属性说明属性名称类型说明ci_widthdimension小圆点的宽度ci_heightdimension小圆点的高度ci_margindimension小圆点间的间距ci_animatorreferenceViewPager页面切换时,给小圆点设置动画。设置当前的小圆点恢复到未选中时的状态的动画。例如:当前正从a切换到b,那么该属性设置的是a的动画。ci_animator_reversereference和"ci_animator"属性一样,只不过该属性设置的是b的动画。ci_drawablereference设置当前或选中的小圆点的样式,如颜色、圆角等。ci_drawable_unselectedreference和"ci_drawable"一样,只是该属性设置的是未选中的小圆点的样式。点击上面的"下载源码"下载完整的demo工程。demo简要说明xml布局:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical">     <RelativeLayout         android:layout_width="match_parent"         android:layout_height="0dp"         android:layout_weight="1">         <android.support.v4.view.ViewPager             android:id="@ id/viewpager_default"             android:layout_width="match_parent"             android:layout_height="match_parent"/>         <me.relex.circleindicator.CircleIndicator             android:id="@ id/indicator_default"             android:layout_centerInParent="true"             android:layout_width="fill_parent"             android:layout_height="40dp"/>     </RelativeLayout>     <RelativeLayout         android:layout_width="match_parent"         android:layout_height="0dp"         android:layout_weight="1">         <android.support.v4.view.ViewPager             android:id="@ id/viewpager_custom"             android:layout_width="match_parent"             android:layout_height="match_parent"/>         <me.relex.circleindicator.CircleIndicator             android:id="@ id/indicator_custom"             app:ci_width="10dp"             app:ci_height="4dp"             app:ci_margin="6dp"             app:ci_animator="@animator/indicator_animator"             app:ci_animator_reverse="@animator/indicator_animator_reverse"             app:ci_drawable="@drawable/black_radius_square"             android:layout_centerInParent="true"             android:layout_width="fill_parent"             android:layout_height="40dp"/>     </RelativeLayout>     <RelativeLayout         android:layout_width="match_parent"         android:layout_height="0dp"         android:layout_weight="1">         <android.support.v4.view.ViewPager             android:id="@ id/viewpager_unselected_background"             android:layout_width="match_parent"             android:layout_height="match_parent"/>         <me.relex.circleindicator.CircleIndicator             android:id="@ id/indicator_unselected_background"             android:layout_centerInParent="true"             android:layout_width="fill_parent"             app:ci_width="6dp"             app:ci_height="6dp"             app:ci_animator="@animator/indicator_no_animator"             app:ci_drawable="@drawable/white_radius"             app:ci_drawable_unselected="@drawable/black_radius"             android:layout_height="40dp"             />     </RelativeLayout> </LinearLayout>定义了3组ViewPager和指示器。第一组是采用默认样式的,被选中的小圆点带有放大的动画。第2组比较全,既定义了动画又修改了样式。第3组是取消动画的同时使用了ci_drawable_unselected属性。适配器Adapter:public class DemoPagerAdapter extends FragmentPagerAdapter {         private int pagerCount = 5;         private Random random = new Random();         public DemoPagerAdapter(FragmentManager fm) {             super(fm);         }         @Override public Fragment getItem(int i) {             return ColorFragment.newInstance(0xff000000 | random.nextInt(0x00ffffff));         }         @Override public int getCount() {             return pagerCount;         }     }定义了5个页面或Fragment,通过ColorFragment生成Fragment。Fragment:public class ColorFragment extends Fragment {     private static final String ARG_COLOR = "color";     private int mColor;     public static ColorFragment newInstance(int param1) {         ColorFragment fragment = new ColorFragment();         Bundle args = new Bundle();         args.putInt(ARG_COLOR, param1);         fragment.setArguments(args);         return fragment;     }     public ColorFragment() {     }     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         if (getArguments() != null) {             mColor = getArguments().getInt(ARG_COLOR);         }     }     @Override     public View onCreateView(LayoutInflater inflater, ViewGroup container,             Bundle savedInstanceState) {         View v = inflater.inflate(R.layout.color_fragment, container, false);         v.setBackgroundColor(mColor);         return v;     } }生成空的页面,没有任何控件,只是设置了页面的背景色。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值