MagicIndicator选中后字体样式

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

MagicIndicator选中后字体样式_第1张图片

需要重写指示器标题的类方法,直接继承你需要用指示器标题类 重写以下方法

   举个列子:

   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,如下:

 

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;
    }

 

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

   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选中后字体样式)