public RoundedImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundedImageView, defStyle, 0); int index = a.getInt(R.styleable.RoundedImageView_android_scaleType, -1); if (index >= 0) { setScaleType(sScaleTypeArray[index]); } mCornerRadius = a.getDimensionPixelSize(R.styleable.RoundedImageView_corner_radius, -1); mBorderWidth = a.getDimensionPixelSize(R.styleable.RoundedImageView_border_width, -1); // don't allow negative values for radius and border if (mCornerRadius < 0) { mCornerRadius = DEFAULT_RADIUS; } if (mBorderWidth < 0) { mBorderWidth = DEFAULT_BORDER; } mBorderColor = a.getColorStateList(R.styleable.RoundedImageView_border_color); if (mBorderColor == null) { mBorderColor = ColorStateList.valueOf(RoundedDrawable.DEFAULT_BORDER_COLOR); } mRoundBackground = a.getBoolean(R.styleable.RoundedImageView_round_background, false); mOval = a.getBoolean(R.styleable.RoundedImageView_is_oval, false); if (mDrawable instanceof RoundedDrawable) { updateDrawableAttrs((RoundedDrawable) mDrawable); } if (mRoundBackground) { if (!(mBackgroundDrawable instanceof RoundedDrawable)) { // try setting background drawable now that we got the mRoundBackground param setBackgroundDrawable(mBackgroundDrawable); } if (mBackgroundDrawable instanceof RoundedDrawable) { updateDrawableAttrs((RoundedDrawable) mBackgroundDrawable); } } a.recycle(); }
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 setBackground(Drawable background) { setBackgroundDrawable(background); }