自定义tab栏 学习总结

       实现的主要效果是当滑动Viewpager时,自定义tab栏的分类的字体的颜色会有过度的变化。

        本次的学习总结主要包含:

        1、自定义TextView,实现字体从左到右的变色过程。

        2、在学习别人的代码过程中,学习到了一些技巧:

                    1.利用Bundle和getArguments()、setArguments(),实现了New一个Fragmen的时候利用函数传递一个字符串 的功能。

public class frag_item extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.frag_item,container,false);
        TextView textView = view.findViewById(R.id.textView_frag);
        Bundle bundle = getArguments();
        textView.setText(bundle.getString("title"));
        return view;
    }

    public static frag_item newInstace(String fragName){
        Bundle bundle = new Bundle();
        frag_item fragItem = new frag_item();
        bundle.putString("title",fragName);
        fragItem.setArguments(bundle);
        return fragItem;
    }
}

        getArguments()、setArguments()的含义:

当Activity类动态加载fragment时可以通过fragment的setArguments()传入值,并在fragment类中通过fragment的getArguments()方法获得传入的值; 

                2、tab栏中的分类文字在java代码中添加,通过xml文件中定义一个LinerLayout,然后代码中通过addView()的方法,将每个分类中的文字即自定义TextView加入到这个ViewGroup当中去。代码如下:

                

private void initTablayout() {
        for(int i = 0;i<items.length;i++){
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
            params.weight = 1;
            TextColorChange textColorChange = new TextColorChange(this);
            textColorChange.setTextSize(20);
            textColorChange.setText(items[i]);
            textColorChange.setLayoutParams(params);
            tabLayout_lin.addView(textColorChange);
            textColorChangeList.add(textColorChange);
        }
    }

LayoutParams方面知识:这方面内容在自定义View方面还是挺重要的,打算下一步研究一下这个。



viewPager方面,两个重要的函数:

 viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager())
 viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener()

viewPager的初始化可在setAdapter中进行。

  TextColorChange textColorChange = textColorChangeList.get(position);
                textColorChange.setCurrentProgress(positionOffset);

 下面是MainActivity的代码,也当做自己以后做这方面的参考:

public class MainActivity extends AppCompatActivity {
    String[] items = {"王者","吃鸡","求生"};
    LinearLayout tabLayout_lin;
    ViewPager viewPager;
    List<TextColorChange> textColorChangeList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textColorChangeList = new ArrayList<>();
        viewPager = findViewById(R.id.viewPager);

        tabLayout_lin = new LinearLayout(this);
        tabLayout_lin = findViewById(R.id.tabLayout_lin);
        initTablayout();
        initFrag();

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                TextColorChange textColorChange = textColorChangeList.get(position);
                textColorChange.setCurrentProgress(positionOffset);
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initFrag() {
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return frag_item.newInstace(items[position]);
            }

            @Override
            public int getCount() {
                return items.length;
            }
        });
    }

    private void initTablayout() {
        for(int i = 0;i<items.length;i++){
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
            params.weight = 1;
            TextColorChange textColorChange = new TextColorChange(this);
            textColorChange.setTextSize(20);
            textColorChange.setText(items[i]);
            textColorChange.setLayoutParams(params);
            tabLayout_lin.addView(textColorChange);
            textColorChangeList.add(textColorChange);
        }
    }
}
自定义TextView(tab栏分类 文字)代码:
        
public class TextColorChange extends android.support.v7.widget.AppCompatTextView {
    Paint orginPaint,changePaint;
    Boolean animFlag = false;
    float currentProgress;
    Direction direction = Direction.LEFT_TO_RIGHT;

    public enum Direction{
        LEFT_TO_RIGHT,RIGHT_TO_LEFT
    }

    public TextColorChange(Context context) {
        this(context,null);
    }

    public TextColorChange(Context context, @Nullable AttributeSet attrs) {
        this(context,attrs,0);
    }

    public TextColorChange(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initial();
    }

    private void initial() {
        orginPaint = getMyPaint(Color.BLACK);
        changePaint = getMyPaint(Color.RED);

    }


    @Override
    protected void onDraw(Canvas canvas) {
       // super.onDraw(canvas);
        Rect bounds = new Rect();
        String text = getText().toString();
        Log.d(TAG, "Text"+text);
        Log.d(TAG, "orgin"+orginPaint);
        orginPaint.setTextSize(getTextSize());
        changePaint.setTextSize(getTextSize());
        orginPaint.getTextBounds(text,0,text.length(),bounds);
        int x = getWidth() / 2 - bounds.width() / 2;
        Log.d(TAG, "X:"+x+"  ");
        Paint.FontMetricsInt fontMetricsInt = orginPaint.getFontMetricsInt();
        int dy = (fontMetricsInt.bottom - fontMetricsInt.top) / 2 - fontMetricsInt.bottom;
        int baseline = getHeight() / 2 + dy;//top + bottom = getheight?????????????

            if(direction == Direction.LEFT_TO_RIGHT){
                float currntPosition = x + bounds.width() * currentProgress;
                Log.d(TAG, "onDraw: "+currntPosition);
                canvas.save();
                canvas.clipRect(0,0,currntPosition,getHeight());
                canvas.drawText(text,x,baseline,orginPaint);
                canvas.restore();

                canvas.save();
                float textEndPosition = x + bounds.width();
                Log.d(TAG, "bound Width"+bounds.width());
                canvas.clipRect(currntPosition,0,textEndPosition,getHeight());
                canvas.drawText(text,x,baseline,changePaint);
                canvas.restore();
            }else {
                float currntPosition = x + bounds.width() * currentProgress;
                Log.d(TAG, "onDraw: "+currntPosition);
                canvas.save();
                canvas.clipRect(0,0,currntPosition,getHeight());
                canvas.drawText(text,x,baseline,orginPaint);
                canvas.restore();

                canvas.save();
                float textEndPosition = x + bounds.width();
                Log.d(TAG, "bound Width"+bounds.width());
                canvas.clipRect(currntPosition,0,textEndPosition,getHeight());
                canvas.drawText(text,x,baseline,changePaint);
                canvas.restore();
            }



    }

    private void startAnim() {
        ValueAnimator valueAnimator = ObjectAnimator.ofFloat(0,1);
        valueAnimator.setDuration(3000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                currentProgress = (float)animation.getAnimatedValue();
                animFlag = false;
                invalidate();
            }
        });
        valueAnimator.start();
    }

    public Paint getMyPaint(int color) {
        Paint paint = new Paint();
        paint.setColor(color);
        paint.setAntiAlias(true);
        return paint;
    }

    public void setCurrentProgress(float currentProgress){
        this.currentProgress = currentProgress;
        invalidate();

    }
    public void setDirection(Direction direction){
        this.direction = direction;
    }

}

frag_item代码:

public class frag_item extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.frag_item,container,false);
        TextView textView = view.findViewById(R.id.textView_frag);
        Bundle bundle = getArguments();
        textView.setText(bundle.getString("title"));
        return view;
    }

    public static frag_item newInstace(String fragName){
        Bundle bundle = new Bundle();
        frag_item fragItem = new frag_item();
        bundle.putString("title",fragName);
        fragItem.setArguments(bundle);
        return fragItem;
    }
}

今天自己的心情好像还是有些浮躁 ,自己太在意别人对自己的看法?应该还是自己的心态调整不争取,而且感觉自己在安排自己生活中事情的轻重度时,很多时候安排的并不妥当,

    革命尚未成功,同志任需努力啊

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值