简体   繁体   English

Android:GradientDrawable只是绘制黑色

[英]Android: GradientDrawable just drawing black

I have a an app with several sliders with and the purpose is to change the color of a set of LED lights i have connected to it. 我有一个带有多个滑块的应用程序,目的是更改我已连接到它的一组LED灯的颜色。 I am implementing a UI with a function which will change the color of a radial gradient to give an approximation of the LED color. 我正在用一个函数来实现UI,该函数将更改径向渐变的颜色以给出LED颜色的近似值。

I can define the default background like this: 我可以这样定义默认背景:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<gradient
    android:id="@+id/radGrad"
    android:centerColor="#ecd473"
    android:centerY="0%"
    android:centerX="0%"
    android:endColor="#000000"
    android:gradientRadius="600"
    android:innerRadiusRatio="2"
    android:startColor="#f9f7a8"
    android:type="radial" >
</gradient>
</shape>

and in my activity layout: 在我的活动布局中:

This will be displayed as a background which looks like this: enter image description here 这将显示为背景,如下所示: 在此处输入图像描述

I am attempting to overwrite the background with values that are selected by the sliders in the picture. 我试图用图片中的滑块选择的值覆盖背景。 I want to essentially create the same background image with a radial gradient in the top left corner with various colors, but i am stuck. 我本质上想在左上角使用各种颜色创建带有径向渐变的相同背景图像,但是我被卡住了。

Here is my java code, after OnCreate() 这是我的Java代码,位于OnCreate()之后

View backgroundView;
GradientDrawable background;
background = this.getWindow().getDecorView();

and the rest of the code when i select the submit button: 和其余的代码,当我选择“提交”按钮时:

submitButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            color = new int[3];
            color[0]=0xecd473; //these values will eventually be changed by the slider
            color[1]=0xf9f7a8;
            color[2]=0xffffff;
            background = new GradientDrawable(GradientDrawable.Orientation.BL_TR, color);
            background.mutate();
            background.setGradientType(GradientDrawable.RADIAL_GRADIENT);
            background.setGradientRadius(600);
            background.setGradientCenter(0,0);
            background.setShape(GradientDrawable.RECTANGLE);
            background.setCornerRadius(50f);
            background.setColors(color);
            backgroundView.setBackground(background);
            progressTextView.setText("Hue: " + hueValue + " Brightness: " + brightValue + " Saturation: " + satValue);
        }
    });

}    

No matter what colors i feed background.setColors(color) the background appears black after I click the submit. 无论我使用background.setColors(color)是什么颜色,单击提交后背景都会显示为黑色。 I feel like I am missing something important and other examples have not helped. 我觉得我缺少一些重要的东西,而其他例子却无济于事。 I hope this is clear. 我希望这很清楚。

I figured it out. 我想到了。 The color values were missing their Alpha values so I am guessing it was transparent or the function was not taking it. 颜色值缺少其Alpha值,因此我猜测它是透明的或函数未使用它。 By adding ff to each color value the background came into focus. 通过为每个颜色值添加ff,背景便成为焦点。

First off, you dont need to use backdround.mutate(); 首先,您不需要使用backdround.mutate();。 the object is already mutable. 该对象已经可变。 Also the function .setColor() requires an argb value. 同样,函数.setColor()需要一个argb值。 Although the documentation says that it also accepts an rgb, it translates an argb value without an an alpha as 100% transparency, which might as well be invisible. 尽管文档中说它也接受rgb,但它会转换不带alpha值的argb值作为100%透明度,这也可能是不可见的。 You need to add the alpha when you are passing the value. 传递值时,您需要添加Alpha。 That is why you need an extra FF to prepend your rgb. 这就是为什么您需要额外的FF来添加rgb的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM