有没有人设法利用RoundedBitmapDrawable
?纠正我,如果我错了,但我的理解,它使从规则的矩形图像的圆形图像。
什么到目前为止,我已经试过这是
RoundedBitmapDrawable.createRoundedBitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), iconResource))
我想实现:任意变换图像的圆形图像,并使用ImageView的表现出来。
如果我混的事情了,所有我说的是无感。是否有可能(或简单)与任何新的框架来做到这一点? (安卓L或新的支持库)
您需要设置圆角半径。
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,以获得圆润位图如下
RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(getResources(),your_bitmap);
drawable.setCircular(true);
我也发现圆形的图像视图的效率,我有搜索所有的第三方库,我发现所有的人,他们正在创造新的位图,其中在列表中繁琐的任务其消耗更多的内存
审阅库:
从这个库我已经使用
因为快速ImageView的(和绘制对象)支持圆角(和椭圆或圆形)的基础上从罗曼盖伊原始示例
我创建了一个工具类来创建RoundedBitmapDrawables https://gist.github.com/lawloretienne/a91fb0ce40f083073d4b8939281b3ecb
它适用于圆和圆方形。
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;
}
}
全码:
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);
我想提出另一种选择:
您可以根据自己的需要,以扩大其使用这种方法,避免此异常:
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工作在非方形图像不那么好。它使内容舒展。
我建议使用这种替代,因为我在这里写:How to have a circular, center-cropped imageView, without creating a new bitmap?