【问题标题】:mixing two gradient drawable for a view in android在android中为视图混合两个渐变可绘制对象
【发布时间】:2014-09-26 11:06:34
【问题描述】:

我正在尝试在视图的背景中绘制一些颜色。

颜色将被放置为线性渐变和不同的方向。

所以我做了以下事情:

private Orientation[] orientations = new Orientation[]{Orientation.BL_TR, Orientation.BR_TL, Orientation.TL_BR, Orientation.TR_BL, Orientation.TOP_BOTTOM};    
public void drawBackgroudGradient(RelativeLayout backgroundView, int[] colors){
        if (colors != null){
            if (colors.length > 1){
                for (int i=0; i<colors.length; i++){
                    backgroundView.setBackgroundDrawable(getGradientDrawable(colors[i], orientations[i]));
                }
            }else{
                //in case of only one color just set that color as background
                    backgroundView.setBackgroundColor(colors[0]);
            }
        }
    }

    private GradientDrawable getGradientDrawable(int color, Orientation orientation){
        int[] colors = new int[] {color, Color.WHITE};

        GradientDrawable drawable = new GradientDrawable(orientation, colors);
        drawable.setAlpha(125);
        return drawable;
    }

我将每个可绘制对象从起始颜色变为透明,以便所有渐变都可见。 但问题是所有颜色都没有显示出来。只有最后一种颜色是通过渐变可绘制的。其余的看不到。

有人可以帮我弄清楚如何混合和显示所有颜色吗?

谢谢。 晴天

【问题讨论】:

  • 使用 LayerDrawable 作为你的毕业的容器
  • 你能举个例子吗
  • backgroundView.setBackgroundDrawable(yourLayerDrawable)
  • thanks.but 我如何将我的渐变可绘制对象放入该图层可绘制对象中?
  • 怎么样?通过阅读 LayerDrawable 文档

标签: android view gradient android-drawable


【解决方案1】:

创建 LayerDrawable 并混合它们:

    private LayerDrawable getGradientDrawable(int color, Orientation  orientation){
    int[] colors = new int[] {color, Color.WHITE};

    GradientDrawable drawable1 = new GradientDrawable(orientation, colors);
    drawable1.setAlpha(125);

    GradientDrawable drawable2 = new GradientDrawable();
    drawable2.setStroke(4, Color.parseColor("#FFFFFF"));
    drawable2.setColor(Color.TRANSPARENT);

    return new LayerDrawable(new Drawable[]{drawable1 , drawable2});

}

drawable2 放在 drawable1 之上。

【讨论】:

    猜你喜欢
    • 2020-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多