Android must know-the time to get the view coordinates and the length-width

Source: Internet
Author: User

If mobile access is poor, please visit version –>github

Background

Recently to implement a function, using a number of property animation, need to get some view of the coordinate information, the design diagram is as follows:

Here I use DialogFragment to implement, can be reused.

Let's get some information about the view coordinates:

Android View various size location-related methods Explore

Android gets the absolute coordinates of the control in the screen

The right time

To get the coordinates and the width of the view, you have to wait until the view is drawn, which is generally not available within the life cycle function of the normal code:

//ActivityonCreate();onResume();//FragmenonCreate();onCreateView();onResume();onShow();setUserVisibleHint();

None of these opportunities are possible.

The methods currently available for testing are:

/* For: Activity * Overwrite onwindowfocuschanged (Boolean Hasfocus) method */    @Override     Public void onwindowfocuschanged(BooleanHasfocus) {Super. onwindowfocuschanged (Hasfocus);int[] Location1 =New int[2] ; View.getlocationinwindow (Location1);//Gets the absolute coordinates within the current window        int[] Location2 =New int[2] ; View.getlocationonscreen (Location2);//Get absolute coordinates over the entire screen        //do something}/ * * for: Fragmen * Add Addongloballayoutlistener to view in Oncreateview () * *    @Override     PublicViewOncreateview(Layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {View view = Inflater.inflate (r.layout.xxx, container,false);        Viewtreeobserver vto = View.getviewtreeobserver (); Vto.addongloballayoutlistener (NewViewtreeobserver.ongloballayoutlistener () {@Override             Public void Ongloballayout() {//Get coordinates and length-width information for the view and its child controls here}        });//do something        returnView }/ * for: Dialogfragment * in Oncreatedialog () to add addongloballayoutlistener to view * Ps:start-end is the focus, others can not see */    @Override     PublicDialogOncreatedialog(Bundle savedinstancestate)        {Layoutinflater inflater = getactivity (). Getlayoutinflater (); View view = Inflater.inflate (R.layout.xxxx,NULL);//-----Gets the coordinates and the length-width information of the view and its child controls start----Viewtreeobserver vto = View.getviewtreeobserver (); Vto.addongloballayoutlistener (NewViewtreeobserver.ongloballayoutlistener () {@Override             Public void Ongloballayout() {//Get coordinates and length-width information for the view and its child controls here}        });//-----Gets the coordinates and the length-width information end of the view and its child controls----Dialog Dialog =NewDialog (Getactivity (), r.style.customcitypickerdialog);        Dialog.requestwindowfeature (Window.feature_no_title);        Dialog.setcontentview (view); Dialog.setcanceledontouchoutside (true); Butterknife.bind ( This, view);//Set the width to the screen width and close to the bottom of the screen. window window = Dialog.getwindow ();        Window.setbackgrounddrawableresource (r.color.transparent);        Windowmanager.layoutparams WLP = Window.getattributes ();        wlp.gravity = Gravity.bottom;        Wlp.width = WindowManager.LayoutParams.MATCH_PARENT; Window.setattributes (WLP);returnDialog }
Something

This is the final result:

The next article will briefly summarize the recently learned properties animation and how to achieve the effect.

Ps:

You can focus on my github, Csdn and Weibo

Android must know-the time to get the view coordinates and the length-width

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.