content: /*
   * Copyright 2017 Google Inc.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package com.example.android.unsplash.ui;
  
  
  import android.content.Context;
  import android.content.res.TypedArray;
  import android.graphics.Canvas;
  import android.graphics.drawable.Drawable;
  import android.util.AttributeSet;
  import android.view.ViewOutlineProvider;
  import android.widget.ImageView;
  
  import com.example.android.unsplash.R;
  
  
  public class ForegroundImageView extends ImageView {
  
      private Drawable foreground;
  
      public ForegroundImageView(Context context, AttributeSet attrs) {
          super(context, attrs);
  
          TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ForegroundView);
  
          final Drawable d = a.getDrawable(R.styleable.ForegroundView_android_foreground);
          if (d != null) {
              setForeground(d);
          }
          a.recycle();
          setOutlineProvider(ViewOutlineProvider.BOUNDS);
      }
  
      @Override
      protected void onSizeChanged(int w, int h, int oldw, int oldh) {
          super.onSizeChanged(w, h, oldw, oldh);
          if (foreground != null) {
              foreground.setBounds(0, 0, w, h);
          }
      }
  
      @Override
      public boolean hasOverlappingRendering() {
          return false;
      }
  
      @Override
      protected boolean verifyDrawable(Drawable who) {
          return super.verifyDrawable(who) || (who == foreground);
      }
  
      @Override
      public void jumpDrawablesToCurrentState() {
          super.jumpDrawablesToCurrentState();
          if (foreground != null) foreground.jumpToCurrentState();
      }
  
      @Override
      protected void drawableStateChanged() {
          super.drawableStateChanged();
          if (foreground != null && foreground.isStateful()) {
              foreground.setState(getDrawableState());
          }
      }
  
      /**
       * Returns the drawable used as the foreground of this view. The
       * foreground drawable, if non-null, is always drawn on top of the children.
       *
       * @return A Drawable or null if no foreground was set.
       */
      public Drawable getForeground() {
          return foreground;
      }
  
      /**
       * Supply a Drawable that is to be rendered on top of the contents of this ImageView
       *
       * @param drawable The Drawable to be drawn on top of the ImageView
       */
      public void setForeground(Drawable drawable) {
          if (foreground != drawable) {
              if (foreground != null) {
                  foreground.setCallback(null);
                  unscheduleDrawable(foreground);
              }
  
              foreground = drawable;
  
              if (foreground != null) {
                  foreground.setBounds(0, 0, getWidth(), getHeight());
                  setWillNotDraw(false);
                  foreground.setCallback(this);
                  if (foreground.isStateful()) {
                      foreground.setState(getDrawableState());
                  }
              } else {
                  setWillNotDraw(true);
              }
              invalidate();
          }
      }
  
      @Override
      public void draw(Canvas canvas) {
          super.draw(canvas);
          if (foreground != null) {
              foreground.draw(canvas);
          }
      }
  
      @Override
      public void drawableHotspotChanged(float x, float y) {
          super.drawableHotspotChanged(x, y);
          if (foreground != null) {
              foreground.setHotspot(x, y);
          }
      }
  }
  
  