Shader as a drawable (Android)

匿名 (未验证) 提交于 2019-12-03 02:52:02

问题:

How do I use obtain a drawable from a Shader (such as LinearGradient) so that I can use it as a background in my UI?

回答1:

Since the background will probably need to resize, we will use a ShaderFactory to produce the 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);     } }; 

We use this to create a PaintDrawable which we can use as a background:

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

This is just an example and in this case it would actually be better to just declare this using XML. Look at the example here.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!