Skip to content

Commit

Permalink
Add the possibility to round only specific corners (only for RoundedD…
Browse files Browse the repository at this point in the history
…rawable).
  • Loading branch information
Josue Zarzosa committed Apr 10, 2015
1 parent 9718837 commit 8a004ad
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 2 deletions.
4 changes: 4 additions & 0 deletions example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ android {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}

lintOptions {
abortOnError false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public PicassoAdapter(Context context) {
.borderColor(Color.BLACK)
.borderWidthDp(3)
.cornerRadiusDp(30)
.roundTopRight(false)
.roundBottomLeft(false)
.oval(false)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public class RoundedDrawable extends Drawable {
private ColorStateList mBorderColor = ColorStateList.valueOf(DEFAULT_BORDER_COLOR);
private ScaleType mScaleType = ScaleType.FIT_CENTER;

private boolean mRoundTopLeft = true;
private boolean mRoundBottomLeft = true;
private boolean mRoundTopRight = true;
private boolean mRoundBottomRight = true;

public RoundedDrawable(Bitmap bitmap) {
mBitmap = bitmap;

Expand Down Expand Up @@ -278,14 +283,76 @@ public void draw(Canvas canvas) {
} else {
if (mBorderWidth > 0) {
canvas.drawRoundRect(mDrawableRect, Math.max(mCornerRadius, 0),
Math.max(mCornerRadius, 0), mBitmapPaint);
Math.max(mCornerRadius, 0), mBitmapPaint);
canvas.drawRoundRect(mBorderRect, mCornerRadius, mCornerRadius, mBorderPaint);
redrawBitmapForSquareCorners(canvas);
redrawBorderForSquareCorners(canvas);
} else {
canvas.drawRoundRect(mDrawableRect, mCornerRadius, mCornerRadius, mBitmapPaint);
redrawBitmapForSquareCorners(canvas);
}
}
}

private void redrawBitmapForSquareCorners(Canvas canvas) {
if (mRoundBottomLeft && mRoundBottomRight && mRoundTopRight && mRoundTopLeft) {
return; // no square corners, avoid full redraw
}

int width = (int) mDrawableRect.width();
int height = (int) mDrawableRect.height();
int halfWidth = width / 2;
int halfHeight = height / 2;

if (!mRoundTopLeft) {
canvas.drawRect(new Rect(0, 0, halfWidth, halfHeight), mBitmapPaint);
}

if (!mRoundTopRight) {
canvas.drawRect(new Rect(halfWidth, 0, width, halfHeight), mBitmapPaint);
}

if (!mRoundBottomLeft) {
canvas.drawRect(new Rect(0, halfHeight, halfWidth, height), mBitmapPaint);
}

if (!mRoundBottomRight) {
canvas.drawRect(new Rect(halfWidth, halfHeight, width, height), mBitmapPaint);
}
}

private void redrawBorderForSquareCorners(Canvas canvas) {
if (mRoundBottomLeft && mRoundBottomRight && mRoundTopRight && mRoundTopLeft) {
return; // no square corners, avoid full redraw
}

int width = (int) mDrawableRect.width();
int height = (int) mDrawableRect.height();
int halfWidth = width / 2;
int halfHeight = height / 2;
float borderOffset = mBorderWidth / 2;

if (!mRoundTopLeft) {
canvas.drawLine(0, borderOffset, halfWidth + mBorderWidth, borderOffset, mBorderPaint);
canvas.drawLine(borderOffset, 0, borderOffset, halfHeight + mBorderWidth, mBorderPaint);
}

if (!mRoundTopRight) {
canvas.drawLine(halfWidth, borderOffset, width + mBorderWidth, borderOffset, mBorderPaint);
canvas.drawLine(width + borderOffset, 0, width + borderOffset, halfHeight + mBorderWidth, mBorderPaint);
}

if (!mRoundBottomLeft) {
canvas.drawLine(0, height + borderOffset, halfWidth + mBorderWidth, height + borderOffset, mBorderPaint);
canvas.drawLine(borderOffset, halfHeight, borderOffset, height + mBorderWidth, mBorderPaint);
}

if (!mRoundBottomRight) {
canvas.drawLine(halfWidth, height + borderOffset, width + mBorderWidth, height + borderOffset, mBorderPaint);
canvas.drawLine(width + borderOffset, halfHeight, width + borderOffset, height + mBorderWidth, mBorderPaint);
}
}

@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
Expand Down Expand Up @@ -340,6 +407,26 @@ public RoundedDrawable setCornerRadius(float radius) {
return this;
}

public RoundedDrawable setRoundTopRight(boolean roundTopRight) {
mRoundTopRight = roundTopRight;
return this;
}

public RoundedDrawable setRoundTopLeft(boolean roundTopLeft) {
mRoundTopLeft = roundTopLeft;
return this;
}

public RoundedDrawable setRoundBottomRight(boolean roundBottomRight) {
mRoundBottomRight = roundBottomRight;
return this;
}

public RoundedDrawable setRoundBottomLeft(boolean roundBottomLeft) {
mRoundBottomLeft = roundBottomLeft;
return this;
}

public float getBorderWidth() {
return mBorderWidth;
}
Expand Down Expand Up @@ -418,7 +505,7 @@ public RoundedDrawable setTileModeY(Shader.TileMode tileModeY) {
return this;
}

public Bitmap toBitmap() {
public Bitmap toBitmap() {
return drawableToBitmap(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public final class RoundedTransformationBuilder {
private final DisplayMetrics mDisplayMetrics;

private float mCornerRadius = 0;
private boolean mRoundTopLeft = true;
private boolean mRoundBottomLeft = true;
private boolean mRoundTopRight = true;
private boolean mRoundBottomRight = true;
private boolean mOval = false;
private float mBorderWidth = 0;
private ColorStateList mBorderColor =
Expand All @@ -53,6 +57,38 @@ public RoundedTransformationBuilder cornerRadius(float radiusPx) {
return this;
}

/**
* set whether the top left corner should be rounded (default is true)
*/
public RoundedTransformationBuilder roundTopLeft(boolean roundTopLeft) {
mRoundTopLeft = roundTopLeft;
return this;
}

/**
* set whether the top right corner should be rounded (default is true)
*/
public RoundedTransformationBuilder roundTopRight(boolean roundTopRight) {
mRoundTopRight = roundTopRight;
return this;
}

/**
* set whether the bottom left corner should be rounded (default is true)
*/
public RoundedTransformationBuilder roundBottomLeft(boolean roundBottomLeft) {
mRoundBottomLeft = roundBottomLeft;
return this;
}

/**
* set whether the bottom right corner should be rounded (default is true)
*/
public RoundedTransformationBuilder roundBottomRight(boolean roundBottomRight) {
mRoundBottomRight = roundBottomRight;
return this;
}

/**
* set corner radius in dip
*/
Expand Down Expand Up @@ -102,6 +138,10 @@ public Transformation build() {
Bitmap transformed = RoundedDrawable.fromBitmap(source)
.setScaleType(mScaleType)
.setCornerRadius(mCornerRadius)
.setRoundTopRight(mRoundTopRight)
.setRoundTopLeft(mRoundTopLeft)
.setRoundBottomLeft(mRoundBottomLeft)
.setRoundBottomRight(mRoundBottomRight)
.setBorderWidth(mBorderWidth)
.setBorderColor(mBorderColor)
.setOval(mOval)
Expand Down
Binary file added screenshot-round-specific-corners.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8a004ad

Please sign in to comment.