您的位置:首页 > 移动开发 > Android开发

android 获取view在屏幕内的x y坐标

2011-11-02 10:40 288 查看
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];

XML:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:padding="5px"

>

<ViewFlipper

android:id="@+id/fliper"

android:layout_width="fill_parent"

android:layout_height="100dp"

android:background="#FFFFFF"

android:flipInterval="2000" >

<TextView

android:id="@+id/tv_1"

android:text="Hello1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal">

</TextView>

<TextView

android:id="@+id/tv_2"

android:text="Hello"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal">

</TextView>

</ViewFlipper>

<Button android:id="@+id/button2"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="B"

android:layout_gravity="center_horizontal"

/>

</LinearLayout>

JavaCode:

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.ViewFlipper;

public class ViewLocationActivity extends Activity {

private ViewFlipper flipper;

private Rect mRectSrc = new Rect();

int[] location = new int[2];

int x, y;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

flipper = (ViewFlipper) findViewById(R.id.fliper);

Log.d("demo", "left:" + flipper.getLeft());

Log.d("demo", "right:" + flipper.getRight());

Log.d("demo", "Top:" + flipper.getTop());

Log.d("demo", "Bottom:" + flipper.getBottom());

Button btn = (Button) findViewById(R.id.button2);

btn.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

Log.d("demo", "Button.Width--->" + v.getWidth());

Log.d("demo", "Button.Height--->" + v.getHeight());

v.getLocalVisibleRect(mRectSrc);

Log.d("demo", "LocalVisibleRect--->" + mRectSrc);

v.getGlobalVisibleRect(mRectSrc);

Log.d("demo", "GlobalVisibleRect--->" + mRectSrc);

v.getLocationOnScreen(location);

x = location[0];

y = location[1];

Log.d("demo", "Screenx--->" + x + " " + "Screeny--->" + y);

v.getLocationInWindow(location);

x = location[0];

y = location[1];

Log.d("demo", "Window--->" + x + " " + "Window--->" + y);

Log.d("demo", "left:" + v.getLeft());

Log.d("demo", "right:" + v.getRight());

Log.d("demo", "Top:" + v.getTop());

Log.d("demo", "Bottom:" + v.getBottom());

}

});

}

}

**显示**

Button.Width--->470

Button.Height--->72

LocalVisibleRect--->Rect(0, 0 - 470, 72)

GlobalVisibleRect--->Rect(5, 231 - 475, 303)

Screenx--->5 Screeny--->231

Windowx--->5 Windowy--->231

left:5

right:475

Top:155

Bottom:227
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android view坐标 x y