Quelqu’un at-il réussi à utiliser RoundedBitmapDrawable
? Corrigez-moi si je me trompe, mais à ma connaissance, cela crée une image circulaire à partir d’une image rectangular régulière.
Qu’est-ce que j’ai essayé jusqu’à présent est-ce
RoundedBitmapDrawable.createRoundedBitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), iconResource))
Ce que j’ai essayé de réaliser: transformer n’importe quelle image en une image circulaire et l’afficher avec un ImageView.
Dans le cas où je mélange les choses et tout ce que j’ai dit n’est pas sensé. Est-il possible (ou plus simple) de le faire avec l’un des nouveaux frameworks? (Android L ou nouvelle bibliothèque de support)
Vous devez définir le rayon du coin.
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);
C’est peut-être une réponse tardive mais j’espère que cela sera utile aux autres
Si votre image a la même largeur et la même hauteur, vous pouvez simplement définir le paramètre setCircular sur true pour obtenir le bitmap arrondi comme suit
RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(getResources(),your_bitmap); drawable.setCircular(true);
Je suis également en train de trouver une image arrondie pour l’efficacité J’ai cherché dans toutes les bibliothèques tierces J’ai trouvé que tous créaient un nouveau bitmap qui est une tâche fastidieuse dans la liste de sa consommation plus de mémoire
bibliothèque arbitrée:
de cette bibliothèque j’ai utilisé
parce qu’un rapide ImageView (et Drawable) qui supporte des coins arrondis (et des ovales ou des cercles) basé sur l’exemple original de Romain Guy
Je voudrais suggérer une autre option:
vous pouvez utiliser cette méthode afin de l’adapter à vos besoins et éviter cette 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 { Masortingx m = new Masortingx(); m.setRectToRect(new RectF(0, 0, TargetBmp.getWidth(), TargetBmp.getHeight()), new RectF(0, 0, reqWidthInPixels, reqHeightInPixels), Masortingx.ScaleToFit.FILL); Bitmap scaledBitmap = Bitmap.createBitmap(TargetBmp, 0, 0, TargetBmp.getWidth(), TargetBmp.getHeight(), m, true); return scaledBitmap; } catch (Exception e) { Log.e("Utils", e.toSsortingng()); return null; } } return null; } else return null; }
J’ai créé une classe Utility pour créer RoundedBitmapDrawables https://gist.github.com/lawloretienne/a91fb0ce40f083073d4b8939281b3ecb
Cela fonctionne pour les cercles et les carrés arrondis.
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; } }
La manière actuelle dont RoundedBitmapDrawable fonctionne sur les images non carrées n’est pas si bonne. Cela rend le contenu extensible.
Je suggère d’utiliser une alternative pour cela, comme je l’ai écrit ici: Comment avoir une imageView circulaire, recadrée au centre, sans créer un nouveau bitmap?