InformationSwitchBanner.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package com.datarecovery.master.widget;
  2. import android.content.Context;
  3. import android.graphics.drawable.Drawable;
  4. import android.os.Handler;
  5. import android.text.TextUtils;
  6. import android.util.Pair;
  7. import android.view.LayoutInflater;
  8. import android.view.View;
  9. import android.view.animation.AlphaAnimation;
  10. import android.view.animation.Animation;
  11. import android.view.animation.AnimationSet;
  12. import android.view.animation.TranslateAnimation;
  13. import android.widget.ViewSwitcher;
  14. import com.datarecovery.master.databinding.ViewHomePageSwitchTextBinding;
  15. import java.util.List;
  16. public class InformationSwitchBanner implements ViewSwitcher.ViewFactory, View.OnClickListener {
  17. private static final int DURATION = 1500;
  18. private final Context context;
  19. private final ViewSwitcher viewSwitcher;
  20. private final Handler handler = new Handler();
  21. LayoutInflater inflater;
  22. private List<Pair<CharSequence, CharSequence>> texts;
  23. private int marker;
  24. private AnimationSet InAnimationSet;
  25. private AnimationSet OutAnimationSet;
  26. private int delayTime = 1500;
  27. private OnSwitchListener onSwitchListener;
  28. public InformationSwitchBanner(Context context, ViewSwitcher textSwitcher) {
  29. this.context = context;
  30. this.viewSwitcher = textSwitcher;
  31. inflater = LayoutInflater.from(context);
  32. init();
  33. }
  34. private void init() {
  35. viewSwitcher.setFactory(this);
  36. setText("", "");
  37. createAnimation();
  38. viewSwitcher.setInAnimation(InAnimationSet);
  39. viewSwitcher.setOutAnimation(OutAnimationSet);
  40. viewSwitcher.setOnClickListener(this);
  41. }
  42. private void setText(CharSequence left, CharSequence right) {
  43. ViewHomePageSwitchTextBinding binding = ViewHomePageSwitchTextBinding.bind(viewSwitcher.getNextView());
  44. binding.tvSwitchText.setText(left);
  45. binding.tvSwitchDate.setText(right);
  46. binding.tvSwitchText.setVisibility(TextUtils.isEmpty(left) ? View.GONE : View.VISIBLE);
  47. binding.tvSwitchDate.setVisibility(TextUtils.isEmpty(right) ? View.GONE : View.VISIBLE);
  48. viewSwitcher.showNext();
  49. }
  50. public void setTextColor(Drawable leftDrawable, int padding, int prefixColor, int suffixColor) {
  51. ViewHomePageSwitchTextBinding currentView = ViewHomePageSwitchTextBinding.bind(viewSwitcher.getCurrentView());
  52. ViewHomePageSwitchTextBinding nextView = ViewHomePageSwitchTextBinding.bind(viewSwitcher.getNextView());
  53. currentView.tvSwitchText.setTextColor(prefixColor);
  54. nextView.tvSwitchText.setTextColor(prefixColor);
  55. currentView.tvSwitchDate.setTextColor(suffixColor);
  56. nextView.tvSwitchDate.setTextColor(suffixColor);
  57. leftDrawable.setBounds(0, 0, leftDrawable.getMinimumWidth(), leftDrawable.getMinimumHeight());
  58. currentView.tvSwitchText.setCompoundDrawablePadding(padding);
  59. nextView.tvSwitchText.setCompoundDrawablePadding(padding);
  60. currentView.tvSwitchText.setCompoundDrawables(leftDrawable, null, null, null);
  61. nextView.tvSwitchText.setCompoundDrawables(leftDrawable, null, null, null);
  62. }
  63. @Override
  64. public View makeView() {
  65. ViewHomePageSwitchTextBinding viewHomePageSwitchTextBinding = ViewHomePageSwitchTextBinding.inflate(inflater);
  66. return viewHomePageSwitchTextBinding.getRoot();
  67. }
  68. @Override
  69. public void onClick(View v) {
  70. }
  71. public void update(List<Pair<CharSequence, CharSequence>> list) {
  72. if (list == null || list.size() == 0) {
  73. return;
  74. }
  75. stop();
  76. texts = list;
  77. marker = 0;
  78. Pair<CharSequence, CharSequence> firstTxt = texts.get(0);
  79. setText(firstTxt.first, firstTxt.second);
  80. start();
  81. }
  82. public void start() {
  83. stop();
  84. handler.postDelayed(task, delayTime);
  85. }
  86. public void stop() {
  87. handler.removeCallbacks(task);
  88. }
  89. public int getMarker() {
  90. return marker;
  91. }
  92. public InformationSwitchBanner setTexts(List<Pair<CharSequence, CharSequence>> texts) {
  93. this.texts = texts;
  94. return this;
  95. }
  96. public void setDelayTime(int delayTime) {
  97. this.delayTime = delayTime;
  98. }
  99. private void createAnimation() {
  100. AlphaAnimation alphaAnimation;
  101. TranslateAnimation translateAnimation;
  102. int h = viewSwitcher.getHeight();
  103. if (h <= 0) {
  104. viewSwitcher.measure(0, 0);
  105. h = viewSwitcher.getMeasuredHeight();
  106. }
  107. InAnimationSet = new AnimationSet(true);
  108. OutAnimationSet = new AnimationSet(true);
  109. alphaAnimation = new AlphaAnimation(0, 1);
  110. translateAnimation = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0,
  111. Animation.ABSOLUTE, h, Animation.ABSOLUTE, 0);
  112. InAnimationSet.addAnimation(alphaAnimation);
  113. InAnimationSet.addAnimation(translateAnimation);
  114. InAnimationSet.setDuration(DURATION);
  115. alphaAnimation = new AlphaAnimation(1, 0);
  116. translateAnimation = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0,
  117. Animation.ABSOLUTE, 0, Animation.ABSOLUTE, -h);
  118. OutAnimationSet.addAnimation(alphaAnimation);
  119. OutAnimationSet.addAnimation(translateAnimation);
  120. OutAnimationSet.setDuration(DURATION);
  121. }
  122. private void nextView() {
  123. if (onSwitchListener != null) {
  124. onSwitchListener.preSwitch();
  125. }
  126. marker = ++marker % texts.size();
  127. Pair<CharSequence, CharSequence> nextTxt = texts.get(marker);
  128. setText(nextTxt.first, nextTxt.second);
  129. if (onSwitchListener != null) {
  130. onSwitchListener.onSwitch();
  131. }
  132. }
  133. public void setOnSwitchListener(OnSwitchListener listener) {
  134. this.onSwitchListener = listener;
  135. }
  136. public interface OnSwitchListener {
  137. void preSwitch();
  138. void onSwitch();
  139. }
  140. private final Runnable task = new Runnable() {
  141. @Override
  142. public void run() {
  143. nextView();
  144. handler.postDelayed(task, delayTime * 2L);
  145. }
  146. };
  147. }