2010-05-04 62 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