通过实例分析下getDrawingRect,getHitRect,getLocalVisibleRect,getGlobalVisibleRect区别
来源:互联网 发布:vb net入门教程pdf 编辑:程序博客网 时间:2024/05/29 07:23
getLocationOnScreen、getLocationWindow这几个函数的区别。
先看例子源码
布局文件
<LinearLayout 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"
android:background="#ff00ff00"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/location_btn"
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#ff0000ff"
android:onClick="getLocation"
android:text="Test"
android:textSize="40sp" />
</LinearLayout>
代码
package com.example.locationtest;
import android.app.Activity;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnTouchListener{
private Button location_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
location_btn = (Button)findViewById(R.id.location_btn);
location_btn.setOnTouchListener(this);
//location_btn.scrollTo(200, 200);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onTouch(View arg0, MotionEvent event) {
int action = event.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_MOVE:
int x = (int)event.getY();
int rawX = (int)event.getRawY();
//Log.d("lizhihong", "x---" + x +"--rawX---" + rawX);
break;
default:
break;
}
return false;
}
public void getLocation(View view) {
Rect hitRect = new Rect();
Rect drawRect = new Rect();
Rect localVisibleRect = new Rect();
Rect globalVisibleRect = new Rect();
int[] locationOnScreen = new int[2];
int[] locationInWindow = new int[2];
int width = location_btn.getWidth();
int height = location_btn.getHeight();
int paddingLeft = location_btn.getPaddingLeft();
int paddingTop = location_btn.getPaddingTop();
int leftToParent = location_btn.getLeft();
int topToParent = location_btn.getTop();
location_btn.getHitRect(hitRect);
location_btn.getDrawingRect(drawRect);
location_btn.getLocalVisibleRect(localVisibleRect);
location_btn.getGlobalVisibleRect(globalVisibleRect);
location_btn.getLocationOnScreen(locationOnScreen);
location_btn.getLocationInWindow(locationInWindow);
Log.d("TestLocation", "width---" + width + "---height---" + height);
Log.d("TestLocation", "paddingLeft---" + paddingLeft + "---paddingTop---" + paddingTop);
Log.d("TestLocation", "leftToParent---" + leftToParent + "---topToParent---" + topToParent);
Log.d("TestLocation", "hitRect---" + hitRect.toString());
Log.d("TestLocation", "drawRect---" + drawRect.toString());
Log.d("TestLocation", "localVisibleRect---" + localVisibleRect.toString());
Log.d("TestLocation", "globalVisibleRect---" + globalVisibleRect.toString());
Log.d("TestLocation", "locationOnScreen---x--" + locationOnScreen[0] + "--y---" + locationOnScreen[1]);
Log.d("TestLocation", "locationInWindow---x--" + locationInWindow[0] + "--y---" + locationInWindow[1]);
}
}
我们先看下输出的log
11-04 23:44:44.353: D/TestLocation(1994): width---300---height---300--------button的宽度高度
11-04 23:44:44.353: D/TestLocation(1994): paddingLeft---18---paddingTop---12---button的text距离button左上角的距离
11-04 23:44:44.363: D/TestLocation(1994): leftToParent---90---topToParent---195------button距离父窗口也即是绿色窗口为坐标原点的距离
11-04 23:44:44.363: D/TestLocation(1994): hitRect---Rect(90, 195 - 390, 495)----button在父窗口中的矩形区域,坐标以父窗口左上角为坐标原点计算
11-04 23:44:44.363: D/TestLocation(1994): drawRect---Rect(0, 0 - 300, 300)----这个是以button左上角为坐标原点计算,button的矩形区域
11-04 23:44:44.363: D/TestLocation(1994): localVisibleRect---Rect(0, 0 - 300, 300)----这个是以button左上角为坐标原点计算,button的矩形区域
11-04 23:44:44.363: D/TestLocation(1994): globalVisibleRect---Rect(90, 305 - 390, 605)---这个是一窗口左上角为坐标原点,button的显示区域
11-04 23:44:44.363: D/TestLocation(1994): locationOnScreen---x--90--y---305----这个是以窗口左上角为坐标原点,button左上角的坐标
11-04 23:44:44.363: D/TestLocation(1994): locationInWindow---x--90--y---305----这个是以窗口左上角为坐标原点,button左上角的坐标
从上边的例子我们大致得出以上结论
我们再看下把button 在屏幕中offset一定距离后
先看修改的部分源码
package com.example.locationtest;
import android.app.Activity;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnTouchListener{
private Button location_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
location_btn = (Button)findViewById(R.id.location_btn);
location_btn.setOnTouchListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onTouch(View arg0, MotionEvent event) {
int action = event.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_MOVE:
int x = (int)event.getY();
int rawX = (int)event.getRawY();
//Log.d("lizhihong", "x---" + x +"--rawX---" + rawX);
break;
default:
break;
}
return false;
}
public void getLocation(View view) {
location_btn.scrollTo(50, 50);//获得坐标之前先把button文本偏移一点的距离
Rect hitRect = new Rect();
Rect drawRect = new Rect();
Rect localVisibleRect = new Rect();
Rect globalVisibleRect = new Rect();
int[] locationOnScreen = new int[2];
int[] locationInWindow = new int[2];
int width = location_btn.getWidth();
int height = location_btn.getHeight();
int paddingLeft = location_btn.getPaddingLeft();
int paddingTop = location_btn.getPaddingTop();
int leftToParent = location_btn.getLeft();
int topToParent = location_btn.getTop();
location_btn.getHitRect(hitRect);
location_btn.getDrawingRect(drawRect);
location_btn.getLocalVisibleRect(localVisibleRect);
location_btn.getGlobalVisibleRect(globalVisibleRect);
location_btn.getLocationOnScreen(locationOnScreen);
location_btn.getLocationInWindow(locationInWindow);
Log.d("TestLocation", "width---" + width + "---height---" + height);
Log.d("TestLocation", "paddingLeft---" + paddingLeft + "---paddingTop---" + paddingTop);
Log.d("TestLocation", "leftToParent---" + leftToParent + "---topToParent---" + topToParent);
Log.d("TestLocation", "hitRect---" + hitRect.toString());
Log.d("TestLocation", "drawRect---" + drawRect.toString());
Log.d("TestLocation", "localVisibleRect---" + localVisibleRect.toString());
Log.d("TestLocation", "globalVisibleRect---" + globalVisibleRect.toString());
Log.d("TestLocation", "locationOnScreen---x--" + locationOnScreen[0] + "--y---" + locationOnScreen[1]);
Log.d("TestLocation", "locationInWindow---x--" + locationInWindow[0] + "--y---" + locationInWindow[1]);
}
}
可以看到scroll之后button中的text往左上角移动了50像素。
偏移之后的Log
11-05 00:16:28.133: D/TestLocation(2246): width---300---height---300
11-05 00:16:28.133: D/TestLocation(2246): paddingLeft---0---paddingTop---0---------偏移出显示范围padding为0了
11-05 00:16:28.143: D/TestLocation(2246): leftToParent---90---topToParent---195
11-05 00:16:28.143: D/TestLocation(2246): hitRect---Rect(90, 195 - 390, 495)
11-05 00:16:28.143: D/TestLocation(2246): drawRect---Rect(50, 50 - 350, 350)----这个随偏移变了
11-05 00:16:28.143: D/TestLocation(2246): localVisibleRect---Rect(50, 50 - 350, 350)----这个随偏移变了
11-05 00:16:28.143: D/TestLocation(2246): globalVisibleRect---Rect(90, 305 - 390, 605)
11-05 00:16:28.163: D/TestLocation(2246): locationOnScreen---x--90--y---305
11-05 00:16:28.163: D/TestLocation(2246): locationInWindow---x--90--y---305
这说明了drawRect localVisibleRect是以自身左上角为坐标原点,当scrollTo之后把button的drawRect偏移了50之后我们获得的是偏移后以button左上角为坐标原点的坐标。
- 通过实例分析下getDrawingRect,getHitRect,getLocalVisibleRect,getGlobalVisibleRect区别
- Android获取View位置getHitRect、getDrawingRect、getLocalVisibleRect、getGlobalVisibleRect说明
- getGlobalVisibleRect跟getLocalVisibleRect
- getGlobalVisibleRect跟getLocalVisibleRect
- getGlobalVisibleRect和getLocalVisibleRect
- getGlobalVisibleRect和getLocalVisibleRect
- [转]getGlobalVisibleRect和getLocalVisibleRect
- Android View api 详解-LocationInWindow,getLocationOnScreen,getGlobalVisibleRect,getLocalVisibleRect
- Android 判断一个 View 是否可见 getLocalVisibleRect(rect) 与 getGlobalVisibleRect(rect)
- 线程与进程的区别以及通过实例的详细分析
- 通过实例来分析I2C基本通信协议
- linux下mmap实例分析
- getHitRect的作用
- linux下线程通过signal通信实例
- 实例分析varchar和nvarchar的区别
- Linux下通过 top 分析 ndbd 进程
- Centos6下通过 oprofile分析CPU性能
- windows下通过map,cod分析堆栈
- 关于sizeof和strlen
- Win7下使用AndBug
- C++设计模式[六]桥接模式
- Numpy 入门教程(2)
- vi常用设置
- 通过实例分析下getDrawingRect,getHitRect,getLocalVisibleRect,getGlobalVisibleRect区别
- MCV |--------- EF的增删查改-------
- 内存管理器(十一)kernel内存管理---页的获取与释放
- iOS 缓存的路径与缓存
- NumPy的详细教程
- Extjs--点击一个按钮,打开一个新的窗体window重复创建的问题
- BLE按键整理
- ios开发-mac终端命令总集
- cf B. Anton and Lines (STL)