title: programmatically darken a View android
body: <p>I found how to <a href="https://stackoverflow.com/questions/2838757/how-to-set-opacity-alpha-for-view-in-android">change the opacity of a <code>View</code></a>, but I need to actually darken a <code>View</code>.  My best idea is to put a transparent black rectangle over it and then slowly increase the opacity of the rectangle.</p>
  
  <p>Do you know a nicer way to do it?</p>
  
  <pre><code>public class Page07AnimationView extends ParentPageAnimationView {
      private final String TAG = this.getClass().getSimpleName();
      private ImageView overlay;
      private int mAlpha = 0;
  
      public Page07AnimationView(Context context) {
          super(context);
      }
  
      public Page07AnimationView(Context context, AttributeSet attrs) {
          super(context, attrs);
      }
  
      protected void init()
      {
          overlay = new ImageView(mContext);
          overlay.setImageResource(R.drawable.black_background);
          overlay.setAlpha(0);
          overlay.setWillNotDraw(false);
          // make the PageAniSurfaceView focusable so it can handle events
          setFocusable(true);
      }
  
      protected void draw_bitmaps(Canvas canvas)
      {
          overlay.draw(canvas);
          update_bitmaps();
          invalidate();
      }
  
      public void update_bitmaps()
      {
          if(mAlpha &lt; 250)
          {
              mAlpha += 10;
              overlay.setAlpha(mAlpha);
          }
      }
  }
  </code></pre>
  
  <p>The code above isn't doing what I had hoped.  <code>Page07AnimationView</code> is added to a <code>FrameLayout</code> over the view I need to darken.   <code>R.drawable.black_background</code> points to a 787px x 492px black png image.</p>
  
  <p>I added <code>overlay.setWillNotDraw(false);</code> but it didn't help.
  I changed the first <code>setAlpha(0)</code> to <code>setAlpha(255)</code> but that didn't help.
  I removed the <code>setAlpha()</code> calls altogether, but it didn't help.</p>
  
  <p>This basic technique of adding a <code>PageNNAnimationView</code> has been working to draw <code>Bitmaps</code>, but not to draw <code>ImageView overlay</code>.  (I would use <code>Bitmaps</code>, but they don't seem to have an alpha component.)</p>
  
  <p>Edit2: this is the parent of the class above:</p>
  
  <pre><code>public class ParentPageAnimationView extends View {
      private final String TAG = this.getClass().getSimpleName();
      protected Context mContext;
  
      public ParentPageAnimationView(Context context) {
          super(context);
          mContext = context;
          init();
      }
  
      public ParentPageAnimationView(Context context, AttributeSet attrs) {
          super(context, attrs);
          mContext = context;
          init();
      }
  
      protected void init()
      {
      }
  
      protected void draw_bitmaps(Canvas canvas)
      {
          // will be overridden by child classes
      }
  
      @Override
      protected void onDraw(Canvas canvas) {
          if(this.getVisibility() == View.VISIBLE)
          {
              if(canvas != null)
              {
                  draw_bitmaps(canvas);
              }
          }
      }
  
      public void update_bitmaps() 
      {
          // will be overridden by child classes
      }
  
      public void elementStarted(PageElement _pageElement) {
          // Nothing in parent class
      }
  
      public void elementFinished(PageElement mElement) {
          // Nothing in parent class
      }
  }
  </code></pre>