Search code examples
androidandroid-canvas

Android canvas change background color


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


Solution

  • 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);
            }
        });
    }