Android 圆形快速接触徽章
大家好,我有一个带有快速联系徽章的联系人列表视图。它显示方形图像,但我的要求是显示圆形快速联系徽章(如下面的屏幕截图)。请告诉我如何才能做到这一点。提前谢谢 下面是创建圆形图像的两种方法。您只需传递位图图像,使其成为圆形Android 圆形快速接触徽章,android,Android,大家好,我有一个带有快速联系徽章的联系人列表视图。它显示方形图像,但我的要求是显示圆形快速联系徽章(如下面的屏幕截图)。请告诉我如何才能做到这一点。提前谢谢 下面是创建圆形图像的两种方法。您只需传递位图图像,使其成为圆形 /** * To make image in a round shape. Use this method if you want to specify required height and width * * @param i */ public static B
/**
* 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;
}
现在一天,您应该查看RoundedBitmapDrawable。可以通过以下方式轻松创建圆形可拉伸曲面:
Bitmap bitmap = <the bitmap you want to be circular>
RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
来源:好的,我试试这个。但是v7库中是否有任何默认视图样式?appcompat v7库中是否没有任何默认样式集?您必须使其自定义,正如我在上面的方法中向您展示的那样。我希望这是一个单行解决方案,但它不会舍入我的缩略图。它正在将其转换为可拉伸的,但它不是圆形的。有什么想法吗?drawable.setCornerRadius(Math.max(bitmap.getWidth()、bitmap.getHeight())/2.0f);
drawable.setCornerRadius(Math.max(bitmap.getWidth(), bitmap.getHeight()) / 2.0f);