java将图片变成圆角_如何制作带有圆角的ImageView?

在Android中,默认情况下,ImageView是一个矩形。 如何在ImageView中使其成为一个圆角矩形(将Bitmap的所有4个角都切掉为圆角矩形)?

#1楼

非常感谢您首先回答。 这是修改后的版本,可将矩形图像转换为正方形图像(并四舍五入),并通过填充颜色作为参数。

public static Bitmap getRoundedBitmap(Bitmap bitmap, int pixels, int color) {

Bitmap inpBitmap = bitmap;

int width = 0;

int height = 0;

width = inpBitmap.getWidth();

height = inpBitmap.getHeight();

if (width <= height) {

height = width;

} else {

width = height;

}

Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);

Canvas canvas = new Canvas(output);

final Paint paint = new Paint();

final Rect rect = new Rect(0, 0, width, height);

final RectF rectF = new RectF(rect);

final float roundPx = pixels;

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(inpBitmap, rect, rect, paint);

return output;

}

#2楼

为什么不对draw()进行裁剪?

这是我的解决方案:

通过裁剪扩展RelativeLayout

将ImageView(或其他视图)放入布局中:

码:

public class RoundRelativeLayout extends RelativeLayout {

private final float radius;

public RoundRelativeLayout(Context context, AttributeSet attrs) {

super(context, attrs);

TypedArray attrArray = context.obtainStyledAttributes(attrs,

R.styleable.RoundRelativeLayout);

radius = attrArray.getDimension(

R.styleable.RoundRelativeLayout_radius, 0);

}

private boolean isPathValid;

private final Path path = new Path();

private Path getRoundRectPath() {

if (isPathValid) {

return path;

}

path.reset();

int width = getWidth();

int height = getHeight();

RectF bounds = new RectF(0, 0, width, height);

path.addRoundRect(bounds, radius, radius, Direction.CCW);

isPathValid = true;

return path;

}

@Override

protected void dispatchDraw(Canvas canvas) {

canvas.clipPath(getRoundRectPath());

super.dispatchDraw(canvas);

}

@Override

public void draw(Canvas canvas) {

canvas.clipPath(getRoundRectPath());

super.draw(canvas);

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

int oldWidth = getMeasuredWidth();

int oldHeight = getMeasuredHeight();

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

int newWidth = getMeasuredWidth();

int newHeight = getMeasuredHeight();

if (newWidth != oldWidth || newHeight != oldHeight) {

isPathValid = false;

}

}

}

#3楼

虽然上述答案有效,但Romain Guy(Android的核心开发人员)在他的博客中展示了一种更好的方法 ,该方法通过使用着色器而不创建位图的副本来使用更少的内存。 该功能的基本要点如下:

BitmapShader shader;

shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);

Paint paint = new Paint();

paint.setAntiAlias(true);

paint.setShader(shader);

RectF rect = new RectF(0.0f, 0.0f, width, height);

// rect contains the bounds of the shape

// radius is the radius in pixels of the rounded corners

// paint contains the shader that will texture the shape

canvas.drawRoundRect(rect, radius, radius, paint);

与其他方法相比,它的优点是:

不会创建位图的单独副本,因为它会占用大量内存和大图像[与此处的大多数其他答案相比]

支持抗锯齿 [vs clipPath方法]

支持alpha [vs xfermode + porterduff方法]

支持硬件加速 [vs clipPath方法]

只能在画布上绘制一次 [vs xfermode和clippath方法]

我基于此代码创建了RoundedImageView ,该代码将该逻辑包装到ImageView中,并添加了适当的ScaleType支持和可选的圆角边框。

#4楼

罗曼·盖(Romain Guy)就在这里。

缩小版如下。

Bitmap bitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.image)).getBitmap();

Bitmap bitmapRounded = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());

Canvas canvas = new Canvas(bitmapRounded);

Paint paint = new Paint();

paint.setAntiAlias(true);

paint.setShader(new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));

canvas.drawRoundRect((new RectF(0.0f, 0.0f, bitmap.getWidth(), bitmap.getHeight())), 10, 10, paint);

imageView.setImageBitmap(bitmapRounded);

#5楼

将形状应用于imageView ,如下所示:

android:shape="rectangle" >

android:width="1dp"

android:color="#808080" />

android:bottom="5dp"

android:left="5dp"

android:right="5dp"

android:top="5dp" />

这可能对您的朋友有帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值