[置顶] Android必知必会-获取View坐标和长宽的时机
2016-05-13 17:19
531 查看
如果移动端访问不佳,请访问–>Github版
![](https://oscdn.geek-share.com/Uploads/Images/Content/201910/29/52e2fcda8731f3bd35737ee1126bafd5)
这里我使用的是
先贴一下获取View坐标的一些资料:
Android View各种尺寸位置相关的方法探究
Android获得控件在屏幕中的绝对坐标
以上这些时机都是不行的。
目前经过测试可用的方法是:
![](http://o73yhoopc.bkt.clouddn.com/5EAA53013EB5AD8401EBF68B86F60F98.gif)
下一篇文章将简单总结一下最近学习的属性动画,和如何实现上图中效果。
PS:
你可以关注的我Github、CSDN和微博
背景
最近要实现一个功能,用到了一些属性动画,需要获取一些View的坐标信息,设计图如下:这里我使用的是
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; }
后话
这是最后的成果:![](http://o73yhoopc.bkt.clouddn.com/5EAA53013EB5AD8401EBF68B86F60F98.gif)
下一篇文章将简单总结一下最近学习的属性动画,和如何实现上图中效果。
PS:
你可以关注的我Github、CSDN和微博
相关文章推荐
- Android必知必会-获取View坐标和长宽的时机
- Android获取系统时间
- android 对象的序列号与反序列
- The JAR of this class file belongs to container 'Android Private Libraries' Android查看外部依赖jar的源码问题解
- android EditText中的inputType
- Android蓝牙开发入门
- Android中ListView的缓存机制
- Xamarin Android 绑定jar库同时将so库打包进去
- Android中RelativeLayout各个属性的含义
- Android 5.0的调度作业JobScheduler
- android SeekBar的自定义样式设置
- Android中activity保存状态和数据到底该在哪个方法中进行
- Android的View绘制流程
- AndroidAnnotations框架的使用详解
- 关于Android 使用服务器公钥RSA加密 数据到服务器RSA解密报错问题
- android保存文件到sd卡,读取和清空记录功能(可以用来保存用户名和密码)
- Android项目中的欢迎界面和引导页:动画和ViewPager
- Android Handler机制分析
- Missing Gradle Project Information
- Android Studio简记