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。