动态改变渐变中心

问题描述 投票:0回答:1
android gradient
1个回答
4
投票

你可以通过使用Shader实现你的目标

ShapeDrawable.ShaderFactory sf = new ShapeDrawable.ShaderFactory() {
    @Override
    public Shader resize(int width, int height) {
        return new LinearGradient(width/2, 0, width/2, height,
                new int[]{0xff000000, 0xff42f448, 0xff000000},
                new float[]{0, 0.3f, 1},  // start, center and end position
                Shader.TileMode.CLAMP);
    }
};

PaintDrawable pd = new PaintDrawable();
pd.setShape(new RectShape());
pd.setShaderFactory(sf);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
    layout.setBackground(pd);
} else
    layout.setBackgroundDrawable(pd);
© www.soinside.com 2019 - 2024. All rights reserved.