gpt4 book ai didi

Android:通过代码改变线性GradientDrawable的中心

转载 作者:太空狗 更新时间:2023-10-29 13:32:10 25 4
gpt4 key购买 nike

编辑:查看上面的解决方案

我吓坏了。我只想做的是设置一个线性 GradientDrawable,它会改变渐变的垂直中心...绘制渐变效果很好,但我怎样才能改变它的中心?!?

RelativeLayout bgScreen = (RelativeLayout) findViewById(R.id.player_screen);

GradientDrawable gd = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM,
new int[] {startColor,endColor});

gd.setCornerRadius(0f);
gd.setAlpha(200);
bgScreen.setBackground(gd);


public void redrawOrChangeBackgroundGradient(){
//??? either change center of existing
gd.setGradientCenter(float x, float y) //ONLY works in RADIAL_GRADIENT or SWEEP_GRADIENT.

//??? or complete redraw Gradient with different center

}

这是我想如何通过代码更改渐变的图片示例

enter image description here

不会那么难吧?

最佳答案

缺乏以编程方式为线性 GradientDrawable 设置中心的能力是 registered issue已经。

但有一个解决方法 described here .基本上,您应该从 LinearGradient 创建一个 PaintDrawable并将其设置为 View 的可绘制背景。按照此解决方案,您可以通过将颜色映射到 positions 数组来在 LinearGradient 构造函数中设置中心:

float[] positions

May be null. The relative positions [0..1] of each corresponding colorin the colors array. If this is null, the colors are distributedevenly along the gradient line.

(未经测试,但它应该可以为您解决问题)

关于Android:通过代码改变线性GradientDrawable的中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14382872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com