【问题标题】:Android: Change center of linear GradientDrawable via codeAndroid:通过代码更改线性 GradientDrawable 的中心
【发布时间】:2013-01-17 15:51:32
【问题描述】:

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

我吓坏了。我只想做的就是设置一个线性 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

}

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

不会那么难吧?

【问题讨论】:

    标签: android android-layout android-drawable


    【解决方案1】:

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

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

    float[] 位置

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

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

    【讨论】:

    • 我们如何使用这个设置像渐变绘制一样的角度?
    猜你喜欢
    • 2014-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-06
    • 1970-01-01
    • 2015-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多