2011-09-07 19 views
16

Tengo un problema. Me gustaría tener una vista de texto con un degradado como color. Y una sombra negra detrás de ella. El problema es que la sombra está utilizando el color del gradiente en lugar de utilizar el color llamado (Color.BLACK)TextView agregar gradiente Y sombra

Mi código es: numberTextView = (TextView)findViewById(R.id.something);

Shader textShaderTop = new LinearGradient(0, 30, 0, 60, 
       new int[]{Color.parseColor("#A6A6A6"), Color.parseColor("#E8E8E8"), Color.parseColor("#A6A6A6")}, 
       new float[]{0, 0.5f, 1}, TileMode.CLAMP); 
    numberTextView.getPaint().setShader(textShaderTop); 

    numberTextView.setShadowLayer(
       0.1f, //float radius 
       20f, //float dx 
       20f, //float dy 
       Color.BLACK //this is not black on the screen, but it uses the gradient color!? 
      ); 

¿Alguien sabe qué hacer

Respuesta

17

Tuve exactamente el mismo problema. Logré solucionarlo ampliando TextView y reemplazando el método onDraw. Aquí es cómo parece

@Override 
protected void onDraw(Canvas canvas) { 
    // draw the shadow 
    getPaint().setShadowLayer(1, 1, 1, 0xbf000000); // or whatever shadow you use 
    getPaint().setShader(null); 
    super.onDraw(canvas); 

    // draw the gradient filled text 
    getPaint().clearShadowLayer(); 
    getPaint().setShader(new LinearGradient(0, getHeight(), 0, 0, 0xffacacac, 0xffffffff, TileMode.CLAMP)); // or whatever gradient/shader you use 
    super.onDraw(canvas); 
} 

Sin embargo este método probablemente no funcionará si desea utilizar colores con transparencia en el degradado.

Cuestiones relacionadas