Android canvas background color

Android Canvas Color Change

I have an application with two views

<com.myexample.ui.view.BackgroundView android:id="@+id/id_draw_canvas_classroom" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginBottom="3dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginTop="3dp" android:layout_weight="1" android:background="#FFFFFFFF" /> <com.myexample.ui.view.FrontView android:id="@+id/id_draw_canvas_user" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginBottom="3dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginTop="3dp" android:layout_weight="1" android:background="#00000000" /> 

These views overlap, and for a period of time I load the information as a background. During this time, I would like to set FrontView to white, and then (when loading the background) turn into transparent.

In FrontView, I have a bitmap canvas. I work and I can do it if I want to set the background to transparent

 canvas.drawColor(0); 

set background in white

 canvas.drawColor(-1); 

But I can not change white to transparent.

thanks

+6
source share
3 answers

Not what I wanted to achieve, but it is a workaround and perhaps useful for someone, I put an invisible second canvas, and then when it is ready, I put it 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); } }); } 
0
source

Try the following:

 /* * A = Alpha aka 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 pointed out by @ njzk2, you can also use this:

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

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

+12
source

Create paint

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

And set your canvas

 canvas.draw...(... , myPaint); 
+4
source

Source: https://habr.com/ru/post/981613/


All Articles