Java Android:如何以编程方式只对位图的上角进行圆角?
我目前正在使用以下代码:Java Android:如何以编程方式只对位图的上角进行圆角?,java,android,bitmap,Java,Android,Bitmap,我目前正在使用以下代码: @Override public Bitmap transform(Bitmap source) { Bitmap result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(result); final int color = 0xff424242; final Pai
@Override
public Bitmap transform(Bitmap source) {
Bitmap result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(result);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, source.getWidth(),source.getHeight());
final RectF rectF = new RectF(rect);
final float scale = context.getResources().getDisplayMetrics().density;
final float roundDp = 10 * scale;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundDp, roundDp, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(source, rect, rect, paint);
source.recycle();
return result;
}
但问题是,这种方法只允许我一次修改所有4个角点。如何仅使图像的底角圆角 以下是您的答案:
以及onDraw函数:
@Override
protected void onDraw(Canvas canvas) {
//super.onDraw(canvas);
Drawable drawable = getDrawable();
Bitmap b = ((BitmapDrawable)drawable).getBitmap() ;
Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
int w = getWidth(), h = getHeight();
Bitmap roundBitmap = CropImageView.getRoundedCornerBitmap( getContext(), bitmap,10 , w, h , true, false,true, false);
canvas.drawBitmap(roundBitmap, 0,0 , null);
}
Igor,那必须是编写
getRoundedCornerBitmap
的类。这只是一个静态方法调用。让我开心!非常感谢。
@Override
protected void onDraw(Canvas canvas) {
//super.onDraw(canvas);
Drawable drawable = getDrawable();
Bitmap b = ((BitmapDrawable)drawable).getBitmap() ;
Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
int w = getWidth(), h = getHeight();
Bitmap roundBitmap = CropImageView.getRoundedCornerBitmap( getContext(), bitmap,10 , w, h , true, false,true, false);
canvas.drawBitmap(roundBitmap, 0,0 , null);
}