我想在我的应用程序中添加ImageView
,显示一张带有圆形叠加层的照片,以便它显示为圆形而不是方形。但是,当我应用圆形叠加时,圆的边缘会像素化。如何更改我的代码,以便圆圈的边缘不会如此锯齿。
这是我目前代码的一个例子......
这是我的代码......
public Bitmap getRoundedShape(Bitmap bitmap) {
int targetWidth = 100;
int targetHeight = 100;
Bitmap targetBitmap = Bitmap.createBitmap(targetWidth,
targetHeight,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(targetBitmap);
Path path = new Path();
path.addCircle(((float) targetWidth - 1) / 2,
((float) targetHeight - 1) / 2,
(Math.min(((float) targetWidth),
((float) targetHeight)) / 2),
Path.Direction.CCW);
canvas.clipPath(path);
Bitmap sourceBitmap = bitmap;
canvas.drawBitmap(sourceBitmap,
new Rect(0, 0, sourceBitmap.getWidth(),
sourceBitmap.getHeight()),
new Rect(0, 0, targetWidth, targetHeight), null);
return targetBitmap;
}
xml border
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke android:width="2dp" android:color="#fff" />
<solid android:color="@android:color/white"/>
答案 0 :(得分:0)
这是我的代码,对我来说很有用(对于Drawable
s):
// From Drawable
final RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(ApplicationHelper.resources(), bitmap);
roundedBitmapDrawable.setCornerRadius(Math.min(roundedBitmapDrawable.getMinimumWidth(), roundedBitmapDrawable.getMinimumHeight()) / 2.0F);
return roundedBitmapDrawable;
这是旧版本(适用于Bitmap
s):
// From Bitmap
final Bitmap circleBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
final BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
final Paint paint = new Paint();
paint.setShader(shader);
final Canvas canvas = new Canvas(circleBitmap)
canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);
bitmap.recycle();
return circleBitmap;
试一试;)
答案 1 :(得分:0)
为什么不使用许多可用库中的一个? 只需谷歌圆形图像视图,或圆形图像视图,你就会找到你需要的东西。
我一直在使用RoundedImageView。它有我所需要的一切。 =)
如果Apache许可证不适合您,也许可以选择 CircularImageView
答案 2 :(得分:0)
绘制圆形时需要设置“消除锯齿” 添加/更改您的代码:
{{1}}