=============== Problem description ====================
Now I have a requirement to customize the view. The ondraw method is called during initialization. After that, I define a method. When I call this method, I need to call the ondraw method again, however, postinvalidate is invalid. The Code is as follows:
Startup class:
public class MainActivity extends Activity { TestOnDraw view; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); view = (TestOnDraw) findViewById(R.id.test); new Thread(new Runnable() { @Override public void run() { view.test(); } }).start(); }}
Layout file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <com.example.testdraw.TestOnDraw android:id="@+id/test" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>
Custom View
public class TestOnDraw extends View { public TestOnDraw(Context context) { this(context, null); } public TestOnDraw(Context context, AttributeSet attrs) { this(context, attrs, 0); } public TestOnDraw(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); Log.d("test", "in"); } public void test() { postInvalidate(); }}
I checked the log. The ondraw method is only entered once, that is, postinvalidate (); has not taken effect. How can I modify it to enter the ondraw method again when I call the test method after initializing and entering ondraw ()?
============= Solution 1 ======================
The ondraw () method is called only when the interface needs to be displayed on the screen. Android will determine whether an interface needs to be drawn, if you don't need it, you won't call its ondraw method. For example, if you set your custom view to gone, you won't be able to see the log.
In addition, why does the dynamic binding mechanism of Java call the ondraw method of the subclass by default? What does it mean for you to retrieve postinvliade again in the thread?
============= Solution 2 ======================
Reference the reply from luqing414 on the 7th floor:
Quote: reference the reply of kifile on the third floor:
The ondraw () method is called only when the interface needs to be displayed on the screen. Android will determine whether an interface needs to be drawn, if you don't need it, you won't call its ondraw method. For example, if you set your custom view to gone, you won't be able to see the log.
In addition, why does the dynamic binding mechanism of Java call the ondraw method of the subclass by default? What does it mean for you to retrieve postinvliade again in the thread?
I am a program that constantly changes the display shape of this custom view, so we need to re-paint it constantly. I am here to make it clear that other logic is not included.
... Call invaliade in ondraw (). We also recommend using surface view to avoid blocking threads.
============= Solution 3 ======================
Reference the reply from luqing414 on the 6th floor:
Quote: reference the reply from lionfresh on the 5th floor:
In my opinion, when view = (testondraw) findviewbyid (R. Id. Test), ondraw does not delay execution, but does not.
After the command is executed, I say that the test method is cleared to empty implement the test method, and there is no code in it. After that, we can see that ondraw is still called.
Postinvalidate () is to drop a thread in the UI queue and then start the rendering process.
All interfaces in oncreate are not actually generated, so the code in postinvalidate is executed first (this is before the rendering Queue ).
However, this is ineffective. In view, the skipinvalidate () method indicates that the view is dropped by the skip when it is not displayed.
If you have delay for 1-2 seconds, the interface has been generated after the code is executed, and its invalidate takes effect, the desired execution will appear twice.
When the interface is generated, ondraw is called once.
/**
* Invalidate the whole view. If the view is visible,
* {@ Link # ondraw (Android. Graphics. Canvas)} will be called at some point in
* The future. This must be called from a UI thread. To call from a non-UI thread,
* Call {@ link # postinvalidate ()}.
*/
Public void invalidate (){
Invalidate (true );
}
Public void invalidate (rect dirty ){
If (skipinvalidate ()){
Return;
}
}
}
Custom view call ondraw Method