简体   繁体   English

Android重绘矩形填充白色

[英]Android Repainting Rectangle Fills White

I'm trying to create a custom view which reads a colour from my arduino via bluetooth and displays that colour on my phone screen. 我正在尝试创建一个自定义视图,该视图通过蓝牙从arduino中读取颜色并将其显示在手机屏幕上。 I'm able to successfuly read a colour from the bluetooth device through a thread which I have created to continually read data from the arduino. 我能够通过创建的线程从蓝牙设备成功读取颜色,以不断从arduino读取数据。 I have also got another thread running on a loop to see if that colour has changed and if it has, to change the global variable chosenColor accordingly: 我还让另一个线程在循环上运行,以查看该颜色是否已更改以及是否已更改,以相应地更改全局变量selectedColor:

       while(true){
            int newColor = device.activeSensorColor;
            if(chosenColor != newColor){
                chosenColor = newColor;
                invalidater.post(invalidating);
            }
        }

After it has done this, it uses a Handler on the UI thread to call invalidate(); 完成此操作后,它将在UI线程上使用Handler来调用invalidate();。 and to redraw the form. 并重新绘制表格。 The following void is always called: 始终调用以下void:

@Override
 protected void onDraw(Canvas canvas){
    super.onDraw(canvas);

    canvas.drawRect(bounds, boxPaint);

    samplePaint.setColor(chosenColor);

    canvas.drawRect(colorSample, samplePaint);

    canvas.drawText(buttonText, (width - height) / 2 + height, height / 2, textPaint);

    canvas.drawRect(sampleBorder, borderPaint);
}

And this will without fail set the colour of the box to white. 这将毫无疑问地将盒子的颜色设置为白色。 I've been trying to figure this out for hours, but every time I try something, the colour of the sample rectangle will be white, no matter what colour is being picked up from the arduino. 我已经尝试了好几个小时,但每次尝试进行操作时,无论从arduino拾取什么颜色,示例矩形的颜色都将变为白色。 Has anyone got any ideas? 有人知道吗? Thanks. 谢谢。

I've figured it out! 我知道了! I can't believe I was so stupid! 我简直不敢相信自己如此愚蠢! After the colour was being read from my arduino, I was saving it to an integer using the Color.argb(a,r,g,b); 从我的arduino中读取颜色后,我使用Color.argb(a,r,g,b)将其保存为整数。 method. 方法。 The device wasn't sending me an alpha value, so this was zero and the color I was creating became transparrent. 设备没有向我发送Alpha值,因此该值为零,而我创建的颜色变得透明。 I'm now using the Color.rgb(r,g,b) method and this is working great :) 我现在正在使用Color.rgb(r,g,b)方法,这很好用:)

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

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