bitmap 上面两个圆角

本文介绍了一种在不同屏幕尺寸下实现图片顶部或底部圆角效果的方法。通过使用Bitmap、Canvas和Paint等组件,结合DisplayMetrics进行适配,确保圆角效果在各种设备上的一致性。
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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值