Android canvas change background color

送分小仙女□ 提交于 2019-12-05 02:34:58

Try this:

/*
* A = Alpha a.k.a. transparency
* R = Red color
* G = Green color
* B = Blue color
*
* All of them have a range from 0 to 255
*/
canvas.drawARGB(0, 225, 225, 255);

Or, as @njzk2 stated, you can also use this one:

canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);

But I think the first option is better because it is more precise for, as example, if you want to set it less transparent.

Create a paint

Paint myPaint = new Paint();
myPaint.setColor(res.getColor(R.color.white));

And set your canvas

canvas.draw...(... ,  myPaint);

Is not what I wanted to achieve but is a workaround and maybe is helpful for somebody, I am putting in invisible the second canvas, and then when is ready, I put it visible back.

@Override
public void lock(String message) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            canvasFront.setReadyToDraw(false);
            canvasBackground.setVisibility(View.INVISIBLE);
        }
    });
}

@Override
public void unlock() {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            drawViewClassroom.setVisibility(View.VISIBLE);
            canvasFront.setReadyToDraw(true);
        }
    });
}
Francisco Azevedo

if you want to change the background color of Canvas try this:

canvas.drawColor(ContextCompat.getColor(getContext(), R.color.yourColor));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!