Reprint Please specify source: http://blog.csdn.net/guolin_blog/article/details/17045157
View state and redraw
The View is stateful. For example, click on the time, click on a change of appearance.
First, view state
A total of more than 10 types, we will only use a few of them
1. Enabled
Indicates whether the current view is available. You can call the setenable() method to change the available state of the view, pass in true to indicate availability, and pass in false to indicate that it is unavailable. The biggest difference between them is that the unavailable view is not responsive to the Ontouch event .
2. Focused
Indicates whether the current view is gaining focus. There are usually two ways to get the focus of the view,
That is, through the keyboard to switch views around the next key, and call the Requestfocus () method. And now the Android phone almost no keyboard,
So basically only the Requestfocus () method can be used to get the focus of the view. The Requestfocus () method does not guarantee that the view has the focus, it will have a Boolean return value, and if True indicates that the focus is successful, the return false indicates that the focus failed. It is generally only when the view is set up in focusable and focusable in touch mode that the focus can be successfully obtained, such as EditText.
3. window_focused
Indicates whether the current view is in an interactive window, and the value is automatically determined by the system and the application cannot be changed.
4. Selected
Indicates whether the current view is selected. One interface can have more than one view selected, call the SetSelected () method to change the view of the selected state, passed in true to indicate that the check, passed false indicates unchecked.
5. Pressed
Indicates whether the current view is in the pressed state. You can call the Setpressed () method to change this state by passing in true to indicate that the pass-through false indicates that it was not pressed. Normally this state is automatically assigned by the system, but developers can call this method to make changes themselves.
We can create a selector file in the drawable directory of the project, where the background image of the view is configured in each state. For example, create a compose_bg.xml file, and write the following code in it:
<selectorxmlns:android= "Http://schemas.android.com/apk/res/android"> <Itemandroid:drawable= "@drawable/compose_pressed"android:state_pressed= "true"></Item> <Itemandroid:drawable= "@drawable/compose_pressed"android:state_focused= "true"></Item> <Itemandroid:drawable= "@drawable/compose_normal"></Item> </selector>
This code means that when the view is in a normal state, the background image of Compose_normal is displayed, and when the view gets to the focus or is pressed, the compose_pressed is displayed.
Once this selector file is created, we can use it in layout or code, such as setting it as the background of a button, as shown below:
<?XML version= "1.0" encoding= "Utf-8"?> <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" > <ButtonAndroid:id= "@+id/compose"Android:layout_width= "60DP"Android:layout_height= "40DP"android:layout_gravity= "Center_horizontal"Android:background= "@drawable/compose_bg" /> </LinearLayout>
The drawablestatechanged() method of the view is recalled whenever the state of the views has changed
protected void drawablestatechanged() { = mbgdrawable; if null && d.isstateful ()) { d.setstate (getdrawablestate ()); } }
Second, view redraw
Although the view is automatically drawn to the screen after the activity is loaded, there is every reason to ask for dynamic updates to the view when interacting with the activity, such as changing the state of the view and displaying or hide a control, and so on. At this point, the previously drawn view is actually out of date, and we should redraw the image.
Calling methods such as setvisibility (), setenabled (), setselected () of the view causes the view to redraw, and if we want to manually force the view to redraw, you can call invalidate () method to implement. Of course, the interior of Setvisibility (), setenabled (), setselected () is actually implemented by calling the invalidate () method, so let's take a look at the invalidate () What is the code of the method?
There will be several overloads of the Invalidate () method and a Invalidatedrawable () method in the view's source code, and of course they all have the same principle, so we only analyze one of them, and the code looks like this:
voidInvalidateBooleanInvalidatecache) { if(viewdebug.trace_hierarchy) {viewdebug.trace ( This, ViewDebug.HierarchyTraceType.INVALIDATE); } if(Skipinvalidate ()) {//Use it to determine if the view needs to be redrawn. return; } if(Mprivateflags & (Drawn | has_bounds) = = (Drawn | Has_bounds) | |(Invalidatecache&& (Mprivateflags & drawing_cache_valid) = = Drawing_cache_valid) | |(Mprivateflags& invalidated)! = invalidated | | Isopaque ()! =Mlastisopaque) {Mlastisopaque=Isopaque (); Mprivateflags&= ~drawn; Mprivateflags|=DIRTY; if(invalidatecache) {mprivateflags|=invalidated; Mprivateflags&= ~Drawing_cache_valid; } FinalAttachinfo ai =Mattachinfo; Final viewparentp =mparent; Here's Viewparent's Invalidatechild ()if(!hardwarerenderer.render_dirty_regions) { if(P! =NULL&& ai! =NULL&&ai.mhardwareaccelerated) {p.invalidatechild ( This,NULL); return; } } if(P! =NULL&& ai! =NULL) { FinalRect r =Ai.mtmpinvalrect; R.set (0, 0, Mright-mleft, Mbottom-mtop); P.invalidatechild ( This, R); } } }
android--custom Controls (iii)