PagerSlidingTabStrip更改指标的颜色

时间:2015-04-02 05:13:00

标签: android android-viewpager android-pageradapter

您好我正在使用PagerSlidingTabStrip在viewpager中显示标签。当用户从第1个片段滑动到第2个片段时,我必须将指示器的颜色从红色更改为绿色。颜色的过渡必须是平滑的。但我无法决定如何做到这一点。请帮忙 !!! 在此先感谢!!!!

2 个答案:

答案 0 :(得分:0)

打开PagerSlidingTabStrip库类,您将获得这些代码行,如

private int indicatorColor = 0xFF666666;
private int underlineColor = 0x1A000000;

否则通过编程方式可以执行此操作

tabs.setIndicatorColor(indicatorColor)

更改您想要的颜色值。

答案 1 :(得分:0)

试试这个!

int NTABS = 2;
int[] COLORS = { 0xFFFF0000, 0xFF00FF00 }; // COLORS.length == NTABS

mPagerSlidingTabStrip.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        mPagerSlidingTabStrip.setIndicatorColor(getNewColor(position, positionOffset));
    }

    @Override public void onPageSelected(int position) {

    }

    @Override public void onPageScrollStateChanged(int state) {

    }
});


private int getNewColor(int position, float offset) {
    int next;
    if (offset > 0) next = (position < NTABS) ? position+1 : position;
    else next = (position > 0) ? position-1 : 0;
    return blendColors(COLORS[next], COLORS[position], offset);
}

public int blendColors(int color1, int color2, float ratio) {
    final float inverseRation = 1f - ratio;
    float a = (Color.alpha(color1) * ratio) + (Color.alpha(color2) * inverseRation);
    float r = (Color.red  (color1) * ratio) + (Color.red  (color2) * inverseRation);
    float g = (Color.green(color1) * ratio) + (Color.green(color2) * inverseRation);
    float b = (Color.blue (color1) * ratio) + (Color.blue (color2) * inverseRation);
    return Color.argb((int) a, (int) r, (int) g, (int) b);
}