Advertisement
Guest User

Untitled

a guest
Dec 20th, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.49 KB | None | 0 0
  1. package com.hero.tweaks.widget;
  2.  
  3. import java.util.Locale;
  4.  
  5. import android.animation.Animator;
  6. import android.annotation.SuppressLint;
  7. import android.content.Context;
  8. import android.content.res.Resources;
  9. import android.graphics.Color;
  10. import android.graphics.Paint;
  11. import android.graphics.Point;
  12. import android.graphics.Rect;
  13. import android.graphics.drawable.Drawable;
  14. import android.os.Build;
  15. import android.view.LayoutInflater;
  16. import android.view.MotionEvent;
  17. import android.view.View;
  18. import android.widget.ImageView;
  19. import android.widget.TextView;
  20. import at.markushi.ui.RevealColorView;
  21.  
  22. import com.hero.tweaks.R;
  23.  
  24. @SuppressLint({"InflateParams", "ClickableViewAccessibility"})
  25. /**
  26. * A simple Tab with Material Design style
  27. * @author neokree
  28. *
  29. */
  30. public class MaterialTab
  31. implements
  32. View.OnTouchListener,
  33. Animator.AnimatorListener {
  34.  
  35. private final static int REVEAL_DURATION = 400;
  36. private final static int HIDE_DURATION = 500;
  37.  
  38. private View completeView;
  39. private ImageView icon;
  40. private TextView text;
  41. private RevealColorView background;
  42. private ImageView selector;
  43.  
  44. private Resources res;
  45. private MaterialTabListener listener;
  46. private Drawable iconDrawable;
  47.  
  48. private int textColor;
  49. private int iconColor;
  50. private int primaryColor;
  51. private int accentColor;
  52.  
  53. private boolean active;
  54. private int position;
  55. private boolean hasIcon;
  56. private float density;
  57. private Point lastTouchedPoint;
  58.  
  59. public MaterialTab(Context ctx, boolean hasIcon) {
  60. this.hasIcon = hasIcon;
  61. density = ctx.getResources().getDisplayMetrics().density;
  62. res = ctx.getResources();
  63.  
  64. if (!hasIcon) {
  65. // if there is no icon
  66. completeView = LayoutInflater.from(ctx).inflate(
  67. R.layout.material_tab, null);
  68.  
  69. text = (TextView) completeView.findViewById(R.id.text);
  70. } else {
  71. // with icon
  72. completeView = LayoutInflater.from(ctx).inflate(
  73. R.layout.material_tab_icon, null);
  74.  
  75. icon = (ImageView) completeView.findViewById(R.id.icon);
  76. }
  77.  
  78. background = (RevealColorView) completeView.findViewById(R.id.reveal);
  79. selector = (ImageView) completeView.findViewById(R.id.selector);
  80.  
  81. // set the listener
  82. completeView.setOnTouchListener(this);
  83.  
  84. active = false;
  85. textColor = Color.WHITE; // default white text
  86. iconColor = Color.WHITE; // and icon
  87. }
  88.  
  89. public void setAccentColor(int color) {
  90. this.accentColor = color;
  91. this.textColor = color;
  92. this.iconColor = color;
  93. }
  94.  
  95. public void setPrimaryColor(int color) {
  96. this.primaryColor = color;
  97. background.setBackgroundColor(color);
  98. }
  99.  
  100. public void setTextColor(int color) {
  101. textColor = color;
  102. if (text != null) {
  103. text.setTextColor(color);
  104. }
  105. }
  106.  
  107. public void setIconColor(int color) {
  108. this.iconColor = color;
  109. if (this.icon != null)
  110. this.icon.setColorFilter(color);
  111. }
  112.  
  113. public MaterialTab setText(CharSequence text) {
  114. this.text.setText(text.toString().toUpperCase(Locale.US));
  115. return this;
  116. }
  117.  
  118. public MaterialTab setIcon(Drawable icon) {
  119. iconDrawable = icon;
  120.  
  121. this.icon.setImageDrawable(icon);
  122. this.setIconColor(this.iconColor);
  123. return this;
  124. }
  125.  
  126. public void disableTab() {
  127. // set 60% alpha to text color
  128. if (text != null)
  129. this.text.setTextColor(Color.argb(0x99, Color.red(textColor),
  130. Color.green(textColor), Color.blue(textColor)));
  131. // set 60% alpha to icon
  132. if (icon != null)
  133. setIconAlpha(0x99);
  134.  
  135. // set transparent the selector view
  136. this.selector.setBackgroundColor(res
  137. .getColor(android.R.color.transparent));
  138.  
  139. active = false;
  140.  
  141. if (listener != null)
  142. listener.onTabUnselected(this);
  143. }
  144.  
  145. public void activateTab() {
  146. // set full color text
  147. if (text != null)
  148. this.text.setTextColor(textColor);
  149. // set 100% alpha to icon
  150. if (icon != null)
  151. setIconAlpha(0xFF);
  152.  
  153. // set accent color to selector view
  154. this.selector.setBackgroundColor(accentColor);
  155.  
  156. active = true;
  157. }
  158.  
  159. public boolean isSelected() {
  160. return active;
  161. }
  162.  
  163. @Override
  164. public boolean onTouch(View v, MotionEvent event) {
  165. lastTouchedPoint = new Point();
  166. lastTouchedPoint.x = (int) event.getX();
  167. lastTouchedPoint.y = (int) event.getY();
  168.  
  169. /*
  170. * // old effects if(event.getAction() == MotionEvent.ACTION_DOWN ) { //
  171. * reveal the tab this.background.reveal(point.x, point.y,
  172. * Color.argb(0x80, Color.red(accentColor), Color.green(accentColor),
  173. * Color.blue(accentColor)) ,0,250, null);
  174. *
  175. * return true; }
  176. *
  177. * if(event.getAction() == MotionEvent.ACTION_UP) { // hide reveal
  178. * this.background.reveal(point.x, point.y, primaryColor,0,700, null);
  179. * //background.hide(point.x, point.y, primaryColor,0,700, null);
  180. *
  181. * // set the click if(listener != null) {
  182. *
  183. * if(active) { // if the tab is active when the user click on it it
  184. * will be reselect listener.onTabReselected(this); } else {
  185. * listener.onTabSelected(this); } } // if the tab is not activated, it
  186. * will be active if(!active) this.activateTab();
  187. *
  188. * return true; }
  189. */
  190. if (event.getAction() == MotionEvent.ACTION_DOWN) {
  191. // do nothing
  192. return true;
  193. }
  194.  
  195. // new effects
  196. if (event.getAction() == MotionEvent.ACTION_UP) {
  197. // set the backgroundcolor
  198. this.background.reveal(
  199. lastTouchedPoint.x,
  200. lastTouchedPoint.y,
  201. Color.argb(0x80, Color.red(accentColor),
  202. Color.green(accentColor), Color.blue(accentColor)),
  203. 0, REVEAL_DURATION, this);
  204.  
  205. // set the click
  206. if (listener != null) {
  207.  
  208. if (active) {
  209. // if the tab is active when the user click on it it will be
  210. // reselect
  211. listener.onTabReselected(this);
  212. } else {
  213. listener.onTabSelected(this);
  214. }
  215. }
  216. // if the tab is not activated, it will be active
  217. if (!active)
  218. this.activateTab();
  219.  
  220. return true;
  221. }
  222.  
  223. return false;
  224. }
  225.  
  226. public View getView() {
  227. return completeView;
  228. }
  229.  
  230. public MaterialTab setTabListener(MaterialTabListener listener) {
  231. this.listener = listener;
  232. return this;
  233. }
  234.  
  235. public int getPosition() {
  236. return position;
  237. }
  238.  
  239. public void setPosition(int position) {
  240. this.position = position;
  241. }
  242.  
  243. @SuppressLint({"NewApi"})
  244. private void setIconAlpha(int paramInt) {
  245. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
  246. this.icon.setImageAlpha(paramInt);
  247. return;
  248. }
  249. this.icon.setColorFilter(Color.argb(paramInt,
  250. Color.red(this.iconColor), Color.green(this.iconColor),
  251. Color.blue(this.iconColor)));
  252. }
  253.  
  254. private int getTextLenght() {
  255. String textString = text.getText().toString();
  256. Rect bounds = new Rect();
  257. Paint textPaint = text.getPaint();
  258. textPaint.getTextBounds(textString, 0, textString.length(), bounds);
  259. return bounds.width();
  260. }
  261.  
  262. private int getIconWidth() {
  263. return (int) (density * 24);
  264. }
  265.  
  266. public int getTabMinWidth() {
  267. if (hasIcon) {
  268. return getIconWidth();
  269. } else {
  270. return getTextLenght();
  271. }
  272. }
  273.  
  274. // AnimatorListener methods
  275. @Override
  276. public void onAnimationStart(Animator animation) {
  277. // this.background.setBackgroundColor(Color.argb(0x80,
  278. // Color.red(accentColor), Color.green(accentColor),
  279. // Color.blue(accentColor)));
  280. }
  281.  
  282. @Override
  283. public void onAnimationEnd(Animator animation) {
  284. // this.background.setBackgroundColor(Color.TRANSPARENT);
  285.  
  286. this.background.reveal(lastTouchedPoint.x, lastTouchedPoint.y,
  287. primaryColor, 0, HIDE_DURATION, null);
  288. }
  289.  
  290. @Override
  291. public void onAnimationCancel(Animator animation) {
  292.  
  293. }
  294.  
  295. @Override
  296. public void onAnimationRepeat(Animator animation) {
  297.  
  298. }
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement