android rect 单位,Android view及其Rect

这篇博客详细介绍了Android中Rect类的使用,包括获取View在屏幕中的位置的方法,如getLocalVisibleRect、getGlobalVisibleRect、getLocationOnScreen和getLocationInWindow。还解释了Rect的坐标表示以及相关的方法,如width、height、isEmpty等。此外,文章还讨论了Rect的构造函数、平移操作以及如何从字符串中恢复Rect。
摘要由CSDN通过智能技术生成

Android 获取view在屏幕中的位置

getLocalVisibleRect , 返回一个填充的Rect对象, 是这个View的Rect大小,left,top取到的都是0

getGlobalVisibleRect , 获取全局坐标系的一个视图区域, 返回一个填充的Rect对象;该Rect是基于总整个屏幕的

getLocationOnScreen ,计算该视图在全局坐标系中的x,y值,(注意这个值是要从屏幕顶端算起,也就是索包括了通知栏的高度)//获取在当前屏幕内的绝对坐标

getLocationInWindow ,计算该视图在它所在的widnow的坐标x,y值,//获取在整个窗口内的绝对坐标

getLeft , getTop, getBottom, getRight, 这一组是获取相对在它父亲里的坐标注:如果在Activity的OnCreate()事件输出那些参数,是全为0,要等UI控件都加载完了才能获取到这些example:

int[] location = new int[2];

v.getLocationOnScreen(location);

int x = location[0];

int y = location[1];

Rect 其实就是左上右下 :(下面是源码)

/*

* Copyright (C) 2006 The Android Open Source Project

*

* Licensed under the Apache License, Version 2.0 (the "License");

* you may not use this file except in compliance with the License.

* You may obtain a copy of the License at

*

* http://www.apache.org/licenses/LICENSE-2.0

*

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS,

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and

* limitations under the License.

*/

package android.graphics;

import android.annotation.CheckResult;

import android.os.Parcel;

import android.os.Parcelable;

import java.io.PrintWriter;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

/**

* Rect holds four integer coordinates for a rectangle. The rectangle is

* represented by the coordinates of its 4 edges (left, top, right bottom).

* These fields can be accessed directly. Use width() and height() to retrieve

* the rectangle's width and height. Note: most methods do not check to see that

* the coordinates are sorted correctly (i.e. left <= right and top <= bottom).

*/

public final class Rect implements Parcelable {

public int left;

public int top;

public int right;

public int bottom;

/**

* A helper class for flattened rectange pattern recognition. A separate

* class to avoid an initialization dependency on a regular expression

* causing Rect to not be initializable with an ahead-of-time compilation

* scheme.

*/

private static final class UnflattenHelper {

private static final Pattern FLATTENED_PATTERN = Pattern.compile(

"(-?\\d+) (-?\\d+) (-?\\d+) (-?\\d+)");

static Matcher getMatcher(String str) {

return FLATTENED_PATTERN.matcher(str);

}

}

/**

* Create a new empty Rect. All coordinates are initialized to 0.

*/

public Rect() {}

/**

* Create a new rectangle with the specified coordinates. Note: no range

* checking is performed, so the caller must ensure that left <= right and

* top <= bottom.

*

* @param left The X coordinate of the left side of the rectangle

* @param top The Y coordinate of the top of the rectangle

* @param right The X coordinate of the right side of the rectangle

* @param bottom The Y coordinate of the bottom of the rectangle

*/

public Rect(int left, int top, int right, int bottom) {

this.left = left;

this.top = top;

this.right = right;

this.bottom = bottom;

}

/**

* Create a new rectangle, initialized with the values in the specified

* rectangle (which is left unmodified).

*

* @param r The rectangle whose coordinates are copied into the new

* rectangle.

*/

public Rect(Rect r) {

if (r == null) {

left = top = right = bottom = 0;

} else {

left = r.left;

top = r.top;

right = r.right;

bottom = r.bottom;

}

}

@Override

public boolean equals(Object o) {

if (this == o) return true;

if (o == null || getClass() != o.getClass()) return false;

Rect r = (Rect) o;

return left == r.left && top == r.top && right == r.right && bottom == r.bottom;

}

@Override

public int hashCode() {

int result = left;

result = 31 * result + top;

result = 31 * result + right;

result = 31 * result + bottom;

return result;

}

@Override

public String toString() {

StringBuilder sb = new StringBuilder(32);

sb.append(

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值