TextView在ViewPager中颜色切换

创建一个自定义Viwe

1、这里是xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_height="wrap_content">

    <FrameLayout
        android:layout_gravity="center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
      <TextView
        android:id="@+id/tv_title"
        android:textColor="@android:color/black"
        android:layout_gravity="center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="10dp"
        tools:text="微信"/>
</LinearLayout>

2、新建一个Java类继承FrameLayout 颜色变化主要是通过evaluate()方法实现

public class TabView extends FrameLayout{
	private TextView tvTitle;
	private static final int COLOR_DEFAULT = Color.parseColor("#ff000000");		//开始颜色
	private static final int COLOR_SELECT = Color.parseColor("#FF45C01A");		//结束颜色
	public View(Context context, AttributeSet attrs) {
   		 super(context, attrs);
   	 	//绑定布局
   		 inflate(context, R.layout.tab_view, this);
   		 //初始化
   		 tvTitle = fidViewById(R.id.tv_title);
   		 setProgress(0);    //设置开始颜色
	 	}
   	 //对外获取文字的方法
   	 public void setText(String title){
		tvTitle.setText(title);
	    }
   	 //对外设置颜色变化的方法 progress透明度        这里你直接打log   progress看比较直观
         public void setProgress(int progress){
		//ivIcon.setAlpha(1 - progress);		//从深变浅
		//ivIconSelect.setAlpha(progress);		//由浅变深
        	 tvTitle.setTextColor(evaluate(progress,COLOR_DEFAULT,COLOR_SELECT));
          }
	/**
 	 * 颜色变化
 	 * @param fraction  	  进度
	  * @param startValue    开始颜色
 	 * @param endValue      结束颜色
 	 * @return
	  */
	  public int evaluate(float fraction,int startValue,int endValue){
	  	int startInt = startValue;
		float startA = (startInt >> 24) & 0xff;
		float startR = (startInt >> 16) & 0xff;
		float startG = (startInt >> 8) & 0xff;
		float startB = startInt & 0xff;
		int endInt = endValue;
		float endA = (endInt >> 24) & 0xff;
		float endR = (endInt >> 16) & 0xff;
		float endG = (endInt >> 8) & 0xff;
		float endB = endInt & 0xff;
		return ((int) (startA + (int) (fraction * (endA - startA))) << 24) |
      		  ((int) (startR + (int) (fraction * (endR - startR))) << 16) |
       		 ((int) (startG + (int) (fraction * (endG - startG))) << 8) |
       		 ((int) (startB + (int) (fraction * (endB - startB))));
	  }
 }

ViewPager示例代码比较简单就不写注释了

        mVpMain.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                /**
                 * positionOffset滑动的进度
                 * 左->右 0->1, left pos ,right pos + 1, positionOffset 0~1
                 * left progress:1~0 (1 - positionOffset); right progress: 0~1  positionOffset;
                 *
                 * 右->左 1->0, left pos, right pos + 1, positionOffset 1~0
                 * left progress:0~1 (1 - positionOffset); right progress: 1~0  positionOffset;
                 */
//                if(positionOffset > 0){
//                    RadioButton left = mTabs.get(position);
//                    RadioButton right = mTabs.get(position + 1);
//                    left.setText((1 - positionOffset) + "");
//                    right.setText(positionOffset + "");
//                }
                if(positionOffset > 0){
                //主要是把进度传给TabView
                    TabView left = mTabs.get(position);		
                    TabView right = mTabs.get(position + 1);
                    left.setProgress((1 - positionOffset));
                    right.setProgress(positionOffset);
                }
            }


            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xpq_lrh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值