ppamorim

Untitled

Jun 30th, 2014
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.36 KB | None | 0 0
  1. package com.Tag_Interativa.vejo_ao_vivo.app.core.view;
  2.  
  3. import android.R;
  4. import android.content.Context;
  5. import android.graphics.Canvas;
  6. import android.graphics.Color;
  7. import android.graphics.Paint;
  8. import android.util.AttributeSet;
  9. import android.util.TypedValue;
  10. import android.view.View;
  11. import android.widget.LinearLayout;
  12.  
  13. public class SlidingTabStrip extends LinearLayout {
  14.  
  15. private static final int DEFAULT_BOTTOM_BORDER_THICKNESS_DIPS = 2;
  16. private static final byte DEFAULT_BOTTOM_BORDER_COLOR_ALPHA = 0x26;
  17. private static final int SELECTED_INDICATOR_THICKNESS_DIPS = 80;
  18. private static final int DEFAULT_SELECTED_INDICATOR_COLOR = 0xFF33B5E5;
  19.  
  20. private static final int DEFAULT_DIVIDER_THICKNESS_DIPS = 1;
  21. private static final byte DEFAULT_DIVIDER_COLOR_ALPHA = 0x20;
  22. private static final float DEFAULT_DIVIDER_HEIGHT = 0.5f;
  23.  
  24. private final int mBottomBorderThickness;
  25. private final Paint mBottomBorderPaint;
  26.  
  27. private final int mSelectedIndicatorThickness;
  28. private final Paint mSelectedIndicatorPaint;
  29.  
  30. private final int mDefaultBottomBorderColor;
  31.  
  32. private final Paint mDividerPaint;
  33. private final float mDividerHeight;
  34.  
  35. private int mSelectedPosition;
  36. private float mSelectionOffset;
  37.  
  38. private SlidingTabLayout.TabColorizer mCustomTabColorizer;
  39. private final SimpleTabColorizer mDefaultTabColorizer;
  40.  
  41. SlidingTabStrip(Context context) {
  42. this(context, null);
  43. }
  44.  
  45. SlidingTabStrip(Context context, AttributeSet attrs) {
  46. super(context, attrs);
  47. setWillNotDraw(false);
  48.  
  49. final float density = getResources().getDisplayMetrics().density;
  50.  
  51. TypedValue outValue = new TypedValue();
  52. context.getTheme().resolveAttribute(R.attr.colorForeground, outValue, true);
  53. final int themeForegroundColor = outValue.data;
  54.  
  55. mDefaultBottomBorderColor = setColorAlpha(themeForegroundColor,
  56. DEFAULT_BOTTOM_BORDER_COLOR_ALPHA);
  57.  
  58. mDefaultTabColorizer = new SimpleTabColorizer();
  59. mDefaultTabColorizer.setIndicatorColors(DEFAULT_SELECTED_INDICATOR_COLOR);
  60. mDefaultTabColorizer.setDividerColors(setColorAlpha(themeForegroundColor,
  61. DEFAULT_DIVIDER_COLOR_ALPHA));
  62.  
  63. mBottomBorderThickness = (int) (DEFAULT_BOTTOM_BORDER_THICKNESS_DIPS * density);
  64. mBottomBorderPaint = new Paint();
  65. mBottomBorderPaint.setColor(mDefaultBottomBorderColor);
  66.  
  67. mSelectedIndicatorThickness = (int) (SELECTED_INDICATOR_THICKNESS_DIPS * density);
  68. mSelectedIndicatorPaint = new Paint();
  69.  
  70. mDividerHeight = DEFAULT_DIVIDER_HEIGHT;
  71. mDividerPaint = new Paint();
  72. mDividerPaint.setStrokeWidth((int) (DEFAULT_DIVIDER_THICKNESS_DIPS * density));
  73. }
  74.  
  75. void setCustomTabColorizer(SlidingTabLayout.TabColorizer customTabColorizer) {
  76. mCustomTabColorizer = customTabColorizer;
  77. invalidate();
  78. }
  79.  
  80. void setSelectedIndicatorColors(int... colors) {
  81. // Make sure that the custom colorizer is removed
  82. mCustomTabColorizer = null;
  83. mDefaultTabColorizer.setIndicatorColors(colors);
  84. invalidate();
  85. }
  86.  
  87. void setDividerColors(int... colors) {
  88. // Make sure that the custom colorizer is removed
  89. mCustomTabColorizer = null;
  90. mDefaultTabColorizer.setDividerColors(colors);
  91. invalidate();
  92. }
  93.  
  94. void onViewPagerPageChanged(int position, float positionOffset) {
  95. mSelectedPosition = position;
  96. mSelectionOffset = positionOffset;
  97. invalidate();
  98. }
  99.  
  100. @Override
  101. protected void onDraw(Canvas canvas) {
  102. final int height = getHeight();
  103. final int childCount = getChildCount();
  104. final int dividerHeightPx = (int) (Math.min(Math.max(0f, mDividerHeight), 1f) * height);
  105. final SlidingTabLayout.TabColorizer tabColorizer = mCustomTabColorizer != null
  106. ? mCustomTabColorizer
  107. : mDefaultTabColorizer;
  108.  
  109. // Thick colored underline below the current selection
  110. if (childCount > 0) {
  111. View selectedTitle = getChildAt(mSelectedPosition);
  112. int left = selectedTitle.getLeft();
  113. int right = selectedTitle.getRight();
  114. int color = tabColorizer.getIndicatorColor(mSelectedPosition);
  115.  
  116. if (mSelectionOffset > 0f && mSelectedPosition < (getChildCount() - 1)) {
  117. int nextColor = tabColorizer.getIndicatorColor(mSelectedPosition + 1);
  118. if (color != nextColor) {
  119. color = blendColors(nextColor, color, mSelectionOffset);
  120. }
  121.  
  122. // Draw the selection partway between the tabs
  123. View nextTitle = getChildAt(mSelectedPosition + 1);
  124. left = (int) (mSelectionOffset * nextTitle.getLeft() +
  125. (1.0f - mSelectionOffset) * left);
  126. right = (int) (mSelectionOffset * nextTitle.getRight() +
  127. (1.0f - mSelectionOffset) * right);
  128. }
  129.  
  130. mSelectedIndicatorPaint.setColor(color);
  131.  
  132. canvas.drawRect(left, 0, right,
  133. height, mSelectedIndicatorPaint);
  134. }
  135.  
  136. // Thin underline along the entire bottom edge
  137. canvas.drawRect(0, height - mBottomBorderThickness, getWidth(), height, mBottomBorderPaint);
  138.  
  139. // Vertical separators between the titles
  140. int separatorTop = (height - dividerHeightPx) / 2;
  141. for (int i = 0; i < childCount - 1; i++) {
  142. View child = getChildAt(i);
  143. mDividerPaint.setColor(tabColorizer.getDividerColor(i));
  144. canvas.drawLine(child.getRight(), separatorTop, child.getRight(),
  145. separatorTop + dividerHeightPx, mDividerPaint);
  146. }
  147. }
  148.  
  149. /**
  150. * Set the alpha value of the {@code color} to be the given {@code alpha} value.
  151. */
  152. private static int setColorAlpha(int color, byte alpha) {
  153. return Color.argb(alpha, Color.red(color), Color.green(color), Color.blue(color));
  154. }
  155.  
  156. /**
  157. * Blend {@code color1} and {@code color2} using the given ratio.
  158. *
  159. * @param ratio of which to blend. 1.0 will return {@code color1}, 0.5 will give an even blend,
  160. * 0.0 will return {@code color2}.
  161. */
  162. private static int blendColors(int color1, int color2, float ratio) {
  163. final float inverseRation = 1f - ratio;
  164. float r = (Color.red(color1) * ratio) + (Color.red(color2) * inverseRation);
  165. float g = (Color.green(color1) * ratio) + (Color.green(color2) * inverseRation);
  166. float b = (Color.blue(color1) * ratio) + (Color.blue(color2) * inverseRation);
  167. return Color.rgb((int) r, (int) g, (int) b);
  168. }
  169.  
  170. private static class SimpleTabColorizer implements SlidingTabLayout.TabColorizer {
  171. private int[] mIndicatorColors;
  172. private int[] mDividerColors;
  173.  
  174. @Override
  175. public final int getIndicatorColor(int position) {
  176. return mIndicatorColors[position % mIndicatorColors.length];
  177. }
  178.  
  179. @Override
  180. public final int getDividerColor(int position) {
  181. return mDividerColors[position % mDividerColors.length];
  182. }
  183.  
  184. void setIndicatorColors(int... colors) {
  185. mIndicatorColors = colors;
  186. }
  187.  
  188. void setDividerColors(int... colors) {
  189. mDividerColors = colors;
  190. }
  191. }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment