concept dimmer in category android

appears as: dimmer
RxJava for Android Developers

This is an excerpt from Manning's book RxJava for Android Developers.

Coding the dimmer

You could choose from a few approaches to code the dimmer, but you’ll use a simple solution here and directly use the openRatio exposed from the view model. You have to make a little adjustment to it because the setAlpha function takes an integer between 0 and 255.

// Retrieve the views we want to use
FanView fanView = (FanView) findViewById(R.id.fan_view);
View dimmerView = findViewById(R.id.dimmer);

// Make binding for the actual fan
fanViewModel
  .getOpenRatio()                                     #1
  .observeOn(AndroidSchedulers.mainThread())
  .subscribe(fanView::setOpenRatio);

// Make binding for the dimmer
fanViewModel
   .getOpenRatio()                                    #2
   .map(ratio -> (int) (64f * ratio))                 #3
   .observeOn(AndroidSchedulers.mainThread())
   .subscribe(dimmerView.getBackground()::setAlpha);  #4