Jak używać RoundedBitmapDrawable
Czy komuś udało się użyć RoundedBitmapDrawable
? Popraw mnie, jeśli się mylę, ale według mojego zrozumienia, To sprawia, że okrągły obraz z regularnego prostokątnego obrazu.
Do tej pory próbowałem tego
RoundedBitmapDrawable.createRoundedBitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), iconResource))
Co starałem się osiągnąć: przekształcić dowolny obraz w obraz okrągły i pokazać go za pomocą ImageView.
Na wypadek, gdybym coś pomieszał i wszystko, co powiedziałem, było bezsensowne. Czy jest to możliwe (lub prostsze), aby to zrobić z którymkolwiek z nowych ram? (Android L lub Nowa biblioteka wsparcia)6 answers
Musisz ustawić promień narożnika.
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);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-11-18 18:31:39
Może to być późna odpowiedź, ale mam nadzieję, że będzie ona pomocna dla innych
Jeśli obraz ma taką samą szerokość i wysokość, możesz po prostu ustawić setcircular na true, aby uzyskać zaokrągloną bitmapę w następujący sposób
RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(getResources(),your_bitmap);
drawable.setCircular(true);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-09 10:36:38
Znajduję również zaokrąglony widok obrazu dla wydajności mam przeszukać wszystkie biblioteki stron trzecich znalazłem, że wszystkie z nich tworzą nową bitmapę, która jest żmudnym zadaniem na liście jej zużywa więcej pamięci
Biblioteka sędziowska:
- http://ruibm.com/2009/06/16/rounded-corner-bitmaps-on-android/
- https://github.com/vinc3m1/RoundedImageView
- https://github.com/lopspower/CircularImageView
Z tej biblioteki i używali
Ponieważ Szybki podgląd obrazu (i Rysowalny), który obsługuje zaokrąglone rogi (i owale lub okręgi) na podstawie oryginalnego przykładu Romain Guy
- nie tworzy kopii oryginalnej bitmapy
- nie używa ścieżki clipPath, która nie jest przyspieszana sprzętowo ani antyaliasowana.
- nie używa setXfermode do przycinania bitmapy i rysowania dwukrotnie do obszaru roboczego.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-07-22 05:47:46
Chciałbym zaproponować inną opcję:
Możesz użyć tej metody, aby skalować ją według swoich potrzeb i uniknąć tego wyjątku:
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;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-06-08 06:36:26
I created a Utility class to create RoundedBitmapDrawables https://gist.github.com/lawloretienne/a91fb0ce40f083073d4b8939281b3ecb
Działa na koła i zaokrąglone kwadraty.
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;
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-29 11:37:05
Obecny sposób RoundedBitmapDrawable działa na obrazach innych niż kwadratowe nie jest zbyt dobry. To sprawia, że zawartość się rozciąga.
Proponuję użyć alternatywy dla tego, jak pisałem tutaj: Jak mieć okrągły, center-przycięty imageView, bez tworzenia nowej bitmapy?
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-24 13:26:53