activity中获取view的宽高以及所在屏幕中的位置
2015-11-06 15:24
274 查看
其中mBlueBall为我们想要得到相关参数的view.
如果是mBlueBall的点击事件可以直接得到宽高,原因是点击时activity已经加载了view。
mBlueBall.getRootView().getViewTreeObserver().addOnWindowFocusChangeListener(new OnWindowFocusChangeListener() {
@Override
public void onWindowFocusChanged(boolean hasFocus) {
mBlueBall.post(new Runnable() {
@Override
public void run() {
int width = mBlueBall.getWidth();
int height = mBlueBall.getHeight();
int meturewidth = mBlueBall.getMeasuredWidth();
int metureheight = mBlueBall.getMeasuredHeight();
int[] location=new int[2];
mBlueBall.getLocationOnScreen(location);
float x =location[0] ;
float y = location[1];
// x=mBlueBall.getX();
// y=mBlueBall.getY();
Log.i(TAG, "width"+width+"height"+height+"meturewidth"+meturewidth+"metureheight"+metureheight+"x"+x+"y"+y);
}
});
}
});
如果是mBlueBall的点击事件可以直接得到宽高,原因是点击时activity已经加载了view。
mBlueBall.getRootView().getViewTreeObserver().addOnWindowFocusChangeListener(new OnWindowFocusChangeListener() {
@Override
public void onWindowFocusChanged(boolean hasFocus) {
mBlueBall.post(new Runnable() {
@Override
public void run() {
int width = mBlueBall.getWidth();
int height = mBlueBall.getHeight();
int meturewidth = mBlueBall.getMeasuredWidth();
int metureheight = mBlueBall.getMeasuredHeight();
int[] location=new int[2];
mBlueBall.getLocationOnScreen(location);
float x =location[0] ;
float y = location[1];
// x=mBlueBall.getX();
// y=mBlueBall.getY();
Log.i(TAG, "width"+width+"height"+height+"meturewidth"+meturewidth+"metureheight"+metureheight+"x"+x+"y"+y);
}
});
}
});
相关文章推荐
- bat脚本整理
- jQuery jQuery on()方法
- swift-自动计算字符串的宽高
- 安卓 ScrollView标题栏随手势变化透明
- Android——最佳性能实战
- 实现非常简单的js双向数据绑定
- 安卓编年史(7)
- Android获取时间戳,以及将时间戳转换为时间
- commons-beanutils的copyProperties方法
- 在易景地球三维平台展示三维效果参考
- IAP 的问题汇总
- 鸟哥的linux私房菜学习
- springmvc
- timer实现Grid自动换行(连续相同的id跳到下一行)
- python时间相关
- [bootstrap] 实用的bootstrap模版下载
- The method getDispatcherType() is undefined for the type HttpServletRequest
- jsp页面中修改html元素内容关于单双引号的错误笔记
- 网格部件设置字段样式的智能向导不起作用??
- spring hadoop系列二(MapReduce and Distributed cache)