创建一个自定义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) {
}
});