Android坐标系统

1.获取屏幕区域的宽高尺寸

DisplayMetrics metrics = new DisplayMetrics();
context.getSystemService(Context.WINDOW_SERVICE).getDefaultDisplay().getMetrics(metrics);
int widthPixels = metrics.widthPixels;
int heightPixels = metrics.heightPixels;
context.getWindowManager().getDefaultDisplay().getWidth()/getHeight();
context.getResources().getDisplayMetrics().widthPixels;

context.getResources().getDisplayMetrics().heightPixels;

context.getResources().getDisplayMetrics().density;

2.应用程序App区域宽高等尺寸

Rect rect = new Rect();
getWindow().getDecorWindow().getWindowVisibleDisplayFrame(rect);

3.状态栏高度

Rect rect= new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
int statusBarHeight = rectangle.top;
int resourceId = context.getResources()
            .getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
        statusBarHeight= context.getResources().getDimensionPixelSize(resourceId);
    }

4.ActionBar高度

TypedValue tv = new TypedValue();
if(activity.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
int actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, activity.getResources().getDisplayMetrics());
}
getActionBar().getHeight();

5.View布局区域宽高等尺寸获取

Rect rect = new Rect();  
getWindow().findViewById(Window.ID_ANDROID_CONTENT).getDrawingRect(rect);  

6.View的静态坐标方法
- getLeft() 返回View自身左边到父布局左边的距离
- getTop() 返回View自身顶边到父布局顶边的距离
- getRight() 返回View自身右边到父布局左边的距离
- getBottom() 返回View自身底边到父布局顶边的距离
- getX() 返回值为getLeft()+getTranslationX(),当setTranslationX()时getLeft()不变,getX()变。
- getY() 返回值为getTop()+getTranslationY(),当setTranslationY()时getTop()不变,getY()变。
7.MotionEvent坐标方法
- getX() 当前触摸事件距离当前View左边的距离
- getY() 当前触摸事件距离当前View顶边的距离
- getRawX() 当前触摸事件距离整个屏幕左边的距离
- getRawY() 当前触摸事件距离整个屏幕顶边的距离
8.View宽高方法
- getWidth() layout后有效,返回值是mRight-mLeft,一般会参考measure的宽度(measure可能没用),但不是必须的。
- getHeight() layout后有效,返回值是mBottom-mTop,一般会参考measure的高度(measure可能没用),但不是必须的。
- getMeasuredWidth() 返回measure过程得到的mMeasuredWidth值,供layout参考,或许没用。
- getMeasuredHeight() 返回measure过程得到的mMeasuredHeight值,供layout参考,或许没用。
9.View获取屏幕中位置的一些方法
- getLocalVisibleRect() 获取View自身可见的坐标区域,坐标以自己的左上角为原点(0,0),另一点为可见区域右下角相对自己(0,0)点的坐标,
- getGlobalVisibleRect() 获取View在屏幕绝对坐标系中的可视区域,坐标以屏幕左上角为原点(0,0),另一个点为可见区域右下角相对屏幕原点(0,0)点的坐标。
- getLocationOnScreen() 坐标是相对整个屏幕而言,坐标为View左上角到屏幕左端和顶部的距离。
- getLocationInWindow() 如果为普通Activity则Y坐标为View左上角到屏幕顶部(此时Window与屏幕一样大);如果为对话框式的Activity则Y坐标为当前Dialog模式Activity的标题栏顶部到View左上角的距离。
10.View滑动相关坐标体系
View的scrollTo()和scrollBy()是用于滑动View中的内容,而不是改变View的位置;改变View在屏幕中的位置可以使用offsetLeftAndRight()和offsetTopAndBottom()方法,他会导致getLeft()等值改变。
- 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()的基础上,相对于View现在的位置继续滑动xy。
- setScrollX(int value) 实质为scrollTo(),只是只改变Y轴滑动。
- setScrollY(int value) 实质为scrollTo(),只是只改变X轴滑动。
- getScrollX()/getScrollY() 获取当前滑动位置偏移量。(相对于ParentView左上角为坐标原点)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值