private void updateDrawableAttrs(RoundedDrawable drawable) {
   drawable.setScaleType(mScaleType);
   drawable.setCornerRadius(mCornerRadius);
   drawable.setBorderWidth(mBorderWidth);
   drawable.setBorderColors(mBorderColor);
   drawable.setOval(mOval);
 }
 public void setOval(boolean oval) {
   mOval = oval;
   if (mDrawable instanceof RoundedDrawable) {
     ((RoundedDrawable) mDrawable).setOval(oval);
   }
   if (mRoundBackground && mBackgroundDrawable instanceof RoundedDrawable) {
     ((RoundedDrawable) mBackgroundDrawable).setOval(oval);
   }
   invalidate();
 }
  public void setCornerRadius(int radius) {
    if (mCornerRadius == radius) {
      return;
    }

    mCornerRadius = radius;
    if (mDrawable instanceof RoundedDrawable) {
      ((RoundedDrawable) mDrawable).setCornerRadius(radius);
    }
    if (mRoundBackground && mBackgroundDrawable instanceof RoundedDrawable) {
      ((RoundedDrawable) mBackgroundDrawable).setCornerRadius(radius);
    }
  }
  public void setBorderWidth(int width) {
    if (mBorderWidth == width) {
      return;
    }

    mBorderWidth = width;
    if (mDrawable instanceof RoundedDrawable) {
      ((RoundedDrawable) mDrawable).setBorderWidth(width);
    }
    if (mRoundBackground && mBackgroundDrawable instanceof RoundedDrawable) {
      ((RoundedDrawable) mBackgroundDrawable).setBorderWidth(width);
    }
    invalidate();
  }
 @Override
 public void setImageBitmap(Bitmap bm) {
   mResource = 0;
   mDrawable = RoundedDrawable.fromBitmap(bm);
   updateDrawableAttrs();
   super.setImageDrawable(mDrawable);
 }
 @Override
 public void setImageDrawable(Drawable drawable) {
   mResource = 0;
   mDrawable = RoundedDrawable.fromDrawable(drawable);
   updateDrawableAttrs();
   super.setImageDrawable(mDrawable);
 }
Example #7
0
  private void updateBackgroundDrawableAttrs(boolean convert) {

    if (convert) {
      mBackgroundDrawable = RoundedDrawable.fromDrawable(mBackgroundDrawable);
    }
    updateAttrs(mBackgroundDrawable);
  }
  public void setBorderColors(ColorStateList colors) {
    if (mBorderColor.equals(colors)) {
      return;
    }

    mBorderColor =
        colors != null ? colors : ColorStateList.valueOf(RoundedDrawable.DEFAULT_BORDER_COLOR);
    if (mDrawable instanceof RoundedDrawable) {
      ((RoundedDrawable) mDrawable).setBorderColors(colors);
    }
    if (mRoundBackground && mBackgroundDrawable instanceof RoundedDrawable) {
      ((RoundedDrawable) mBackgroundDrawable).setBorderColors(colors);
    }
    if (mBorderWidth > 0) {
      invalidate();
    }
  }
