Rounding ONE Image Corner Only - Not All Four

I use this very general class to round corners:

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap .getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(rect); final float roundPx = pixels; paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; } 

I would like to change it to round only the upper left corner. I can not find the parameter in the code that does this? Can anyone help?

+6
source share
5 answers

This is probably not the most efficient way to do this, but you can fill in the rounded corners by drawing the top of your current mask. You can start with the current code and then use canvas.drawRect (immediately after calling canvas.drawRoundRect ) in the respective regions (corners). I assume that something like this will only cover the upper left corner:

 public static Bitmap getRoundedTopLeftCornerBitmap(Bitmap bitmap, int pixels) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap .getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(rect); final float roundPx = pixels; final Rect topRightRect = new Rect(bitmap.getWidth()/2, 0, bitmap.getWidth(), bitmap.getHeight()/2); final Rect bottomRect = new Rect(0, bitmap.getHeight()/2, bitmap.getWidth(), bitmap.getHeight()); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); // Fill in upper right corner canvas.drawRect(topRightRect, paint); // Fill in bottom corners canvas.drawRect(bottomRect, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; } 

There's a bit of optimization you could do here if you like it, but I think it should work. The general idea definitely should. I have not tried this code, although it will not look correct if pixels > bitmap.getWidth()/2 or pixels > bitmap.getHeight()/2 . Again, this probably was before.

+5
source
 public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int topLeftX, int topLeftY, int topRightX, int topRightY, int bottomRightX, int bottomRightY, int bottomLeftX, int bottomLeftY) { final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); // the float array passed to this function defines the x/y values of the corners // it starts top-left and works clockwise // so top-left-x, top-left-y, top-right-x etc RoundRectShape rrs = new RoundRectShape(new float[]{topLeftX, topLeftY, topRightX, topRightY, bottomRightX, bottomRightY, bottomLeftX, bottomLeftY}, null, null); canvas.drawARGB(0, 0, 0, 0); paint.setAntiAlias(true); paint.setColor(0xFF000000); rrs.resize(bitmap.getWidth(), bitmap.getHeight()); rrs.draw(canvas, paint); paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; } 

Or you can see the source code of RoundRects.java - an example that shows how to create the round corners available in the SDK samples: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google. android / android-apps / 4.3_r2.1 / com / example / android / apis / graphics / RoundRects.java /

+4
source

this is for selected angles:

 public static Bitmap getRoundedCornerBitmap(Context context, Bitmap bitmap, float roundDip, boolean roundTL, boolean roundTR, boolean roundBL, boolean roundBR) { try { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(rect); final float roundPx = convertDipToPixel(roundDip, context); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint);// draw round // 4Corner if (!roundTL) { Rect rectTL = new Rect(0, 0, bitmap.getWidth() / 2, bitmap.getHeight() / 2); canvas.drawRect(rectTL, paint); } if (!roundTR) { Rect rectTR = new Rect(bitmap.getWidth() / 2, 0, bitmap.getWidth(), bitmap.getHeight() / 2); canvas.drawRect(rectTR, paint); } if (!roundBR) { Rect rectBR = new Rect(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth(), bitmap.getHeight()); canvas.drawRect(rectBR, paint); } if (!roundBL) { Rect rectBL = new Rect(0, bitmap.getHeight() / 2, bitmap.getWidth() / 2, bitmap.getHeight()); canvas.drawRect(rectBL, paint); } paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; } catch (Exception e) { } return bitmap; } 
+2
source

This is more of a conceptual answer, but can you draw a rounded rectangle and then put two normal rectangles on top of the corner you want to round?

Visual example

+1
source

If you need to draw (on canvas) a round rectangle with different radii for different angles, you can use this:

 private void drawAsymmetricRoundRect(Canvas canvas, RectF rectF, float[] radii, Paint paint) { float topLeftX = rectF.left + radii[0]; float topLeftY = rectF.top + radii[0]; float topRightX = rectF.right - radii[1]; float topRightY = rectF.top + radii[1]; float bottomRightX = rectF.right - radii[2]; float bottomRightY = rectF.bottom - radii[2]; float bottomLeftY = rectF.bottom - radii[3]; float bottomLeftX = rectF.left + radii[3]; RectF topLeftCorner = new RectF(rectF.left, rectF.top, topLeftX + radii[0], topLeftY + radii[0]); RectF topRightCorner = new RectF(topRightX - radii[1], rectF.top, rectF.right, topRightY + radii[1]); RectF bottomRightCorner = new RectF(bottomRightX - radii[2], bottomRightY - radii[2], rectF.right, rectF.bottom); RectF bottomLeftCorner = new RectF(rectF.left, bottomLeftY - radii[3], bottomLeftX + radii[3], rectF.bottom); canvas.drawArc(topLeftCorner, 180, 90, true, paint); canvas.drawArc(topRightCorner, 270, 90, true, paint); canvas.drawArc(bottomRightCorner, 0, 90, true, paint); canvas.drawArc(bottomLeftCorner, 90, 90, true, paint); canvas.drawRect(topLeftX, rectF.top, topRightX, bottomLeftY < bottomRightY ? bottomLeftY : bottomRightY, paint); //top rect canvas.drawRect(topLeftX > bottomLeftX ? topLeftX : bottomLeftX, topRightY, rectF.right, bottomRightY, paint); //right rect canvas.drawRect(bottomLeftX, topLeftY > topRightY ? topLeftY : topRightY, bottomRightX, rectF.bottom, paint); //bottom rect canvas.drawRect(rectF.left, topLeftY, bottomRightX < topRightX ? bottomRightX : topRightX, bottomLeftY, paint); //left rect } 

float[] radii - an array with a floating point (length = 4), which stores the sizes of the radii of your corners (clockwise, starting from the top left corner => {topLeft, topRight, bottomRight, bottomLeft} ).

Basically this approach draws 4 arcs (corners) and fills everything between these corners with four rectangles.

IMPORTANT NOTE: I put RectFs corner RectFs in this method to reduce the complexity of the posted code. Because you are likely to call this method from your onDraw() method, you should extract this piece of code and place it where you run another Rects (unless you initialize them in onDraw() : P).

0
source

Source: https://habr.com/ru/post/947792/


All Articles