简体   繁体   English

PagerSlidingTabStrip更改指示器的颜色

[英]PagerSlidingTabStrip Changing colour of the indicator

Hi I am using PagerSlidingTabStrip for showing tabs in viewpager. 嗨,我正在使用PagerSlidingTabStrip在viewpager中显示选项卡。 I have to change the colour of the indicator from red to green when the user slides from 1st to 2nd fragment. 当用户从第一片段滑动到第二片段时,我必须将指示器的颜色从红色更改为绿色。 The transition of colors has to be a smooth one. 颜色的过渡必须是平滑的。 But I am not able to decide how I can do it. 但是我无法决定如何去做。 Please Help !!! 请帮忙 !!! Thanks in advance!!!! 提前致谢!!!!

Open your PagerSlidingTabStrip library class you will get these lines of codes like 打开您的PagerSlidingTabStrip库类,您将获得以下代码行:

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

Otherwise by programmatically you can do this 否则,可以通过编程方式执行此操作

tabs.setIndicatorColor(indicatorColor)

Change those color values you will get what you want. 更改这些颜色值,您将获得所需的颜色。

Try this! 尝试这个!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM