14

Android canvas change color

I have an app 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" />

This views are overlapped, and during a period of time I load information in the view of the background. During that time I would like to set the FrontView in white, and then (when the background finish loading) turn to transparent.

In the FrontView I have a Canvas with a bitmap. Is working, and I am able to do this if I want to set the background in transparent

canvas.drawColor(0);

set the background in white

canvas.drawColor(-1);

But I am not able to change white for transparent.

Thank you

agusgambina
  • 6,229
  • 14
  • 54
  • 94
  • 2
    use `Color.TRANSPARENT` and `Color.WHITE` constants. Also, see that answer : http://stackoverflow.com/a/10882301/671543 – njzk2 Jan 27 '15 at 16:04

4 Answers4

24

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.

  • thank you for your answer, is almost working, but the problem is than when the background view is loading information brings the background view to the front. I just realized that. – agusgambina Jan 27 '15 at 16:32
  • @agusgambina You can call `bringToFront()` to the view you want to appear at the top. –  Jan 27 '15 at 16:37
  • thank you. I am using bringToFront() the problem is I do not know why is bringing the "second canvas" to the front when I don't want it. – agusgambina Jan 27 '15 at 16:46
  • 1
    @BaradiBaradari post a new question with your code and Ill take a look at it. –  Jan 09 '17 at 15:47
  • Thank you, have a look when you can http://stackoverflow.com/questions/41551893/how-to-paint-background-canvas-same-color-in-android – David Aleksanyan Jan 09 '17 at 16:01
6

Create a paint

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

And set your canvas

canvas.draw...(... ,  myPaint);
rafaelasguerra
  • 2,685
  • 5
  • 24
  • 56
4

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

canvas.drawColor(ContextCompat.getColor(getContext(), R.color.yourColor));
1

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);
        }
    });
}
agusgambina
  • 6,229
  • 14
  • 54
  • 94