Je souhaite afficher le texte ci-dessous dans mon application. J’utilise la classe Paint
avec le style FILL_AND_STROKE
pour y parvenir. Mais une seule méthode, setColor()
est disponible pour définir la couleur.
Comment définir différentes couleurs de trait et de contour?
À l’intérieur de TextView personnalisé (ne fonctionne pas dans EditText):
@Override public void onDraw(Canvas canvas) { final ColorStateList textColor = getTextColors(); TextPaint paint = this.getPaint(); paint.setStyle(Style.STROKE); paint.setStrokeJoin(Join.ROUND); paint.setStrokeMiter(10); this.setTextColor(strokeColor); paint.setStrokeWidth(strokeWidth); super.onDraw(canvas); paint.setStyle(Style.FILL); setTextColor(textColor); super.onDraw(canvas); }
N’utilisez pas FILL_AND_STROKE. Dessinez une fois avec FILL, puis changez de couleur et dessinez avec STROKE.
(Cela fonctionne pour les rectangles. Je ne suis pas sûr que STROKE fonctionne du tout pour le texte. Vous devrez l’essayer et le découvrir.)
pas tout à fait sûr, mais peut-être que vous pourriez utiliser ceci:
lien
TextView test = (TextView) findViewById(R.id.test); test.setShadowLayer(float, float, float, int);
J’ai utilisé la première solution ci-dessus pour trouver cette idée: posez un texte plus grand sur STROKE, puis superposez-le avec un texte FILL_AND_STROKE plus petit:
mScorePaint = new TextPaint(); mScorePaint.setTextSize(63); mScorePaint.setStyle(Style.STROKE); mScorePaint.setStrokeJoin(Join.ROUND); mScorePaint.setStrokeMiter(10.0f); mScorePaint.setStrokeWidth(frameWidth/50.0f); // about 12 mScorePaint.setColor(0xffff0000); // black c.drawText(Integer.toSsortingng(mScore), x, y, mScorePaint); // red first mScorePaint.setStrokeWidth(frameWidth/125.0f); // about 5 mScorePaint.setColor(0xff000000); // red c.drawText(Integer.toSsortingng(mScore), x, y, mScorePaint); // black on top
Parce que le FILL seul ne voyait aucun des atsortingbuts de l’AVC et sortait très maigre.