Guest User

Untitled

a guest
Apr 22nd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1. public static void changeOverTime(final MonitoredVariable<Integer> tVar, final int tTo, final long tTime, final long tUpdateFreq) {
  2. if (tTime < tUpdateFreq) { Log.e(TAG, "Time must be less then update freq."); }
  3. if (tVar == null) { Log.e(TAG, "Container cannot be null."); }
  4. else {
  5. final Thread tBackgroundThread = new Thread(new Runnable() {
  6. @Override
  7. public void run() {
  8. float tSteps = tTime / tUpdateFreq; // 2000/100 = 20
  9. float tInterval = (tTo - tVar.get()) / tSteps; // 67-175 = -108/20 = -5.4
  10. float tVal = tVar.get(); //175
  11. while (Math.round(tVal) != tTo) { //67(After 20 Times) != 67 -> FALSE
  12. Debug.Log(TAG, "EQ: " + Math.round(tVal) + "?=" + tTo);
  13. tVal += tInterval; // -5.4 * 20(Times) = -108+175 = 67
  14. tryToSleep(tUpdateFreq); // 100ms * 20(Times) = 2000ms total
  15. tVar.set(Math.round(tVal));
  16. }
  17. }
  18. });
  19. tBackgroundThread.start();
  20. }
  21. }
  22.  
  23. private static void tryToSleep(long tTime) {
  24. try { sleep(tTime); }
  25. catch (InterruptedException e) { e.printStackTrace(); }
  26. }
  27.  
  28. public class MonitoredVariable<Prototype> {
  29. protected Prototype mData;
  30. protected ChangeListener mListener;
  31.  
  32. public MonitoredVariable(Prototype tData) {
  33. this(tData, null);
  34. }
  35.  
  36. public MonitoredVariable(Prototype tData, ChangeListener tListener) {
  37. if (tListener != null) setListener(tListener);
  38. mData = tData;
  39. }
  40.  
  41. public Prototype get() {
  42. return mData;
  43. }
  44. public void set(Prototype tData) {
  45. if (mData != tData) {
  46. mData = tData;
  47. notifyChange();
  48. }
  49. }
  50.  
  51. public void setListener(ChangeListener tListener) {
  52. mListener = tListener;
  53. }
  54. public ChangeListener getListener() {
  55. return mListener;
  56. }
  57. public void notifyChange() {
  58. if (mListener != null) mListener.onChange();
  59. }
  60. public interface ChangeListener {
  61. void onChange();
  62. }
  63. }
  64.  
  65. public static void init() {
  66. MonitoredVariable.ChangeListener tUpdateBackground = new MonitoredVariable.ChangeListener() {
  67. @Override
  68. public void onChange() { updateBackgroud();
  69. }
  70. };
  71. mTop = new MonitoredVariable[]{
  72. new MonitoredVariable<>(0, tUpdateBackground),
  73. new MonitoredVariable<>(0, tUpdateBackground),
  74. new MonitoredVariable<>(0, tUpdateBackground)
  75. };
  76. mBottom = new MonitoredVariable[]{
  77. new MonitoredVariable<>(0, tUpdateBackground),
  78. new MonitoredVariable<>(0, tUpdateBackground),
  79. new MonitoredVariable<>(0, tUpdateBackground)
  80. };
  81.  
  82.  
  83. mAnimationLoop = new Handler();
  84. mAnimation = new Runnable() {
  85. @Override
  86. public void run() {
  87. Debug.Log(TAG, "RUNNING ANIMATION");
  88. final Random RNG = new Random();
  89. for (MonitoredVariable<Integer>[] tBackground: new MonitoredVariable[][] {mTop, mBottom}) {
  90. for (MonitoredVariable<Integer> tColor : tBackground) {
  91. int tRandomColor = RNG.nextInt(255);
  92. //tColor.set(tRandomColor);
  93. Shift.changeOverTime(tColor, tRandomColor, 2000, 100);
  94. }
  95. }
  96. if(mAnimate.get()) {
  97. mAnimationLoop.postDelayed(mAnimation, 10000);
  98. }
  99. }
  100. };
  101.  
  102. mAnimate = new MonitoredVariable<>(false, new MonitoredVariable.ChangeListener() {
  103. @Override
  104. public void onChange() {
  105. if (mAnimate.get()) mAnimationLoop.postDelayed(mAnimation, 0);
  106. else mAnimationLoop.removeCallbacks(mAnimation);
  107. }
  108. });
  109. }
  110.  
  111. public static void setBackground(final Activity tActivity){
  112. final View tActivityBackground = tActivity.findViewById(R.id.background);
  113. mListener = new ChangeListener() {
  114. @Override
  115. public void onChange() { tActivityBackground.setBackground(mBackground); }
  116. };
  117.  
  118. notifyChange();
  119. }
  120.  
  121. private static void updateBackgroud() {
  122. int tTop = Color.argb(255, mTop[0].get(), mTop[1].get(), mTop[2].get());
  123. int tBottom = Color.argb(255, mBottom[0].get(), mBottom[1].get(), mBottom[2].get());
  124. int[] colors = {tTop, tBottom};
  125. mBackground = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);
  126. mBackground.setCornerRadius(0f);
  127. notifyChange();
  128. }
  129.  
  130. public static void animateBackground(boolean tAnimate) {
  131. mAnimate.set(tAnimate);
  132. }
  133.  
  134. public static void notifyChange() {
  135. if (mListener != null) mListener.onChange();
  136. }
  137.  
  138. public interface ChangeListener {
  139. void onChange();
  140. }
Add Comment
Please, Sign In to add comment