简体   繁体   English

如何以编程方式使图像角变圆

[英]How to make an image corner rounded programmatically

I am using a text view.我正在使用文本视图。 It has one image as background.它有一个图像作为背景。 How do I programmatically round this image's corner?如何以编程方式绕过此图像的角落?

Convert your image to bitmap and then convert that bitmap with rounded corners bitmap.将您的图像转换为 bitmap,然后将 bitmap 转换为圆角 bitmap。 Finally apply that bitmap to your textview background.最后将 bitmap 应用到您的 textview 背景。 The below code is for convert bitmap to rounded bitmap image.以下代码用于将 bitmap 转换为圆形 bitmap 图像。

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,int roundPixelSize) { 
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); 
    Canvas canvas = new Canvas(output); 
    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 = roundPixelSize;
    paint.setAntiAlias(true);
    canvas.drawRoundRect(rectF,roundPx,roundPx, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint); 
    return output; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM