欢迎使用CSDN-markdown编辑器

来源:互联网 发布:动态壁纸软件情侣 编辑:程序博客网 时间:2024/05/30 19:34

代码布局文件:

<FrameLayout   xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity">    <Button        android:id="@+id/click_show"        android:layout_width="100dp"        android:layout_height="60dp"        android:layout_marginLeft="-20dp"        android:text="click"/></FrameLayout>

代码:

@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mClickBtn = (Button) findViewById(R.id.click_show);        mClickBtn.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                Rect r = new Rect();                mClickBtn.getDrawingRect(r);                Log.e("cyx", "drawing rect == " + r);                mClickBtn.getHitRect(r);                Log.e("cyx", "hit rect == " + r);                mClickBtn.getLocalVisibleRect(r);                Log.e("cyx", "local visible rect == " + r);                mClickBtn.getGlobalVisibleRect(r);                Log.e("cyx", "global visible rect == " + r);                int[] location = new int[2];                mClickBtn.getLocationInWindow(location);                Log.e("cyx", "getLocationInWindow == " + location[0] + ", " + location[1]);                mClickBtn.getLocationOnScreen(location);                Log.e("cyx", "getLocationOnScreen == " + location[0] + ", " + location[1]);            }        });}

结果:
drawing rect == Rect(0, 0 - 200, 120)
hit rect == Rect(-39, 0 - 161, 120)
local visible rect == Rect(39, 0 - 200, 120)
global visible rect == Rect(0, 162 - 161, 282)
getLocationInWindow == -38, 162
getLocationOnScreen == -38, 162

总结:
getDrawingRect(): 返回view自身原始的宽高;
getHitRect(): 相对于坐标原点而言,view所在的矩形区域位置;
getLocalVisibleRect(): view在当前screen可见区域内,测量出来的宽高;
getGlobalVisibleRect(): view在当前screen可见区域内,相对于屏幕顶点(非坐标原点)的宽高;
getLocationOnScreen(): view的左上角顶点相对于屏幕顶点的坐标;
getLocationInWindow():同getLocationOnScreen()一样,只是dialog时处理不同;

0 0
原创粉丝点击