2013-02-26 18 views
5

Cosa voglio fare? (blu sarà cambiata come bianco) enter image description hereCome delineare un TextView?

Che cosa ho fatto?
Ho trovato una classe che estende TextView che è in grado di delineare la visualizzazione del testo molto vicino a ciò che voglio. Il problema è che non sono riuscito a cambiare il colore del tratto a nessun colore, esso disegna sempre come nero. Come impostare il colore del bordo come bianco?

Qual è la mia uscita:
enter image description here

Dove sono i miei codici?

public class TypeFaceTextView extends TextView { 

private static Paint getWhiteBorderPaint(){ 
    Paint p = new Paint(Color.WHITE); 
    return p; 
} 

private static final Paint BLACK_BORDER_PAINT = getWhiteBorderPaint(); 

static { 
    BLACK_BORDER_PAINT.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT)); 
} 

@Override 
public void setText(CharSequence text, BufferType type) { 

    super.setText(String.format(text.toString()), type); 
} 

private static final int BORDER_WIDTH = 1; 

private Typeface typeface; 

public TypeFaceTextView(Context context) { 
    super(context); 
} 

public TypeFaceTextView(Context context, AttributeSet attrs) { 
    super(context, attrs); 

    setDrawingCacheEnabled(false); 

    setTypeface(attrs); 
} 

private void setTypeface(AttributeSet attrs) { 
    final String typefaceFileName = attrs.getAttributeValue(null, "typeface"); 
    if (typefaceFileName != null) { 
     typeface = Typeface.createFromAsset(getContext().getAssets(), typefaceFileName); 
    } 

    setTypeface(typeface); 
} 

public TypeFaceTextView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 

    setTypeface(attrs); 
} 

@Override 
public void draw(Canvas aCanvas) { 
    aCanvas.saveLayer(null, BLACK_BORDER_PAINT, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG 
      | Canvas.FULL_COLOR_LAYER_SAVE_FLAG | Canvas.MATRIX_SAVE_FLAG); 

    drawBackground(aCanvas, -BORDER_WIDTH, -BORDER_WIDTH); 
    drawBackground(aCanvas, BORDER_WIDTH + BORDER_WIDTH, 0); 
    drawBackground(aCanvas, 0, BORDER_WIDTH + BORDER_WIDTH); 
    drawBackground(aCanvas, -BORDER_WIDTH - BORDER_WIDTH, 0); 

    aCanvas.restore(); 
    super.draw(aCanvas); 

} 

private void drawBackground(Canvas aCanvas, int aDX, int aDY) { 
    aCanvas.translate(aDX, aDY); 
    super.draw(aCanvas); 
} 
} 
+0

Si prega di guardare l'immagine, è irrilevante ciò che voglio fare. –

+0

Ciao Mustafa, ho pubblicato la soluzione al tuo problema. Controllalo se funziona, invece di usare la soluzione "shadow". Grazie. – Elye

+0

possibile duplicato di [testo di testo con vista testo Android] (http://stackoverflow.com/questions/3182393/android-textview-outline-text) –

risposta

4

1) creare l'oggetto TextView estende TextView

public class YourTextView extends TextView { ......... 

2) Fate questo sul suo metodo draw

@Override 
public void draw(Canvas canvas) { 
     for (int i = 0; i < 5; i++) { 
     super.draw(canvas); 
    } 
} 

3) lato xml set di TextView come di seguito

android:shadowColor="@color/white" 
android:shadowRadius="5" 
0

è necessario modificare il metodo vostra getWhiteBorderPaint() per la seguente:

private static Paint getWhiteBorderPaint(){ 
    Paint p = new Paint(); 
    p.setColor(Color.WHITE); 
    return p; 
} 

I Paint constructor bandiere richiede solo una maschera di bit e non supporta interi arbitrari come parametri.

+0

no, non ha funzionato. –

0

Indagato nel problema originale indicato da questa domanda. Trovato la soluzione.

In primo luogo, cambiare DST_OUT per scurire

static { 
    BLACK_BORDER_PAINT.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DARKEN)); 
} 

In secondo luogo, salvare il colore del testo originale, e mettere il contorno destinato colore up, disegnare il contorno, e quindi ripristinare il colore del testo originale.

@Override 
public void draw(Canvas aCanvas) { 
    int originalColor = this.getCurrentTextColor(); 
    this.setTextColor(0xff000000); //set it to white. 

    aCanvas.saveLayer(null, borderPaint, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG 
      | Canvas.FULL_COLOR_LAYER_SAVE_FLAG | Canvas.MATRIX_SAVE_FLAG); 

     drawBackground(aCanvas, -BORDER_WIDTH, -BORDER_WIDTH); 
     drawBackground(aCanvas, BORDER_WIDTH + BORDER_WIDTH, 0); 
     drawBackground(aCanvas, 0, BORDER_WIDTH + BORDER_WIDTH); 
     drawBackground(aCanvas, -BORDER_WIDTH - BORDER_WIDTH, 0); 

    this.setTextColor(originalColor); 
    aCanvas.restore(); 
    super.draw(aCanvas); 
} 
Problemi correlati