Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android onDraw() executing on all Views?

Here is a weird behavior that I don't understand about custom views. I have two views in a frame layout, one on top of the other. The views are simple and I've made them just for a short test

public class View1  extends View  {
....

    @Override
     public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN){
            this.updateDrawings();
        }
        return true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (canvas != null) 
        {
            Log.i("Test", "Draw View1");
        }
    }

    public void updateDrawings() {
        try {
            this.invalidate();
        }
        finally {
        }
    }
}

and View2

public class View2  extends View  {
....

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (canvas != null) 
        {
            Log.i("Test", "Draw View2");
        }
    }

    public void updateDrawings() {
        try {
            this.invalidate();
        }
        finally {
        }
    }
}

Everything well packed on a FrameLayout

<FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="#ffffff">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1">

            <com.View2
                android:layout_centerInParent="true"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_marginBottom="5dip" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1">

            <com.View1
                android:layout_centerInParent="true"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_marginBottom="5dip" />
        </LinearLayout>
</FrameLayout>

My question is: why when onTouch executes from View1, both view's onDraw() method executes ? Why not only View1's ?

Later Edit View2 has a big image to draw, the image is rotated and scaled according to some saved preferences. Normally, when I start the Activity, the View2.onDraw() takes care of rotating, translating and scaling of the Bitmap and draw it on canvas. When the user touches View1 I want only View1.onDraw() to execute because there's no point to redraw the same background image over and over again for each user interaction. How can I stop View2.onDraw() to execute ?

like image 721
Alin Avatar asked Nov 14 '22 14:11

Alin


1 Answers

I think this should answer your question:

http://developer.android.com/reference/android/view/ViewParent.html#requestLayout()

"Called when something has changed which has invalidated the layout of a child of this view parent. This will schedule a layout pass of the view tree."

like image 95
Jordy Langen Avatar answered Nov 16 '22 03:11

Jordy Langen