简体   繁体   中英

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. 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. 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:

       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(); and to redraw the form. The following void is always called:

@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. 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); method. The device wasn't sending me an alpha value, so this was zero and the color I was creating became transparrent. I'm now using the Color.rgb(r,g,b) method and this is working great :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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