2011-09-07 107 views
16

我有一个问题。我想有一个渐变颜色的文本视图。它背后有一个黑影。问题是,阴影使用渐变的颜色使用被叫颜色代替(Color.BLACKTextView添加渐变和阴影

我的代码是: 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!? 
      ); 

有谁知道该怎么做

回答

17

我有完全相同的问题。 我设法通过扩展TextView和覆盖onDraw方法来解决它。 这里是如何看起来像

@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); 
} 

但是这种方法可能不会,如果你想在渐变中使用的颜色与透明度的工作。