如何以编程方式使图像角变圆 [英] How to make an image corner rounded programmatically

查看:36
本文介绍了如何以编程方式使图像角变圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用文本视图.它有一个图像作为背景.如何以编程方式绕过此图像的角?

I am using a text view. It has one image as background. How do I programmatically round this image's corner?

推荐答案

将您的图像转换为位图,然后将该位图转换为圆角位图.最后将该位图应用于您的 textview 背景.以下代码用于将位图转换为圆角位图图像.

Convert your image to bitmap and then convert that bitmap with rounded corners bitmap. Finally apply that bitmap to your textview background. The below code is for convert bitmap to rounded bitmap image.

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

这篇关于如何以编程方式使图像角变圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