Android onDraw () runs in all views?

Here is a weird behavior that I don't understand about custom views. I have two views in the frame layout, one on top of the other. The views are simple and I made them 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 is well packed on 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 running onTouch from View1 does the onDraw () method view? Why not just View1?

Subsequently, Edit View2 has a large image for drawing, the image is rotated and scaled in accordance with some saved settings. Usually, when I run an Activity, View2.onDraw () takes care of rotating, translating, and scaling the bitmap and drawing it on the canvas. When the user touches View1, I want to execute only View1.onDraw (), because it makes no sense to redraw the same background image over and over for each user interaction. How can I stop View2.onDraw () to execute?

+4
source share
1 answer

I think this should answer your question:

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

"Called when something has changed, which invalidates the layout of the child of this view parent. This will schedule the layout of the views tree to go through."

+1
source

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


All Articles