How to add a black banner like Snapchat
lukas6410
-
BunoCS Posted messages 436 Registration date Status Moderator Last intervention -
BunoCS Posted messages 436 Registration date Status Moderator Last intervention -
Bonjour, je cherche à placer un bandeau noir derrière le texte. Voici la fonction qui dessine le texte :
```java
public void drawMultilineTextToBimap(Context context, String text, Bitmap bitmap) {
Resources resources = context.getResources();
float scale = resources.getDisplayMetrics().density;
Bitmap.Config bitmapConfig = bitmap.getConfig();
if (bitmapConfig == null) {
bitmapConfig = Bitmap.Config.ARGB_8888;
}
Canvas canvas = new Canvas(bitmap);
// Dessiner le rectangle noir derrière le texte
Paint paintRect = new Paint();
paintRect.setColor(Color.BLACK);
int heightRect = 100;
int topRect = (bitmap.getHeight() - heightRect) / 2; // Centrer verticalement
int bottomRect = topRect + heightRect;
canvas.drawRect(30, topRect, bitmap.getWidth() - 30, bottomRect, paintRect); // Ajuster les coordonnées du rectangle
TextPaint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.WHITE);
paint.setTextSize((int) (100 * scale));
paint.setShadowLayer(1f, 0f, 1f, Color.BLACK);
int textWidth = canvas.getWidth() - (int) (16 * scale);
// Initier le StaticLayout pour le texte
StaticLayout textLayout = new StaticLayout(text, paint, textWidth, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
// Obtenir la hauteur du texte multilignes
int textHeight = textLayout.getHeight();
// Obtenir la position du coin supérieur gauche du texte
float x = (bitmap.getWidth() - textWidth) / 2;
float y = (bitmap.getHeight() - textHeight) / 2;
// Dessiner le texte sur le Canvas centré
canvas.save();
canvas.translate(x, y);
textLayout.draw(canvas);
canvas.restore();
}
```
**Configuration :** Windows / Firefox 98.0
```java
public void drawMultilineTextToBimap(Context context, String text, Bitmap bitmap) {
Resources resources = context.getResources();
float scale = resources.getDisplayMetrics().density;
Bitmap.Config bitmapConfig = bitmap.getConfig();
if (bitmapConfig == null) {
bitmapConfig = Bitmap.Config.ARGB_8888;
}
Canvas canvas = new Canvas(bitmap);
// Dessiner le rectangle noir derrière le texte
Paint paintRect = new Paint();
paintRect.setColor(Color.BLACK);
int heightRect = 100;
int topRect = (bitmap.getHeight() - heightRect) / 2; // Centrer verticalement
int bottomRect = topRect + heightRect;
canvas.drawRect(30, topRect, bitmap.getWidth() - 30, bottomRect, paintRect); // Ajuster les coordonnées du rectangle
TextPaint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.WHITE);
paint.setTextSize((int) (100 * scale));
paint.setShadowLayer(1f, 0f, 1f, Color.BLACK);
int textWidth = canvas.getWidth() - (int) (16 * scale);
// Initier le StaticLayout pour le texte
StaticLayout textLayout = new StaticLayout(text, paint, textWidth, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
// Obtenir la hauteur du texte multilignes
int textHeight = textLayout.getHeight();
// Obtenir la position du coin supérieur gauche du texte
float x = (bitmap.getWidth() - textWidth) / 2;
float y = (bitmap.getHeight() - textHeight) / 2;
// Dessiner le texte sur le Canvas centré
canvas.save();
canvas.translate(x, y);
textLayout.draw(canvas);
canvas.restore();
}
```
**Configuration :** Windows / Firefox 98.0