Android必知必会-获取View坐标和长宽的时机

如果移动端访问不佳,请访问–>Github版

背景

最近要实现一个功能,用到了一些属性动画,需要获取一些View的坐标信息,设计图如下:

design

这里我使用的是DialogFragment来实现的,可以复用。

先贴一下获取View坐标的一些资料:

Android View各种尺寸位置相关的方法探究

Android获得控件在屏幕中的绝对坐标

合适的时机

要想获得View的坐标和长宽,必须要等到View绘制完毕,在平常写代码的生命周期函数内一般是获取不到的:

//Activity
onCreate();
onResume();
//Fragmen
onCreate();
onCreateView();
onResume();
onShow();
setUserVisibleHint();

以上这些时机都是不行的。

目前经过测试可用的方法是:

/*
 * 对于:Activity
 * 覆写 onWindowFocusChanged(boolean hasFocus)方法
 */
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        int[] location1 = new int[2] ;
        view.getLocationInWindow(location1); //获取在当前窗口内的绝对坐标
        int[] location2 = new int[2] ;
        view.getLocationOnScreen(location2);//获取在整个屏幕内的绝对坐标
        //do something
    }

/*
 * 对于:Fragmen
 * 在 onCreateView()中为View添加addOnGlobalLayoutListener
 */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.xxx, container, false);
        ViewTreeObserver vto = view.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new  ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //在这里获取View及其子控件的坐标和长宽信息
            }
        });
      //do something
        return view;
    }
/*
 * 对于:DialogFragment
 * 在 onCreateDialog()中为View添加addOnGlobalLayoutListener
 * PS: start-end之间的是重点,其他可以不看
 */
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.xxxx, null);
      //-----获取View及其子控件的坐标和长宽信息 start----
        ViewTreeObserver vto = view.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //在这里获取View及其子控件的坐标和长宽信息
            }
        });
      //-----获取View及其子控件的坐标和长宽信息 end----

        Dialog dialog = new Dialog(getActivity(), R.style.CustomCityPickerDialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(true);
        ButterKnife.bind(this, view);
        // 设置宽度为屏宽、靠近屏幕底部。
        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);
        return dialog;
    }

后话

这是最后的成果:
成果

下一篇文章将简单总结一下最近学习的属性动画,和如何实现上图中效果。

PS:

你可以关注的我GithubCSDN微博

原文地址:https://www.cnblogs.com/wuyida/p/6300466.html