安卓:线性GradientDrawable通过code更改中心线性、中心、GradientDrawable、code

由网友(与往事干杯)分享简介:编辑:看溶液上方 I M吓坏了。所有我只是想要做的,是设定一个线性GradientDrawable,从而改变渐变的垂直中心...绘制渐变工作正常,但我怎么可以改变它的中心?!? RelativeLayout的bgScreen =(RelativeLayout的)findViewById(R.id.player_scr...

编辑:看溶液上方

I M吓坏了。所有我只是想要做的,是设定一个线性GradientDrawable,从而改变渐变的垂直中心...绘制渐变工作正常,但我怎么可以改变它的中心?!?

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

GradientDrawable GD =新GradientDrawable(
     GradientDrawable.Orientation.TOP_BOTTOM,
     新的INT [] {startColor,endColor});

gd.setCornerRadius(0F);
gd.setAlpha(200);
bgScreen.setBackground(GD);


公共无效redrawOrChangeBackgroundGradient(){
    // ???要么改变现有的中心
    gd.setGradientCenter(浮X,浮动Y)//只能在RADIAL_GRADIENT或SWEEP_GRADIENT。

    // ???或完全重绘梯度不同的中心

}
 

下面SA画面我要如何改变通过code梯度例子

不能那么难,可以吗?

解决方案

在缺乏能力,以编程方式设置中心直线 GradientDrawable s是的注册问题已

但是,这里描述的解决办法。基本上,你应该创建一个 PaintDrawable 从的 的LinearGradient 并将其设置为视图的背景绘制。   

浮法[]位置的

     

可以为空。的相对位置[0..1]每个对应的颜色   colors数组中。如果这是空,颜色分布   沿着均匀渐变线。

(没有测试过,但它应该做的伎俩为您)的

EDIT: LOOK AT SOLUTION ABOVE

在哪可以修改三线性过滤,在哪里修改啊,图中说的我好晕,我想知道图中所说都要在电脑的什么地方操作

i m freaking out. all i just want to do, is setting a linear GradientDrawable, which changes the vertical center of the gradient... drawing the gradient works fine, but how can i change the center of it?!?

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

}

here s a picture example of how i want to change the gradient via code

cannot be that hard, can it?

解决方案

The lacking ability to set the center programmatically for linear GradientDrawables is a registered issue already.

But there is a workaround described here. Basically, you should create a PaintDrawable from a LinearGradient and set it as your view's background drawable. Following this solution, you can set the center in your LinearGradient constructor by mapping the colors to the positions array:

float[] positions

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

(not tested, but it should do the trick for you)

阅读全文

相关推荐

最新文章