bitmap 上面两个圆角

public Bitmap getTopRoundedCorner(Bitmap bitmap, DisplayMetrics metrics) {


// Using this so it scales with different screen sizes
double dH = (metrics.heightPixels / 100.0) * 3;
int iHeight = (int) dH;


// Subtract this from bitmap height
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight() - iHeight, 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());


// Again used so it scales with diff screen sizes
// Can play around with this value, depending on how rounded you wanted
// the corner
dH = (metrics.heightPixels / 100.0) * 3.5;
iHeight = (int) dH;


final RectF rectF = new RectF(rect);
final float roundPx = iHeight;


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;
}


public Bitmap getBottomRoundedCorner(Bitmap bitmap, DisplayMetrics metrics) {
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);


// Again play around with this to get the rounded value you require
double dH = (metrics.heightPixels / 100.0) * 2.5;
int iHeight = (int) dH;
final float roundPx = iHeight;


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);


// Draw second rectangle over the top of the first one
// So it hides the top rounded corners
iHeight = (int) dH;


final int color2 = 0xff424242;
final Paint paint2 = new Paint();
Canvas canvas2 = new Canvas(output);
final Rect testRect = new Rect(0, 0, bitmap.getWidth(),
bitmap.getHeight() - iHeight);
final RectF testF = new RectF(testRect);


paint2.setAntiAlias(true);
canvas2.drawARGB(0, 0, 0, 0);
paint2.setColor(color2);
canvas2.drawRoundRect(testF, roundPx, roundPx, paint2);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas2.drawBitmap(bitmap, testRect, testRect, paint2);
return output;
}

你可能感兴趣的:(bitmap 上面两个圆角)