/** * Swap Tab effect(color or cross fade) * * @param position The page position * @param positionOffset The page position offset */ private void swapTabColor(int position, float positionOffset, boolean left) { final int colorDefault = mTabColorize.getDefaultTabColor(position); final int colorSelected = mTabColorize.getSelectedTabColor(position); final int nextColorDefault = mTabColorize.getDefaultTabColor(position + 1); final int nextColorSelected = mTabColorize.getSelectedTabColor(position + 1); int color = colorDefault; int nextColor = nextColorSelected; if (mTabColorBlendMode == TabColorBlendMode.DEFAULT_SELECTED) { if (colorDefault != colorSelected) { color = ColorUtils.blendColors(colorDefault, colorSelected, positionOffset); } if (nextColorDefault != nextColorSelected) { nextColor = ColorUtils.blendColors(nextColorSelected, nextColorDefault, positionOffset); } } else if (mTabColorBlendMode == TabColorBlendMode.NEXT_SELECTED) { if (!left) { if (colorDefault != colorSelected) { color = ColorUtils.blendColors(colorDefault, colorSelected, positionOffset); } if (nextColorSelected != colorSelected) { nextColor = ColorUtils.blendColors(nextColorSelected, colorSelected, positionOffset); } } else { if (colorSelected != nextColorSelected) { color = ColorUtils.blendColors(colorSelected, nextColorSelected, 1 - positionOffset); } if (nextColorDefault != nextColorSelected) { nextColor = ColorUtils.blendColors(nextColorDefault, nextColorSelected, 1 - positionOffset); } } } setTabColor(getNeededView(position), color); setTabColor(getNeededView(position + 1), nextColor); }
/** Reset all tab to defaults and select current page tab. */ private void resetTabStrip() { int childCount = mNiceTabStrip.getChildCount(); int selectedPosition = mViewPager.getCurrentItem(); for (int i = 0; i < childCount; i++) { View view = getNeededView(i); switch (mTabMode) { case TITLE_ONLY: { ((TextView) view) .setTextColor( i == selectedPosition ? mTabColorize.getSelectedTabColor(i) : mTabColorize.getDefaultTabColor(i)); break; } case ICON_ONLY: { Drawable drawable = ((ImageView) view).getDrawable(); if (mIconCrossFade && drawable instanceof CrossFadeDrawable) { // tintDrawable(((CrossFadeDrawable) // drawable).getFading().mutate(), mTabColorize.getSelectedTabColor(i)); tintDrawable( ((CrossFadeDrawable) drawable).getFading(), mTabColorize.getSelectedTabColor(i)); // tintDrawable(((CrossFadeDrawable) // drawable).getBase().mutate(), mTabColorize.getDefaultTabColor(i)); tintDrawable( ((CrossFadeDrawable) drawable).getBase(), mTabColorize.getDefaultTabColor(i)); crossFadeDrawable(drawable, i == selectedPosition ? 1f : 0f); } break; } case BOTH: { final int color = i == selectedPosition ? mTabColorize.getSelectedTabColor(i) : mTabColorize.getDefaultTabColor(i); ((TextView) view).setTextColor(color); Drawable drawable = ((TextView) view).getCompoundDrawables()[1]; if (mIconCrossFade && drawable instanceof CrossFadeDrawable) { // tintDrawable(((CrossFadeDrawable) // drawable).getFading().mutate(), mTabColorize.getSelectedTabColor(i)); tintDrawable( ((CrossFadeDrawable) drawable).getFading(), mTabColorize.getSelectedTabColor(i)); // tintDrawable(((CrossFadeDrawable) // drawable).getBase().mutate(), mTabColorize.getDefaultTabColor(i)); tintDrawable( ((CrossFadeDrawable) drawable).getBase(), mTabColorize.getDefaultTabColor(i)); crossFadeDrawable(drawable, i == selectedPosition ? 1f : 0f); } else { // tintDrawable(drawable.mutate(), color); tintDrawable(drawable, color); } break; } } view.setSelected(i == selectedPosition); } }
/** Create a default view to be used for tabs. */ private View createDefaultTabView(int position) { final PagerAdapter adapter = mViewPager.getAdapter(); View view; switch (mTabMode) { case TITLE_ONLY: { TextView textView = new TextView(getContext()); textView.setGravity(Gravity.CENTER); textView.setText(adapter.getPageTitle(position)); textView.setTextColor(mTabColorize.getDefaultTabColor(position)); textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize); textView.setTypeface(null, mTextStyle); textView.setLayoutParams( new ViewGroup.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT)); if (mTabBackground != NO_ID) { textView.setBackgroundResource(mTabBackground); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { TypedValue outValue = new TypedValue(); getContext() .getTheme() .resolveAttribute(android.R.attr.selectableItemBackground, outValue, true); textView.setBackgroundResource(outValue.resourceId); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { textView.setAllCaps(mTextAllCaps); } textView.setPadding(mTabPaddingLeft, mTabPaddingTop, mTabPaddingRight, mTabPaddingBottom); if (position == mViewPager.getCurrentItem()) { textView.setTextColor(mTabColorize.getSelectedTabColor(position)); textView.setSelected(true); } view = textView; break; } case ICON_ONLY: { ImageView imageView = new ImageView(getContext()); imageView.setScaleType(ImageView.ScaleType.CENTER); imageView.setLayoutParams( new ViewGroup.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT)); if (mTabBackground != NO_ID) { imageView.setBackgroundResource(mTabBackground); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { TypedValue outValue = new TypedValue(); getContext() .getTheme() .resolveAttribute(android.R.attr.selectableItemBackground, outValue, true); imageView.setBackgroundResource(outValue.resourceId); } // Drawable drawable = ContextCompat.getDrawable(getContext(), // ((IconTabProvider) adapter).getPageIconResId(position)).mutate(); Drawable drawable = ContextCompat.getDrawable( getContext(), ((IconTabProvider) adapter).getPageIconResId(position)); if (mIconCrossFade && drawable instanceof StateListDrawable) { try { StateListDrawable stateListDrawable = (StateListDrawable) drawable; int fadingIndex = StateListDrawableHelper.getStateDrawableIndex( stateListDrawable, new int[] {android.R.attr.state_selected}); Drawable fading = StateListDrawableHelper.getStateDrawable(stateListDrawable, fadingIndex); int baseIndex = StateListDrawableHelper.getStateDrawableIndex(stateListDrawable, new int[] {0}); Drawable base = StateListDrawableHelper.getStateDrawable(stateListDrawable, baseIndex); CrossFadeDrawable cd = new CrossFadeDrawable(); cd.setFading(fading); // tintDrawable(cd.getFading().mutate(), // mTabColorize.getSelectedTabColor(position)); tintDrawable(cd.getFading(), mTabColorize.getSelectedTabColor(position)); cd.setBase(base); // tintDrawable(cd.getBase().mutate(), // mTabColorize.getDefaultTabColor(position)); tintDrawable(cd.getBase(), mTabColorize.getDefaultTabColor(position)); imageView.setImageDrawable(cd); } catch (Exception e) { imageView.setImageDrawable(drawable); } } else { imageView.setImageDrawable(drawable); } imageView.setPadding( mTabPaddingLeft, mTabPaddingTop, mTabPaddingRight, mTabPaddingBottom); if (position == mViewPager.getCurrentItem()) { Drawable d = imageView.getDrawable(); if (d instanceof CrossFadeDrawable) { crossFadeDrawable(d, 1); } else { tintDrawable(d, mTabColorize.getSelectedTabColor(position)); } imageView.setSelected(true); } else { tintDrawable(imageView.getDrawable(), mTabColorize.getDefaultTabColor(position)); } view = imageView; break; } case BOTH: { TextView textView = new TextView(getContext()); textView.setGravity(Gravity.CENTER); textView.setText(adapter.getPageTitle(position)); textView.setTextColor(mTabColorize.getDefaultTabColor(position)); textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize); textView.setTypeface(null, mTextStyle); textView.setLayoutParams( new ViewGroup.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT)); if (mTabBackground != NO_ID) { textView.setBackgroundResource(mTabBackground); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { TypedValue outValue = new TypedValue(); getContext() .getTheme() .resolveAttribute(android.R.attr.selectableItemBackground, outValue, true); textView.setBackgroundResource(outValue.resourceId); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { textView.setAllCaps(mTextAllCaps); } textView.setPadding(mTabPaddingLeft, mTabPaddingTop, mTabPaddingRight, mTabPaddingBottom); // Drawable drawable = ContextCompat.getDrawable(getContext(), // ((IconTabProvider) adapter).getPageIconResId(position)).mutate(); Drawable drawable = ContextCompat.getDrawable( getContext(), ((IconTabProvider) adapter).getPageIconResId(position)); if (mIconCrossFade && drawable instanceof StateListDrawable) { try { StateListDrawable stateListDrawable = (StateListDrawable) drawable; int fadingIndex = StateListDrawableHelper.getStateDrawableIndex( stateListDrawable, new int[] {android.R.attr.state_selected}); Drawable fading = StateListDrawableHelper.getStateDrawable(stateListDrawable, fadingIndex); int baseIndex = StateListDrawableHelper.getStateDrawableIndex(stateListDrawable, new int[] {0}); Drawable base = StateListDrawableHelper.getStateDrawable(stateListDrawable, baseIndex); CrossFadeDrawable cd = new CrossFadeDrawable(); cd.setFading(fading); cd.setFading(fading); // tintDrawable(cd.getFading().mutate(), // mTabColorize.getSelectedTabColor(position)); tintDrawable(cd.getFading(), mTabColorize.getSelectedTabColor(position)); cd.setBase(base); // tintDrawable(cd.getBase().mutate(), // mTabColorize.getDefaultTabColor(position)); tintDrawable(cd.getBase(), mTabColorize.getDefaultTabColor(position)); textView.setCompoundDrawablesWithIntrinsicBounds(null, cd, null, null); } catch (Exception e) { textView.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null); } } else { textView.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null); } textView.setCompoundDrawablePadding(mDrawablePadding); if (position == mViewPager.getCurrentItem()) { textView.setTextColor(mTabColorize.getSelectedTabColor(position)); Drawable d = textView.getCompoundDrawables()[1]; if (d instanceof CrossFadeDrawable) { crossFadeDrawable(d, 1); } else { tintDrawable(d, mTabColorize.getSelectedTabColor(position)); } textView.setSelected(true); } else { Drawable d = textView.getCompoundDrawables()[1]; if (!(d instanceof CrossFadeDrawable)) { tintDrawable(d, mTabColorize.getDefaultTabColor(position)); } } view = textView; break; } default: { throw new IllegalStateException("Invalid tab mode: " + mTabMode); } } return view; }