3
如何使用從Shader
(例如LinearGradient
)獲取可繪製對象,以便我可以在我的UI中將它用作背景?作爲可繪製着色器(Android)
如何使用從Shader
(例如LinearGradient
)獲取可繪製對象,以便我可以在我的UI中將它用作背景?作爲可繪製着色器(Android)
由於背景可能需要調整,我們將使用一個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。