View基础知识

来源:互联网 发布:ubuntu sys time.h 编辑:程序博客网 时间:2024/06/08 04:45

Android屏幕区域划分
这里写图片描述

这里写图片描述
1). 位置参数
View的静态坐标方法 (相对于父布局,以父布局左上角为坐标原点)
getLeft() 返回View自身左边到父布局左边的距离
getTop() 返回View自身顶边到父布局顶边的距离
getRight() 返回View自身右边到父布局左边的距离
getBottom() 返回View自身底边到父布局顶边的距离
getX() 返回值为View左上角顶点横坐标X,X=getLeft()+getTranslationX(),当setTranslationX()时getLeft()不变,getX()变化。
getY() 返回值为View左上角顶点纵坐标Y,Y=getTop()+getTranslationY(),当setTranslationY()时getTop()不变,getY()变。

2).View获取宽高方法
getWidth() layout后有效,返回值是getRight()-getLeft(),一般会参考measure的宽度(measure可能没用),但不是必须的。
getHeight() layout后有效,返回值是getBottom()-getTop(),一般会参考measure的高度(measure可能没用),但不是必须的。
getMeasuredWidth() 返回measure过程得到的mMeasuredWidth值,供layout参考,或许没用。
getMeasuredHeight() 返回measure过程得到的mMeasuredHeight值,供layout参考,或许没用。

3).MotionEvent坐标方法 (相对于View本身,以View左上角为坐标原点)
getX() 当前触摸事件距离View本身左边的距离
getY() 当前触摸事件距离View本身顶边的距离
getRawX() 当前触摸事件距离整个屏幕左边的距离
getRawY() 当前触摸事件距离整个屏幕顶边的距离

4).View的滑动方法
offsetLeftAndRight(int offset) 水平方向挪动View,offset为正则x轴正向移动,移动的是整个View,getLeft()会变的,自定义View很有用。
offsetTopAndBottom(int offset) 垂直方向挪动View,offset为正则y轴正向移动,移动的是整个View,getTop()会变的,自定义View很有用。
scrollTo(int x, int y) 将View中内容(不是整个View)滑动到相应的位置,参考坐标原点为ParentView左上角,x,y为正则向xy轴反方向移动,反之同理。
scrollBy(int x, int y) 在scrollTo()的基础上继续滑动xy。

特别注意:View的scrollTo()和scrollBy()是用于滑动View中的内容,而不是改变View的位置;改变View在屏幕中的位置可以使用offsetLeftAndRight()和offsetTopAndBottom()方法,他会导致getLeft()等值改变。

5).Android绝对坐标系即屏幕坐标系
以状态栏左上角为原点,向右为x轴正方向, 向下为Y轴正方向。

6).View获取屏幕中位置的方法
这些方法需要在Activity的onWindowFocusChanged ()方法之后才能使用
getLocalVisibleRect() 获取View自身可见的坐标区域,坐标以自己的左上角为原点(0,0),另一点为可见区域右下角相对自己(0,0)点的坐标。
getGlobalVisibleRect() 获取View在屏幕绝对坐标系中的可视区域,坐标以屏幕左上角为原点(0,0),另一个点为可见区域右下角相对屏幕原点(0,0)点的坐标。
getLocationOnScreen() 坐标是相对整个屏幕而言,Y坐标为View左上角到屏幕顶部的距离。
getLocationInWindow()如果为普通Activity则Y坐标为View左上角到屏幕顶部(此时Window与屏幕一样大);如果为对话框式的Activity则Y坐标为当前Dialog模式Activity的标题栏顶部到View左上角的距离。

7)其他

//获取屏幕区域的宽高等尺寸获取DisplayMetrics metrics = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(metrics);int widthPixels = metrics.widthPixels;int heightPixels = metrics.heightPixels;//应用程序App区域宽高等尺寸获取Rect rect = new Rect();getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);//获取状态栏高度Rect rect= new Rect();getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);int statusBarHeight = rectangle.top;//View布局区域宽高等尺寸获取Rect rect = new Rect();  getWindow().findViewById(Window.ID_ANDROID_CONTENT).getDrawingRect(rect);  

8).TouchSlop
TouchSloup是系统所能识别出的被认为是滑动的最小距离,这是一个常量,与设备有关,可通过以下方法获得:

ViewConfiguration.get(getContext()).getScaledTouchSloup()

9).VelocityTracker对象
速度追踪,用于追踪手指在滑动过程中的速度,包括水平方向速度和竖直方向速度。 使用方法:
1.在View的onTouchEvent方法中追踪当前单击事件的速度
VelocityRracker velocityTracker = VelocityTracker.obtain();
velocityTracker.addMovement(event);
2.计算速度,获得水平速度和竖直速度
//1000指的是1000毫秒,得到的是1000毫秒内滑过的像素数
velocityTracker.computeCurrentVelocity(1000);
int xVelocity = (int)velocityTracker.getXVelocity();
int yVelocity = (int)velocityTracker.getYVelocity();
3.当不需要使用的时候,需要调用clear()方法重置并回收内存
velocityTracker.clear();
velocityTracker.recycle();

10).GestureDetector对象
手势检测,用于辅助检测用户的单击、滑动、长按、双击等行为。
示例:

GestureDetector mGestureDetector = new GestureDetector(this);//解决长按屏幕后无法拖动的现象mGestureDetector.setIsLongpressEnabled(false);在目标View的OnTouchEvent方法中添加以下实现,便可接管目标View的onTouchEvent 方法:boolean consume = mGestureDetector.onTouchEvent(event);return consume;
原创粉丝点击