2015-10-24 2 views
2

Je crée un rectangle dans la méthode @Override de ReplacementSpan. Comment ajouter les RoundCorner et padding à cela?Comment ajouter coin rond à Drawrect de toile dans Android?

code:

@Override 
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) { 
    RectF rect = new RectF(x, top, x + measureText(paint, text, start, end), bottom); 
    paint.setColor(mBackgroundColor); 
    canvas.drawRect(rect, paint); 
    paint.setColor(mForegroundColor); 
    canvas.drawText(text, start, end, x, y, paint); 
} 

Modifier-1

J'utilise measureText:

private float measureText(Paint paint, CharSequence text, int start, int end) { return paint.measureText(text, start, end); }

Modifier-2

Après quelques suggestions que je fait ces changements et je peut voir Rounded coin sur Rectangle

@Override 
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) { 
    RectF rect = new RectF(x, top, x + measureText(paint, text, start, end), bottom); 
    paint.setColor(mBackgroundColor); 
    canvas.drawRoundRect(rect, 15,15,paint); 
    paint.setColor(mForegroundColor); 
    canvas.drawText(text, start, end, x, y, paint); 

} 

et voici la capture d'écran:

enter image description here

J'appelle méthode draw de code suivant:

int currentIndex = 0; 
for (int i = 0; i < words.length - 1; i++) { 
      s.setSpan(new CustomDrawble(Color.GRAY, Color.WHITE), currentIndex, currentIndex + words[i].length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
      currentIndex += words[i].length() + 1; 
     } 

Répondre

4

toile a la méthode drawRoundRect. Vous devrez fournir le RectF à dessiner, le Paint, ainsi que drawRect et deux paramètres d'addition, rx et ry qui représentent le rayon x et y de vos coins arrondis. Par exemple.

canvas.drawRoundRect(rect, 5, 5, paint); 

dessinera un rect avec coin rond de 5pixels

Edit2:

Avertissement: utiliser dip au lieu de pixels

@Override 
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) { 
    RectF rect = new RectF(x, top, x + measureText(paint, text, start, end) + 10, bottom); 
    paint.setColor(mBackgroundColor); 
    canvas.drawRoundRect(rect, 15,15,paint); 
    paint.setColor(mForegroundColor); 
    canvas.drawText(text, start, end, x + 5, y, paint); 
} 
+0

Merci pour votre réponse je viens d'utiliser et il fonctionne ! Je cherchais Comment définir le texte Margin of DrawRect? Comment puis je faire ça? –

+0

marge? La seule chose qui me vient à l'esprit est de rendre le rectangle un peu plus grand. la toile ne s'occupe pas de maring/padding. Vous pouvez essayer le niveau de vue – Blackbelt

+0

Actuellement, j'utilise ReplacementSpan et pour mesurer le texte, j'utilise measureText. S'il vous plaît jeter un oeil à la section Modifier-1 –