2011-04-01 5 views
11

Je veux dessiner du texte en rectangle au centre (horizontalement et verticalement). S'il y a trop de texte qui recadre ce qui ne rentre pas dans rect.Android dessiner du texte dans le rectangle au centre et recadrer si nécessaire

J'ai essayé de le faire comme this example show, mais sans chance.

Des idées?

+0

Vérifiez cela, il est un peu ce même que ce dont vous avez besoin, http://stackoverflow.com/questions/13285510/how-to-overlay-image-with- multiline-texttext-will-être-dans-le-centre-de-la-toile/13287621 # 13287621 – Atrix1987

+0

Essayez ceci, c'est du travail pour moi: http://stackoverflow.com/questions/11120392/android-center-text-on- toile –

+0

Ce travail pour moi, essayez-le. [http://stackoverflow.com/questions/11120392/android-center-text-on-canvas](http://stackoverflow.com/questions/11120392/android-center-text-on-canvas) –

Répondre

0

Que pensez-vous de cela?

<FrameLayout 
    android:id="@+id/frameLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_height="40dp" 
     android:layout_width="100dp" 
     android:layout_gravity="center" 
     android:text="TextViewqqqqqqqqqqqwwww" 
     android:inputType="text"> 
    </TextView> 
</FrameLayout> 
+0

Je veux Dessine un texte sur une toile. Dynamiquement. basé sur la taille du rectangle. – zmeda

5

cette fonction a fonctionné pour moi.

private void drawDigit(Canvas canvas, int textSize, float cX, float cY, int color, String text) { 
     Paint tempTextPaint = new Paint(); 
     tempTextPaint.setAntiAlias(true); 
     tempTextPaint.setStyle(Paint.Style.FILL); 

     tempTextPaint.setColor(color); 
     tempTextPaint.setTextSize(textSize); 

     float textWidth = tempTextPaint.measureText(text); 
     //if cX and cY are the origin coordinates of the your rectangle 
     //cX-(textWidth/2) = The x-coordinate of the origin of the text being drawn 
     //cY+(textSize/2) = The y-coordinate of the origin of the text being drawn 

     canvas.drawText(text, cX-(textWidth/2), cY+(textSize/2), tempTextPaint); 
    } 
9

Essayez cette

private void drawRectText(String text, Canvas canvas, Rect r) { 

    textPaint.setTextSize(20); 
    textPaint.setTextAlign(Align.CENTER); 
    int width = r.width(); 

    int numOfChars = textPaint.breakText(text,true,width,null); 
    int start = (text.length()-numOfChars)/2; 
    canvas.drawText(text,start,start+numOfChars,r.exactCenterX(),r.exactCenterY(),textPaint); 
} 
+0

Ceci dessine du texte avec le centre d'alignement mais seulement horizontalement. Donc, la question est de savoir comment le faire verticalement aussi. – ddmytrenko

Questions connexes