Example #9
0
  private void updateAttrs(Drawable drawable) {
    if (drawable == null) {
      return;
    }

    if (drawable instanceof RoundedDrawable) {
      ((RoundedDrawable) drawable).setScaleType(mScaleType);
      ((RoundedDrawable) drawable).setCornerRadius(mCornerRadius);

      mDrawable.setColorFilter(mColorFilter);
    } else if (drawable instanceof LayerDrawable) {
      // loop through layers to and set drawable attrs
      LayerDrawable ld = ((LayerDrawable) drawable);
      for (int i = 0, layers = ld.getNumberOfLayers(); i < layers; i++) {
        updateAttrs(ld.getDrawable(i));
      }
    }
  }
  public void setRoundBackground(boolean roundBackground) {
    if (mRoundBackground == roundBackground) {
      return;
    }

    mRoundBackground = roundBackground;
    if (roundBackground) {
      if (mBackgroundDrawable instanceof RoundedDrawable) {
        updateDrawableAttrs((RoundedDrawable) mBackgroundDrawable);
      } else {
        setBackgroundDrawable(mBackgroundDrawable);
      }
    } else if (mBackgroundDrawable instanceof RoundedDrawable) {
      ((RoundedDrawable) mBackgroundDrawable).setBorderWidth(0);
      ((RoundedDrawable) mBackgroundDrawable).setCornerRadius(0);
    }

    invalidate();
  }
 @Override
 public void setImageDrawable(Drawable drawable) {
   if (drawable != null) {
     mDrawable =
         RoundedDrawable.fromDrawable(drawable, mCornerRadius, mBorderWidth, mBorderColor, mOval);
     updateDrawableAttrs((RoundedDrawable) mDrawable);
   } else {
     mDrawable = null;
   }
   super.setImageDrawable(mDrawable);
 }
 @Override
 @Deprecated
 public void setBackgroundDrawable(Drawable background) {
   if (mRoundBackground && background != null) {
     mBackgroundDrawable =
         RoundedDrawable.fromDrawable(
             background, mCornerRadius, mBorderWidth, mBorderColor, mOval);
     updateDrawableAttrs((RoundedDrawable) mBackgroundDrawable);
   } else {
     mBackgroundDrawable = background;
   }
   super.setBackgroundDrawable(mBackgroundDrawable);
 }
  /**
   * Controls how the image should be resized or moved to match the size of this ImageView.
   *
   * @param scaleType The desired scaling mode.
   * @attr ref android.R.styleable#ImageView_scaleType
   */
  @Override
  public void setScaleType(ScaleType scaleType) {
    if (scaleType == null) {
      throw new NullPointerException();
    }

    if (mScaleType != scaleType) {
      mScaleType = scaleType;

      switch (scaleType) {
        case CENTER:
        case CENTER_CROP:
        case CENTER_INSIDE:
        case FIT_CENTER:
        case FIT_START:
        case FIT_END:
        case FIT_XY:
          super.setScaleType(ScaleType.FIT_XY);
          break;
        default:
          super.setScaleType(scaleType);
          break;
      }

      if (mDrawable instanceof RoundedDrawable
          && ((RoundedDrawable) mDrawable).getScaleType() != scaleType) {
        ((RoundedDrawable) mDrawable).setScaleType(scaleType);
      }

      if (mBackgroundDrawable instanceof RoundedDrawable
          && ((RoundedDrawable) mBackgroundDrawable).getScaleType() != scaleType) {
        ((RoundedDrawable) mBackgroundDrawable).setScaleType(scaleType);
      }
      setWillNotCacheDrawing(true);
      requestLayout();
      invalidate();
    }
  }
  private Drawable resolveResource() {
    Resources rsrc = getResources();
    if (rsrc == null) {
      return null;
    }

    Drawable d = null;

    if (mResource != 0) {
      try {
        d = rsrc.getDrawable(mResource);
      } catch (Exception e) {
        Log.w(TAG, "Unable to find resource: " + mResource, e);
        // Don't try again.
        mResource = 0;
      }
    }
    return RoundedDrawable.fromDrawable(d);
  }
  private void updateAttrs(Drawable drawable) {
    if (drawable == null) {
      return;
    }

    if (drawable instanceof RoundedDrawable) {
      ((RoundedDrawable) drawable)
          .setScaleType(mScaleType)
          .setCornerRadius(cornerRadius)
          .setBorderWidth(borderWidth)
          .setBorderColor(borderColor)
          .setOval(isOval);
    } else if (drawable instanceof LayerDrawable) {
      // loop through layers to and set drawable attrs
      LayerDrawable ld = ((LayerDrawable) drawable);
      for (int i = 0, layers = ld.getNumberOfLayers(); i < layers; i++) {
        updateAttrs(ld.getDrawable(i));
      }
    }
  }