Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added padding between img and the border #353

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class CircleImageView extends ImageView {
private static final int DEFAULT_BORDER_WIDTH = 0;
private static final int DEFAULT_BORDER_COLOR = Color.BLACK;
private static final int DEFAULT_CIRCLE_BACKGROUND_COLOR = Color.TRANSPARENT;
private static final int DEFAULT_CIRCLE_RADIUS_PADDING = 0;
private static final boolean DEFAULT_BORDER_OVERLAY = false;

private final RectF mDrawableRect = new RectF();
Expand All @@ -73,6 +74,7 @@ public class CircleImageView extends ImageView {
private BitmapShader mBitmapShader;
private int mBitmapWidth;
private int mBitmapHeight;
private int mRadiusPadding;

private float mDrawableRadius;
private float mBorderRadius;
Expand Down Expand Up @@ -103,6 +105,7 @@ public CircleImageView(Context context, AttributeSet attrs, int defStyle) {
mBorderColor = a.getColor(R.styleable.CircleImageView_civ_border_color, DEFAULT_BORDER_COLOR);
mBorderOverlay = a.getBoolean(R.styleable.CircleImageView_civ_border_overlay, DEFAULT_BORDER_OVERLAY);
mCircleBackgroundColor = a.getColor(R.styleable.CircleImageView_civ_circle_background_color, DEFAULT_CIRCLE_BACKGROUND_COLOR);
mRadiusPadding = a.getDimensionPixelSize(R.styleable.CircleImageView_civ_circle_radius_padding, DEFAULT_CIRCLE_RADIUS_PADDING);

a.recycle();

Expand Down Expand Up @@ -154,9 +157,16 @@ protected void onDraw(Canvas canvas) {
}

if (mCircleBackgroundColor != Color.TRANSPARENT) {
canvas.drawCircle(mDrawableRect.centerX(), mDrawableRect.centerY(), mDrawableRadius, mCircleBackgroundPaint);
canvas.drawCircle(mDrawableRect.centerX(), mDrawableRect.centerY(),
mDrawableRadius + (mRadiusPadding / 2.0f), mCircleBackgroundPaint);
}
canvas.drawCircle(mDrawableRect.centerX(), mDrawableRect.centerY(), mDrawableRadius, mBitmapPaint);

if (mRadiusPadding == 0)
canvas.drawCircle(mDrawableRect.centerX(), mDrawableRect.centerY(), mDrawableRadius, mBitmapPaint);
else {
canvas.drawBitmap(mBitmap, null, mDrawableRect, null);
}

if (mBorderWidth > 0) {
canvas.drawCircle(mBorderRect.centerX(), mBorderRect.centerY(), mBorderRadius, mBorderPaint);
}
Expand Down Expand Up @@ -365,7 +375,7 @@ private void setup() {
mBitmapWidth = mBitmap.getWidth();

mBorderRect.set(calculateBounds());
mBorderRadius = Math.min((mBorderRect.height() - mBorderWidth) / 2.0f, (mBorderRect.width() - mBorderWidth) / 2.0f);
mBorderRadius = Math.min(calculateBorderRadiusHeight(), calculateBorderRadiusWidth());

mDrawableRect.set(mBorderRect);
if (!mBorderOverlay && mBorderWidth > 0) {
Expand All @@ -378,8 +388,16 @@ private void setup() {
invalidate();
}

private float calculateBorderRadiusHeight() {
return (mBorderRect.height() - mBorderWidth + mRadiusPadding) / 2.0f;
}

private float calculateBorderRadiusWidth() {
return (mBorderRect.width() - mBorderWidth + mRadiusPadding) / 2.0f;
}

private RectF calculateBounds() {
int availableWidth = getWidth() - getPaddingLeft() - getPaddingRight();
int availableWidth = getWidth() - getPaddingLeft() - getPaddingRight();
int availableHeight = getHeight() - getPaddingTop() - getPaddingBottom();

int sideLength = Math.min(availableWidth, availableHeight);
Expand Down Expand Up @@ -428,7 +446,7 @@ private boolean inTouchableArea(float x, float y) {

return Math.pow(x - mBorderRect.centerX(), 2) + Math.pow(y - mBorderRect.centerY(), 2) <= Math.pow(mBorderRadius, 2);
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private class OutlineProvider extends ViewOutlineProvider {

Expand Down
1 change: 1 addition & 0 deletions circleimageview/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
<attr name="civ_border_color" format="color" />
<attr name="civ_border_overlay" format="boolean" />
<attr name="civ_circle_background_color" format="color" />
<attr name="civ_circle_radius_padding" format="dimension" />
</declare-styleable>
</resources>