0

enter image description heretoile android dessiner le texte dans le triangle

Dans cette image, je veux texte soit tout à fait dans le triangle avec CYAN couleur.
J'ai créé ma propre ImageView:

public class BookImageView extends android.support.v7.widget.AppCompatImageView { 

private static final Float DISCOUNT_SIDE_SIZE = 0.33333F; 

private Bitmap bitmap; 

private Paint drawPaint = new Paint(); 
private Paint trianglePaint = new Paint(); 

{ 
    trianglePaint.setColor(Constants.DISCOUNT_COLOR); 
    trianglePaint.setStyle(Paint.Style.FILL); 
    trianglePaint.setShadowLayer(10.0f, 10.0f, 10.0f, Color.parseColor("#7f000000")); 
    trianglePaint.setAntiAlias(true); 

    drawPaint.setColor(Color.BLACK); 
    drawPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD)); 
    drawPaint.setShadowLayer(1f, 0f, 1f, Color.BLACK); 
} 

// Constractors ...  

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    if (bitmap != null) { 
     Bitmap tempBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.RGB_565); 
     Canvas tempCanvas = new Canvas(tempBitmap); 
     tempCanvas.drawBitmap(bitmap, 0, 0, null); 

     Path path = new Path(); 
     path.setFillType(Path.FillType.EVEN_ODD); 

     float size = bitmap.getWidth() * DISCOUNT_SIDE_SIZE; 

     path.lineTo(size, 0); 
     path.lineTo(0, size); 
     path.lineTo(0, 0); 
     path.close(); 

     tempCanvas.drawPath(path, trianglePaint); 

     float scale = getResources().getDisplayMetrics().density; 

     drawPaint.setTextSize((int) (14 * scale)); 

     Rect textBounds = new Rect(); 
     drawPaint.getTextBounds("50%", 0, "50%".length(), textBounds); 
     int x = (int) (size/2) - textBounds.width()/2; 
     int y = (int) (size/2) - textBounds.height()/2; 

     tempCanvas.save(); 
     tempCanvas.rotate(-45, x, y); 
     tempCanvas.drawText("50%", x, y, drawPaint); 
     tempCanvas.restore(); 

     setImageDrawable(new BitmapDrawable(getContext().getResources(), tempBitmap)); 
    } 
} 

@Override 
public void setImageBitmap(Bitmap bitmap) { 
    this.bitmap = bitmap; 
    invalidate(); 
} 

}

Que puis-je faire pour résoudre ce problème?

+0

Réduire la taille du texte? –

+0

Essayez de dessiner dans un triangle non pivoté, puis en tournant la vue complète de 45 degrés –

+0

@SunnyKumarAditya merci pour la réponse. mais pas travaillé –

Répondre

1

Vous pouvez essayer quelque chose comme ça

1) Mesurez la largeur de votre texte Utilisez measureText

2) Du point que vous dessinez calculer la largeur restante pour dessiner

3) Maintenant, en fonction du cas d'utilisation, vous pouvez réduire la longueur du texte ou l'adapter au besoin

int textWidthRequired = (int) drawPaint.measureText(textToDraw); 
    int widthRemainingToDraw = totalWidth/2 - textDrawX; 
    if(textWidthRequired > widthRemainingToDraw){ 
     //handling 
    } 
    // draw text 
    tempCanvas.drawText(textToDraw,textDrawX, textDrawY, drawPaint);