java怎么给图片设置圆角
时间: 2024-02-20 14:02:03 浏览: 148
要给图片设置圆角,可以按照以下步骤进行:
1. 创建一个 Bitmap 对象,将原始图片加载进来。
2. 创建一个 BitmapShader 对象,将 Bitmap 对象作为 Shader 的图像源。
3. 创建一个 ShapeDrawable 对象,将 BitmapShader 对象作为 ShapeDrawable 的 Shader。
4. 设置 ShapeDrawable 对象的形状为圆角矩形,可以使用 RoundRectShape 类来创建。
5. 创建一个 ImageView 对象,并将 ShapeDrawable 对象设置为 ImageView 的背景。
以下是一个示例代码:
```
// 加载原始图片
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
// 创建 BitmapShader 对象
BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
// 创建 ShapeDrawable 对象
ShapeDrawable shapeDrawable = new ShapeDrawable(new RoundRectShape(new float[] { 50, 50, 50, 50, 50, 50, 50, 50 }, null, null));
shapeDrawable.getPaint().setShader(shader);
// 创建 ImageView 对象
ImageView imageView = new ImageView(this);
imageView.setBackground(shapeDrawable);
```
这个示例代码会将一张图片设置为圆角矩形,并将其显示在 ImageView 中。其中,圆角的半径为 50,可以根据需要进行调整。
阅读全文
相关推荐














