Android obtains the View component width and ViewTreeObserver

Source: Internet
Author: User

Android obtains the View component width and ViewTreeObserver

View width and height measurement method:

There are three measurement methods:

1) (executed directly in onCreate)

Int w = View. MeasureSpec. makeMeasureSpec (0, View. MeasureSpec. UNSPECIFIED );
Int h = View. MeasureSpec. makeMeasureSpec (0, View. MeasureSpec. UNSPECIFIED );
ImageView. measure (w, h );
Int height = imageView. getMeasuredHeight ();
Int width = imageView. getMeasuredWidth ();
TextView. append ("\ n" + height + "," + width );

2) Both 2 and 3 get the component width and height after onCreate () is called.

ViewTreeObserver vto = imageView. getViewTreeObserver ();
Vto. addOnPreDrawListener (new ViewTreeObserver. OnPreDrawListener (){
Public boolean onPreDraw (){
Int height = imageView. getMeasuredHeight ();
Int width = imageView. getMeasuredWidth ();
TextView. append ("\ n" + height + "," + width );
Return true;
}
});

3)

ViewTreeObserver vto2 = imageView. getViewTreeObserver ();
Vto2.addOnGlobalLayoutListener (new OnGlobalLayoutListener (){
@ Override
Public void onGlobalLayout (){
ImageView. getViewTreeObserver (). removeGlobalOnLayoutListener (this );
TextView. append ("\ n" + imageView. getHeight () + "," + imageView. getWidth ());
}
});

Note: method 1 performs onMeasure calculation once more than other methods, and the callback function of method 2 is called multiple times.

ViewTreeObserver

I. Structure

Public final class ViewTreeObserver extends Object
Java. lang. Object
Android. view. ViewTreeObserver

Ii. Overview

This is an observer (observer) that registers the monitoring view tree and is notified when the global event of the view tree changes. This global event not only includes the layout of the entire tree, but also changes in the touch mode from the painting process. ViewTreeObserver cannot be instantiated by the application because it is provided by the view. For more information, see getViewTreeObserver.

Iii. Internal class

Interface ViewTreeObserver. OnGlobalFocusChangeListener
// Interface class of the callback function to be called when the focus status in a view tree changes
 
Interface ViewTreeObserver. OnGlobalLayoutListener
// Interface class of the callback function to be called when the global layout of a view tree is changed or the visual status of a view in the view tree is changed
 
Interface ViewTreeObserver. OnPreDrawListener
// Interface class of the callback function to be called when a view tree is to be drawn
 
Interface ViewTreeObserver. OnScrollChangedListener
// Interface class of the callback function to be called when some components in a view tree scroll
 
Interface ViewTreeObserver. OnTouchModeChangeListener
// The interface class of the callback function to be called when the touch mode of a view tree changes

Iv. Public Methods

/** Register a callback function. This callback function is called when the focus status in a view tree changes.
* Callback function to be added with the listener Parameter
* Exception IllegalStateException if isAlive () returns false
*/
Public void addOnGlobalFocusChangeListener (ViewTreeObserver. OnGlobalFocusChangeListener listener)

 
/** Registers a callback function. This callback function is called when the global layout of a view tree changes or the visual status of a view in the view tree changes.
* Callback function to be added with the listener Parameter
* Exception IllegalStateException if isAlive () returns false
*/
Public void addOnGlobalLayoutListener (ViewTreeObserver. OnGlobalLayoutListener listener)
 
  
/** Register a callback function, which is called when a view tree is to be drawn.
* Callback function to be added with the listener Parameter
* Exception IllegalStateException if isAlive () returns false
*/
Public void addOnPreDrawListener (ViewTreeObserver. OnPreDrawListener listener)
 

