Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void changeOverTime(final MonitoredVariable<Integer> tVar, final int tTo, final long tTime, final long tUpdateFreq) {
- if (tTime < tUpdateFreq) { Log.e(TAG, "Time must be less then update freq."); }
- if (tVar == null) { Log.e(TAG, "Container cannot be null."); }
- else {
- final Thread tBackgroundThread = new Thread(new Runnable() {
- @Override
- public void run() {
- float tSteps = tTime / tUpdateFreq; // 2000/100 = 20
- float tInterval = (tTo - tVar.get()) / tSteps; // 67-175 = -108/20 = -5.4
- float tVal = tVar.get(); //175
- while (Math.round(tVal) != tTo) { //67(After 20 Times) != 67 -> FALSE
- Debug.Log(TAG, "EQ: " + Math.round(tVal) + "?=" + tTo);
- tVal += tInterval; // -5.4 * 20(Times) = -108+175 = 67
- tryToSleep(tUpdateFreq); // 100ms * 20(Times) = 2000ms total
- tVar.set(Math.round(tVal));
- }
- }
- });
- tBackgroundThread.start();
- }
- }
- private static void tryToSleep(long tTime) {
- try { sleep(tTime); }
- catch (InterruptedException e) { e.printStackTrace(); }
- }
- public class MonitoredVariable<Prototype> {
- protected Prototype mData;
- protected ChangeListener mListener;
- public MonitoredVariable(Prototype tData) {
- this(tData, null);
- }
- public MonitoredVariable(Prototype tData, ChangeListener tListener) {
- if (tListener != null) setListener(tListener);
- mData = tData;
- }
- public Prototype get() {
- return mData;
- }
- public void set(Prototype tData) {
- if (mData != tData) {
- mData = tData;
- notifyChange();
- }
- }
- public void setListener(ChangeListener tListener) {
- mListener = tListener;
- }
- public ChangeListener getListener() {
- return mListener;
- }
- public void notifyChange() {
- if (mListener != null) mListener.onChange();
- }
- public interface ChangeListener {
- void onChange();
- }
- }
- public static void init() {
- MonitoredVariable.ChangeListener tUpdateBackground = new MonitoredVariable.ChangeListener() {
- @Override
- public void onChange() { updateBackgroud();
- }
- };
- mTop = new MonitoredVariable[]{
- new MonitoredVariable<>(0, tUpdateBackground),
- new MonitoredVariable<>(0, tUpdateBackground),
- new MonitoredVariable<>(0, tUpdateBackground)
- };
- mBottom = new MonitoredVariable[]{
- new MonitoredVariable<>(0, tUpdateBackground),
- new MonitoredVariable<>(0, tUpdateBackground),
- new MonitoredVariable<>(0, tUpdateBackground)
- };
- mAnimationLoop = new Handler();
- mAnimation = new Runnable() {
- @Override
- public void run() {
- Debug.Log(TAG, "RUNNING ANIMATION");
- final Random RNG = new Random();
- for (MonitoredVariable<Integer>[] tBackground: new MonitoredVariable[][] {mTop, mBottom}) {
- for (MonitoredVariable<Integer> tColor : tBackground) {
- int tRandomColor = RNG.nextInt(255);
- //tColor.set(tRandomColor);
- Shift.changeOverTime(tColor, tRandomColor, 2000, 100);
- }
- }
- if(mAnimate.get()) {
- mAnimationLoop.postDelayed(mAnimation, 10000);
- }
- }
- };
- mAnimate = new MonitoredVariable<>(false, new MonitoredVariable.ChangeListener() {
- @Override
- public void onChange() {
- if (mAnimate.get()) mAnimationLoop.postDelayed(mAnimation, 0);
- else mAnimationLoop.removeCallbacks(mAnimation);
- }
- });
- }
- public static void setBackground(final Activity tActivity){
- final View tActivityBackground = tActivity.findViewById(R.id.background);
- mListener = new ChangeListener() {
- @Override
- public void onChange() { tActivityBackground.setBackground(mBackground); }
- };
- notifyChange();
- }
- private static void updateBackgroud() {
- int tTop = Color.argb(255, mTop[0].get(), mTop[1].get(), mTop[2].get());
- int tBottom = Color.argb(255, mBottom[0].get(), mBottom[1].get(), mBottom[2].get());
- int[] colors = {tTop, tBottom};
- mBackground = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);
- mBackground.setCornerRadius(0f);
- notifyChange();
- }
- public static void animateBackground(boolean tAnimate) {
- mAnimate.set(tAnimate);
- }
- public static void notifyChange() {
- if (mListener != null) mListener.onChange();
- }
- public interface ChangeListener {
- void onChange();
- }
Add Comment
Please, Sign In to add comment