Android 如何使用 RoundedBitmapDrawable
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24878740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use RoundedBitmapDrawable
提问by gian1200
Has anyone managed to use RoundedBitmapDrawable
? Correct me if I'm wrong, but to my understanding, it makes a circular image from a regular rectangular image.
有没有人设法使用RoundedBitmapDrawable
?如果我错了,请纠正我,但据我所知,它从常规矩形图像生成圆形图像。
What I've tried so far is this
到目前为止我尝试过的是这个
RoundedBitmapDrawable.createRoundedBitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), iconResource))
What I tried to achieve: transform any image to a circular image and show it using an ImageView.
我试图实现的目标:将任何图像转换为圆形图像并使用 ImageView 显示它。
In case I mixed things up and all that I said is non-sense. Is it possible (or simpler) to do it with any of the new framework? (Android L or new Support Library)
万一我把事情搞混了,我说的都是胡说八道。是否有可能(或更简单)使用任何新框架来做到这一点?(Android L 或新的支持库)
回答by alanv
You need to set the corner radius.
您需要设置拐角半径。
Resources res = getResources();
Bitmap src = BitmapFactory.decodeResource(res, iconResource);
RoundedBitmapDrawable dr =
RoundedBitmapDrawableFactory.create(res, src);
dr.setCornerRadius(Math.max(src.getWidth(), src.getHeight()) / 2.0f);
imageView.setImageDrawable(dr);
回答by Vamsi Smart
It may be a late reply but i hope that it will be helpful to others
这可能是一个迟到的回复,但我希望它会对其他人有所帮助
If your image has same width and height then you can simply set the setCircular to true to get Rounded Bitmap as follows
如果您的图像具有相同的宽度和高度,那么您可以简单地将 setCircular 设置为 true 以获得圆角位图,如下所示
RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(getResources(),your_bitmap);
drawable.setCircular(true);
回答by MilapTank
i am also finding rounded image view for efficiency i have search all third party library i found that all of them they are creating new bitmap which is tedious task in list its consuming more memory
我也在寻找圆形图像视图以提高效率我已经搜索了所有第三方库我发现他们所有人都在创建新的位图,这是列表中的乏味任务消耗更多内存
refereed library:
参考图书馆:
- http://ruibm.com/2009/06/16/rounded-corner-bitmaps-on-android/
- https://github.com/vinc3m1/RoundedImageView
- https://github.com/lopspower/CircularImageView
- http://ruibm.com/2009/06/16/rounded-corner-bitmaps-on-android/
- https://github.com/vinc3m1/RoundedImageView
- https://github.com/lopspower/CircularImageView
from this library i have used
从我使用过的这个库中
because A fast ImageView (and Drawable) that supports rounded corners (and ovals or circles) based on the original example from Romain Guy
因为基于 Romain Guy 的原始示例,支持圆角(和椭圆形或圆形)的快速 ImageView(和 Drawable)
- does not create a copy of the original bitmap
- does not use a clipPath which is not hardware accelerated and not anti-aliased.
- does not use setXfermode to clip the bitmap and draw twice to the canvas.
- 不创建原始位图的副本
- 不使用非硬件加速且非抗锯齿的剪辑路径。
- 不使用 setXfermode 来剪辑位图并在画布上绘制两次。
回答by Lakhani Aliraza
Full code:
完整代码:
ImageView img= (ImageView) findViewById(R.id.yourimageid);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.yourpictureresource);
RoundedBitmapDrawable rnd = (RoundedBitmapDrawable) RoundedBitmapDrawableFactory.create(getResources(), bitmap);
rnd.setCircular(true);
img.setImageDrawable(rnd);
回答by toobsco42
I created a Utility class to create RoundedBitmapDrawables https://gist.github.com/lawloretienne/a91fb0ce40f083073d4b8939281b3ecb
我创建了一个实用程序类来创建 RoundedBitmapDrawables https://gist.github.com/lawloretienne/a91fb0ce40f083073d4b8939281b3ecb
It works for circles and rounded squares.
它适用于圆形和圆角正方形。
public class RoundedBitmapDrawableUtility {
public static RoundedBitmapDrawable getRoundedSquareBitmapDrawable(Context context, Bitmap originalBitmap, int cornerRadius){
return getRoundedSquareBitmapDrawable(context, originalBitmap, cornerRadius, -1, -1);
}
public static RoundedBitmapDrawable getRoundedSquareBitmapDrawable(Context context, Bitmap originalBitmap, int cornerRadius, int borderWidth, int borderColor){
int originalBitmapWidth = originalBitmap.getWidth();
int originalBitmapHeight = originalBitmap.getHeight();
if(borderWidth != -1 && borderColor != -1){
Canvas canvas = new Canvas(originalBitmap);
canvas.drawBitmap(originalBitmap, 0, 0, null);
Paint borderPaint = new Paint();
borderPaint.setStyle(Paint.Style.STROKE);
borderPaint.setStrokeWidth(borderWidth);
borderPaint.setAntiAlias(true);
borderPaint.setColor(borderColor);
int roundedRectDelta = (borderWidth/3);
RectF rectF = new RectF(0 + roundedRectDelta, 0 + roundedRectDelta, originalBitmapWidth - roundedRectDelta, originalBitmapHeight - roundedRectDelta);
canvas.drawRoundRect(rectF, cornerRadius, cornerRadius, borderPaint);
}
RoundedBitmapDrawable roundedImageBitmapDrawable = RoundedBitmapDrawableFactory.create(context.getResources(), originalBitmap);
roundedImageBitmapDrawable.setCornerRadius(cornerRadius);
roundedImageBitmapDrawable.setAntiAlias(true);
return roundedImageBitmapDrawable;
}
public static RoundedBitmapDrawable getCircleBitmapDrawable(Context context, Bitmap originalBitmap){
return getCircleBitmapDrawable(context, originalBitmap, -1, -1);
}
public static RoundedBitmapDrawable getCircleBitmapDrawable(Context context, Bitmap originalBitmap, int borderWidth, int borderColor){
if(borderWidth != -1 && borderColor != -1) {
Canvas canvas = new Canvas(originalBitmap);
canvas.drawBitmap(originalBitmap, 0, 0, null);
Paint borderPaint = new Paint();
borderPaint.setStyle(Paint.Style.STROKE);
borderPaint.setStrokeWidth(borderWidth);
borderPaint.setAntiAlias(true);
borderPaint.setColor(borderColor);
int circleDelta = (borderWidth / 2) - DisplayUtility.dp2px(context, 1);
int radius = (canvas.getWidth() / 2) - circleDelta;
canvas.drawCircle(canvas.getWidth() / 2, canvas.getHeight() / 2, radius, borderPaint);
}
RoundedBitmapDrawable roundedImageBitmapDrawable = RoundedBitmapDrawableFactory.create(context.getResources(), originalBitmap);
roundedImageBitmapDrawable.setCircular(true);
roundedImageBitmapDrawable.setAntiAlias(true);
return roundedImageBitmapDrawable;
}
}
回答by android developer
The current way RoundedBitmapDrawable works on non-square images is not so well. It makes the content stretch.
当前 RoundedBitmapDrawable 处理非方形图像的方式不太好。它使内容伸展。
I suggest using an alternative for this, as I've written here: How to have a circular, center-cropped imageView, without creating a new bitmap?
我建议为此使用替代方法,正如我在这里所写的: 如何在不创建新位图的情况下拥有圆形、居中裁剪的 imageView?
回答by Gal Rom
I would like to suggest another option:
我想建议另一种选择:
you can use this method in order to scale it according to your needs and avoid this Exception:
您可以使用此方法根据您的需要对其进行缩放并避免此异常:
public static Bitmap scaleBitmapAndKeepRation(Bitmap TargetBmp, int reqHeightInPixels, int reqWidthInPixels) {
if (TargetBmp != null) {
if (TargetBmp.getWidth() >= TargetBmp.getHeight()) {
TargetBmp = Bitmap.createBitmap(
TargetBmp,
TargetBmp.getWidth() / 2 - TargetBmp.getHeight() / 2,
0,
TargetBmp.getHeight(),
TargetBmp.getHeight()
);
} else {
TargetBmp = Bitmap.createBitmap(
TargetBmp,
0,
TargetBmp.getHeight() / 2 - TargetBmp.getWidth() / 2,
TargetBmp.getWidth(),
TargetBmp.getWidth()
);
}
if (TargetBmp != null) {
try {
Matrix m = new Matrix();
m.setRectToRect(new RectF(0, 0, TargetBmp.getWidth(), TargetBmp.getHeight()), new RectF(0, 0, reqWidthInPixels, reqHeightInPixels), Matrix.ScaleToFit.FILL);
Bitmap scaledBitmap = Bitmap.createBitmap(TargetBmp, 0, 0, TargetBmp.getWidth(), TargetBmp.getHeight(), m, true);
return scaledBitmap;
} catch (Exception e) {
Log.e("Utils", e.toString());
return null;
}
}
return null;
} else
return null;
}