2010-10-28 17 views
26

Sto cercando di sviluppare una semplice classe di grafico a torta per Android. Per ora, può prendere una mappa di etichette e valori e disegnare il grafico a torta. Devo ancora aggiungere le leggende per la torta, che è dove ho bisogno di posizionare i testi vicino a piccoli rettangoli sull'angolo dello schermo. Qualsiasi aiuto è apprezzato, dal momento che sono nuovo di Android dev.Come disegnare un testo su tela?

risposta

1

Un altro (forse meglio) modo per disegnare il testo su una tela è quello di utilizzare un StaticLayout. Questo gestisce il testo multilinea quando necessario.

String text = "This is some text."; 

TextPaint textPaint = new TextPaint(); 
textPaint.setAntiAlias(true); 
textPaint.setTextSize(16 * getResources().getDisplayMetrics().density); 
textPaint.setColor(0xFF000000); 

int width = (int) textPaint.measureText(text); 
StaticLayout staticLayout = new StaticLayout(text, textPaint, (int) width, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0, false); 
staticLayout.draw(canvas); 

Il TextPaint e StaticLayout sono stati istanziati destra prima di essere utilizzato qui per il bene di illustrazione. Farlo nel onDraw farebbe male alle prestazioni, comunque. Here is a better example mostrandoli nel contesto di una vista personalizzata che disegna il proprio testo.

6

In passato c'era un'altra risposta che è stata cancellata perché era solo un collegamento. Il collegamento originale è here. Il codice è fondamentalmente lo stesso, ma ho eliminato le porzioni di disegno non di testo e ho anche ridimensionato le dimensioni per lavorare meglio sulle densità di schermo moderne.

Questo mostra solo alcune cose che puoi fare con il disegno del testo.

enter image description here

Ecco il codice aggiornato:

public class MainActivity extends AppCompatActivity { 

    DemoView demoview; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     demoview = new DemoView(this); 
     setContentView(demoview); 
    } 

    private class DemoView extends View { 
     public DemoView(Context context){ 
      super(context); 
     } 

     @Override protected void onDraw(Canvas canvas) { 
      super.onDraw(canvas); 

      // custom drawing code here 
      // remember: y increases from top to bottom 
      // x increases from left to right 
      int x = 0; 
      int y = 0; 
      Paint paint = new Paint(); 
      paint.setStyle(Paint.Style.FILL); 

      canvas.save(); 
      canvas.translate(100, 200); 

      // make the entire canvas white 
      canvas.drawColor(Color.WHITE); 

      // draw some text using STROKE style 
      paint.setStyle(Paint.Style.STROKE); 
      paint.setStrokeWidth(1); 
      paint.setColor(Color.MAGENTA); 
      paint.setTextSize(100); 
      canvas.drawText("Style.STROKE", 0, 0, paint); 

      canvas.translate(0, 200); 

      // draw some text using FILL style 
      paint.setStyle(Paint.Style.FILL); 
      //turn antialiasing on 
      paint.setAntiAlias(true); 
      //paint.setTextSize(30); 
      canvas.drawText("Style.FILL", 0, 0, paint); 

      canvas.translate(0, 200); 

      // draw some rotated text 
      // get text width and height 
      // set desired drawing location 
      x = 75; 
      y = 185; 
      paint.setColor(Color.GRAY); 
      //paint.setTextSize(25); 
      String str2rotate = "Rotated!"; 

      // draw bounding rect before rotating text 
      Rect rect = new Rect(); 
      paint.getTextBounds(str2rotate, 0, str2rotate.length(), rect); 
      canvas.translate(x, y); 
      paint.setStyle(Paint.Style.FILL); 
      // draw unrotated text 
      canvas.drawText("!Rotated", 0, 0, paint); 
      paint.setStyle(Paint.Style.STROKE); 
      canvas.drawRect(rect, paint); 
      // undo the translate 
      canvas.translate(-x, -y); 

      // rotate the canvas on center of the text to draw 
      canvas.rotate(-45, x + rect.exactCenterX(), 
        y + rect.exactCenterY()); 
      // draw the rotated text 
      paint.setStyle(Paint.Style.FILL); 
      canvas.drawText(str2rotate, x, y, paint); 

      //undo the translation and rotation 
      canvas.restore(); 
     } 
    } 
} 

Un'altra cosa che voglio provare tardi è drawing text along a path.

Vedere anche this fuller answer here che fornisce l'immagine seguente.

enter image description here

Problemi correlati