大家好我有联系人列表查看快速联系徽章。它显示方形图像,但我的要求是显示圆形快速联系徽章(如下面的屏幕截图)。 Plz让我知道如何实现这一目标。在此先感谢。
答案 0 :(得分:2)
以下是两种创建圆形图像的方法。你只需要传递位图图像使它成为圆形。
/**
* To make image in a round shape. Use this method if you want to specify required height and width
*
* @param i
*/
public static Bitmap getRoundedShape(Bitmap scaleBitmapImage, int i) {
int targetWidth = i;
int targetHeight = i;
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 = scaleBitmapImage;
canvas.drawBitmap(sourceBitmap, new Rect(0, 0, sourceBitmap.getWidth(),
sourceBitmap.getHeight()), new Rect(0, 0, targetWidth,
targetHeight), null);
return targetBitmap;
}
/**
* To make image in a round shape
*/
public static Bitmap getCroppedBitmap(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
// canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
bitmap.getWidth() / 2, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
// Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false);
// return _bmp;
return output;
}
答案 1 :(得分:1)
现在有一天,你应该研究一下RoundedBitmapDrawable。
可以轻松创建圆形绘图Bitmap bitmap = <the bitmap you want to be circular>
RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
别忘了(感谢Gil SH指出这一点):
drawable.setCornerRadius(Math.max(bitmap.getWidth(), bitmap.getHeight()) / 2.0f);