Example #1
0
  @Subscribe(mode = Subscribe.Mode.Background)
  public void onUpdatedAmbientLight(LightSensor sensor) {
    // calculates the new color based on the sensor values and the original color
    // the algorithm is magical
    final int newColor =
        Color.HSVToColor(GaiaUtils.computeHSV(sensor, objectToMutate, originalColor));
    int currentColor = 0;

    // mutate all the things
    Object view = objectToMutate.get();
    if (view != null) {
      if (view instanceof LinearLayout) {
        currentColor = ((ColorDrawable) ((LinearLayout) view).getBackground()).getColor();
      } else if (view instanceof TextView) {
        currentColor = ((TextView) view).getCurrentTextColor();
      } else if (view instanceof FloatingActionButton) {
        currentColor = ((FloatingActionButton) view).getBackgroundTintList().getDefaultColor();
      } else if (view instanceof FrameLayout) {
        currentColor = ((ColorDrawable) ((FrameLayout) view).getBackground()).getColor();
      }
    }

    boolean hasNewColor = currentColor != newColor;
    boolean isReady = GaiaUtils.isReady(lastUpdate);

    // we don't want to update the colors every time the sensor outputs new values. Yes, the sensor
    // is very nervous
    if (hasNewColor && isReady) {
      final int fromColor = currentColor;
      lastUpdate = System.currentTimeMillis();

      // run this on the Main Thread, this @Subscribe method runs in a background thread
      App.MAIN_THREAD.post(
          new Runnable() {
            @Override
            public void run() {
              // much animation. wow material. such beauty. so rad
              // pass it the object (in this case it's a view) that's going to be mutated,
              // the starting color value, and the color we want to animate to
              // lastly the duration of the animation, in this case the default should be 250ms
              ColorAnimationUtils.animateColor(
                  objectToMutate, fromColor, newColor, COLOR_TRANSFORM_DURATION);
            }
          });
    }
  }
Example #2
0
 private Gaia(Builder builder) {
   this.name = builder.getName();
   this.objectToMutate = builder.getMutativeView();
   this.originalColor = builder.getColorToScale();
   // tango down!
   GaiaUtils.seekAndDestroy(getName());
   // good morning!!
   App.GAIAS.add(this);
   // One Bus to rule them all, One Bus to find them,
   // One Bus to bring them all and in the darkness bind them
   App.BUS.register(this);
 }