着色器作为绘制(安卓) [英] Shader as a drawable (Android)

查看:119
本文介绍了着色器作为绘制(安卓)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使用获得的可绘制一个着色器(如的LinearGradient ),这样我可以用它在我的用户界面背景?

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?

推荐答案

由于背景可能需要调整,我们将使用 ShaderFactory 产生着色器

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);
    }
};

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

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);

这只是一个例子,在这种情况下,它实际上是更好的使用,只是宣布该 XML 。看看这里的例子。

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.

这篇关于着色器作为绘制(安卓)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