/** Register a callback function, which is called when a view is rolled.
* Callback function to be added with the listener Parameter
* Exception IllegalStateException if isAlive () returns false
*/
Public void addOnScrollChangedListener (ViewTreeObserver. OnScrollChangedListener listener)
 
 
/** Register a callback function, which is called when the touch mode changes.
* Callback function to be added with the listener Parameter
* Exception IllegalStateException if isAlive () returns false
*/
Public void addOnTouchModeChangeListener (ViewTreeObserver. OnTouchModeChangeListener listener)
 
  
// Notify the corresponding registration listener when the entire layout changes. If you force the view layout or a view that is not attached to a window or in the GONE status, it can be manually called
Public final void dispatchOnGlobalLayout ()

/** Notify the corresponding registration listener when a view tree is to be drawn. If the listener returns true, the painting will be canceled and rescheduled. If you force a view layout or a view hierarchy that is not attached to a window, or in a GONE state, it can be manually called
* If the current painting can be canceled and rescheduled, true is returned; otherwise, false is returned.
*/
Public final boolean dispatchOnPreDraw ()
 
/** Indicates whether the current ViewTreeObserver is available (alive ). When the observer is unavailable, any method call (except this method) will throw an exception. If an application maintains a long reference with ViewTreeObserver, it always needs to detect the return value of this method before calling other methods.
* Return value. If this object is available, true is returned. Otherwise, false is returned.
*/
Public boolean isAlive ()


/** Remove the previously registered global layout callback function.
* Callback function with the victim parameter to be removed
* Exception IllegalStateException if isAlive () returns false
*/
Public void removeGlobalOnLayoutListener (ViewTreeObserver. OnGlobalLayoutListener victim)
  
/** Remove the previously registered focus change callback function.
* Callback function with the victim parameter to be removed
* Exception IllegalStateException if isAlive () returns false
*/
Public void removeOnGlobalFocusChangeListener (ViewTreeObserver. OnGlobalFocusChangeListener victim)
  
/** Remove the previously registered pre-drawn callback function.
* Callback function with the victim parameter to be removed
* Exception IllegalStateException if isAlive () returns false
*/
Public void removeOnPreDrawListener (ViewTreeObserver. OnPreDrawListener victim)
  
/** Remove the previously registered rolling change callback function.
* Callback function with the victim parameter to be removed
* Exception IllegalStateException if isAlive () returns false
*/
Public void removeOnScrollChangedListener (ViewTreeObserver. OnScrollChangedListener victim)
 
/** Remove the previously registered touch mode change callback function
* Callback function with the victim parameter to be removed
* Exception IllegalStateException if isAlive () returns false
*/
Public void removeOnTouchModeChangeListener (ViewTreeObserver. OnTouchModeChangeListener victim)

V. Sample Code

1. Create a listener

Private final ViewTreeObserver. OnGlobalLayoutListener mGlobalLayoutListener = new ViewTreeObserver. OnGlobalLayoutListener (){
@ Override
Public void onGlobalLayout (){
Int width =-1;
Int height =-1;

Try {
Width = getActivity (). getWindow (). getDecorView (). getWidth ();
Height = getActivity (). getWindow (). getDecorView (). getHeight ();
} Catch (Exception e ){
// Called too early. so, just skip.
}

If (width! =-1 & mGlobalLayoutWidth! = Width) {// The UI update is meaningful only when the size has a value, that is, the value has been determined.
MGlobalLayoutWidth = width;
UpdateUI ();
} Else if (height! =-1 & mGlobalLayoutHeight! = Height ){
MGlobalLayoutHeight = height;
UpdateUI ();
}}};

2. Generally, the listener is registered in onCreate or onCreateView.

MViewTreeObserver = getActivity (). getWindow (). getDecorView (). getViewTreeObserver ();
MViewTreeObserver. addOnGlobalLayoutListener (mGlobalLayoutListener );

Https://www.bkjia.com/topicnews.aspx? Tid = 5

This article permanently updates link: https://www.bkjia.com/Linux/2018-02/151003.htm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.