如何使用RoundedBitmapDrawable
有没有人设法使用RoundedBitmapDrawable
? 纠正我,如果我错了,但根据我的理解,它从一个规则的矩形图像的圆形图像。
我到目前为止所尝试的是这个
RoundedBitmapDrawable.createRoundedBitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), iconResource))
我试图实现的:将任何图像转换为圆形图像并使用ImageView进行显示。
如果我把事情搞混了,我所说的全是无意义的。 是否有可能(或更简单)与新的框架做到这一点? (Android L或新的支持库)
您需要设置拐angular半径。
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);
这可能是一个迟到的答复,但我希望这对别人有帮助
如果您的图像具有相同的宽度和高度,那么您可以简单地将setCircular设置为true以获得圆angular位图,如下所示
RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(getResources(),your_bitmap); drawable.setCircular(true);
我也发现圆angular的图像视图的效率我有search所有第三方库我发现,他们都在创造新的位图,这是繁琐的任务在列表中消耗更多的内存
评审图书馆:
- http://ruibm.com/2009/06/16/rounded-corner-bitmaps-on-android/
- https://github.com/vinc3m1/RoundedImageView
- https://github.com/lopspower/CircularImageView
从这个图书馆我用过
因为一个快速的ImageView(和可绘制),支持圆angular(和椭圆或圆圈)基于Romain Guy的原始示例
- 不会创build原始位图的副本
- 不使用不硬件加速的clipPath,也不使用反锯齿。
- 不使用setXfermode来剪切位图,并画两次。
我创build了一个Utility类来创buildRoundedBitmapDrawables https://gist.github.com/lawloretienne/a91fb0ce40f083073d4b8939281b3ecb
它适用于圆形和圆angular正方形。
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; } }
我想build议另一个select:
您可以使用此方法来根据您的需要进行缩放,并避免此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; }
RoundedBitmapDrawable当前的方式在非方形图像上工作不太好。 它使内容伸展。
我build议使用一个替代scheme,正如我在这里写的: 如何创build一个圆形的,中心裁剪的imageView,而不创build一个新的位图?