安卓:线性GradientDrawable通过code更改中心 [英] Android: Change center of linear GradientDrawable via code

查看:698
本文介绍了安卓:线性GradientDrawable通过code更改中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:看溶液上方

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 并将其设置为视图的背景绘制。 位置阵列设置中心在的LinearGradient 构造P>

  

浮法[]位置

     

可以为空。的相对位置[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)

这篇关于安卓:线性GradientDrawable通过code更改中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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