【发布时间】:2018-04-24 08:59:01
【问题描述】:
我正在尝试将“矩形”位图转换为带边框的圆形位图。我写了这段代码:
using Android.Graphics;
namespace MyNamespace
{
public static class BitmapExtension
{
public static Bitmap GetCircularBitmap(this Bitmap bitmap)
{
Bitmap result = Bitmap.CreateBitmap(bitmap.Width, bitmap.Height, Bitmap.Config.Argb8888);
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
Rect rect = new Rect(0, 0, bitmap.Width, bitmap.Height);
paint.AntiAlias = true;
canvas.DrawARGB(0, 0, 0, 0);
paint.Color = Color.Black;
canvas.DrawCircle(bitmap.Width / 2, bitmap.Height / 2, bitmap.Width / 2, paint);
paint.SetXfermode(new PorterDuffXfermode(PorterDuff.Mode.SrcIn));
canvas.DrawBitmap(bitmap, rect, rect, paint);
// Border
paint.SetStyle(Paint.Style.Stroke);
paint.StrokeWidth = 2;
paint.AntiAlias = true;
canvas.DrawCircle(
canvas.Width / 2,
canvas.Width / 2,
canvas.Width / 2 - 2 / 2,
paint);
// Release pixels on original bitmap.
bitmap.Recycle();
return result;
}
}
}
到目前为止,这很有效,但是,因为此代码在 RecyclerView 中使用,有时它只是无法正确绘制:
如您所见,图像画得有些不合适。所以我有两个问题:
- 发生这种奇怪行为的原因是什么?
- 有没有办法改进我的 GetCircularBitmap 方法?由于性能很重要,它必须非常快。
更新:解决方案
我使用FFImageLoading 和Circle Transformation 来显示我的图像。它还大大提高了性能,并为图像缓存提供了良好的实践。
【问题讨论】:
-
第二个结果不就是因为第二个图像不是正方形的吗?您只需首先切掉它的中心正方形并使用它。
标签: java c# android image-processing bitmap