我是靠谱客的博主 丰富蜗牛,这篇文章主要介绍MagicIndicator选中后字体样式,现在分享给大家,希望可以做个参考。

先上效果,gif不流畅,凑合着看,选中后字体加粗效果

复制代码
1
2
需要重写指示器标题的类方法,直接继承你需要用指示器标题类 重写以下方法

   举个列子:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class MyClipPagerTitleView extends ClipPagerTitleView{ public MyClipPagerTitleView(Context context) { super(context); } /** * 离开 * * @param leavePercent 离开的百分比, 0.0f - 1.0f * @param leftToRight 从左至右离开 */ @Override public void onLeave(int index, int totalCount, float leavePercent, boolean leftToRight) { super.onLeave(index, totalCount, leavePercent, leftToRight); setTypeface(Typeface.DEFAULT_BOLD); //选中后的字体样式,根据需求自己做修改 } /** * 进入 * * @param enterPercent 进入的百分比, 0.0f - 1.0f * @param leftToRight 从左至右离开 */ @Override public void onEnter(int index, int totalCount, float enterPercent, boolean leftToRight) { super.onEnter(index, totalCount, enterPercent, leftToRight); setTypeface(Typeface.DEFAULT);//未选中的字体样式,根据需求自己做修改 } }

 

 

 

上述效果图的代码实现:

1.需要先创建自定义的类继承ColorTransitionPagerTitleView,如下:

 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import android.content.Context; import android.graphics.Typeface; import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView; /** * 带颜色渐变和缩放的指示器标题以及选中字体样式改变 */ public class ScaleTransitionPagerTitleView extends ColorTransitionPagerTitleView { private float mMinScale = 0.75f; public ScaleTransitionPagerTitleView(Context context) { super(context); } @Override public void onEnter(int index, int totalCount, float enterPercent, boolean leftToRight) { super.onEnter(index, totalCount, enterPercent, leftToRight); // 实现颜色渐变 setScaleX(mMinScale + (1.0f - mMinScale) * enterPercent); setScaleY(mMinScale + (1.0f - mMinScale) * enterPercent); setTypeface(Typeface.DEFAULT_BOLD); //选中后的字体样式,根据需求自己做修改 } @Override public void onLeave(int index, int totalCount, float leavePercent, boolean leftToRight) { super.onLeave(index, totalCount, leavePercent, leftToRight); // 实现颜色渐变 setScaleX(1.0f + (mMinScale - 1.0f) * leavePercent); setScaleY(1.0f + (mMinScale - 1.0f) * leavePercent); setTypeface(Typeface.DEFAULT);//未选中的字体样式,根据需求自己做修改 } public float getMinScale() { return mMinScale; } public void setMinScale(float minScale) { mMinScale = minScale; }

 

以下是使用方法和常规一样

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
MagicIndicator magicIndicator = itemView.findViewById(R.id.magic_indicator); CommonNavigator commonNavigator = new CommonNavigator(MyApplication.getContext()); commonNavigator.setScrollPivotX(0.65f); // commonNavigator.setAdjustMode(true); //ture 即标题平分屏幕宽度的模式 commonNavigator.setAdapter(new CommonNavigatorAdapter() { @Override public int getCount() { return mTitleDataList == null ? 0 : mTitleDataList.size(); } @Override public IPagerTitleView getTitleView(Context context, final int index) { SimplePagerTitleView simplePagerTitleView = new ScaleTransitionPagerTitleView(context); //使用自定义 simplePagerTitleView.setText(mTitleDataList.get(index)); simplePagerTitleView.setTextSize(22); simplePagerTitleView.setNormalColor(Color.parseColor("#666666")); simplePagerTitleView.setSelectedColor(Color.parseColor("#11C4C4")); simplePagerTitleView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { myViewPager.setCurrentItem(index); } }); return simplePagerTitleView; } @Override public IPagerIndicator getIndicator(Context context) { BezierPagerIndicator indicator = new BezierPagerIndicator(context); indicator.setColors(Color.parseColor("#11C4C4")); return indicator; } }); magicIndicator.setNavigator(commonNavigator); ViewPagerHelper.bind(magicIndicator, myViewPager);

最后

以上就是丰富蜗牛最近收集整理的关于MagicIndicator选中后字体样式的全部内容,更多相关MagicIndicator选中后字体样式内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(74)

评论列表共有 0 条评论

立即
投稿
返回
顶部