2010-05-04 63 views

回答

14

由于背景可能需要调整,我们将使用一个ShaderFactory产生Shader

ShapeDrawable.ShaderFactory sf=new ShapeDrawable.ShaderFactory() { 
    @Override 
    public Shader resize(int width, int height) { 
     return new LinearGradient(0, 0, width, height, 
      new int[]{Color.WHITE, Color.GRAY, Color.BLACK}, 
      new float[]{0,0.5f,1}, Shader.TileMode.MIRROR); 
    } 
}; 

我们用它来创建一个PaintDrawable,我们可以作为背景使用:

PaintDrawable p=new PaintDrawable(); 
p.setShape(new RectShape()); 
p.setShaderFactory(sf); 
getWindow().setBackgroundDrawable(p); 

这只是一个例子,在这种情况下,只声明这个using XML实际上会更好。看看the example here