view.getlLeft()、view.getBottom()、view.getRight()和view.getBottom()所获得的值不是控件相对于手机屏幕而言的,
而是控件相对于父控件而言的。
下面用一个demo来说明上面四个方法是相对于父控件而言的。我的测试手机的分辨率是 480 * 800。1dp = 1.5px。
1. activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.zhengjunchen.vsgdemo.MainActivity">
<FrameLayout
android:id="@+id/fl"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:background="#aac864c8"
android:layout_width="200dp"
android:layout_height="200dp">
<ImageView
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:id="@+id/iv"
android:background="#aa00ff00"
android:layout_width="100dp"
android:layout_height="100dp"/>
</FrameLayout>
</RelativeLayout>
2. MainActivity.java
package com.example.zhengjunchen.vsgdemo;
import android.bluetooth.BluetoothAdapter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private FrameLayout frameLayout;
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
frameLayout = (FrameLayout) findViewById(R.id.fl);
iv = (ImageView) findViewById(R.id.iv);
//view加载完成时回调
frameLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// TODO Auto-generated method stub
Log.e("TAG", "frameLayout.getLeft(): " + frameLayout.getLeft());
Log.e("TAG","frameLayout.getTop(): " + frameLayout.getTop());
Log.e("TAG","frameLayout.getRight(): " + frameLayout.getRight());
Log.e("TAG","frameLayout.getBottom(): " + frameLayout.getBottom());
Log.e("TAG","--------------------------------------------------");
Log.e("TAG","iv.getLeft(): " + iv.getLeft());
Log.e("TAG","iv.getTop(): " + iv.getTop());
Log.e("TAG","iv.getRight(): " + iv.getRight());
Log.e("TAG","iv.getBottom(): " + iv.getBottom());
}
});
}
3. log的打印日志
11-11 08:25:58.892 4497-4497/com.example.zhengjunchen.vsgdemo E/TAG: frameLayout.getLeft(): 15
11-11 08:25:58.893 4497-4497/com.example.zhengjunchen.vsgdemo E/TAG: frameLayout.getTop(): 15
11-11 08:25:58.893 4497-4497/com.example.zhengjunchen.vsgdemo E/TAG: frameLayout.getRight(): 315
11-11 08:25:58.893 4497-4497/com.example.zhengjunchen.vsgdemo E/TAG: frameLayout.getBottom(): 315
11-11 08:25:58.893 4497-4497/com.example.zhengjunchen.vsgdemo E/TAG: --------------------------------------------------
11-11 08:25:58.893 4497-4497/com.example.zhengjunchen.vsgdemo E/TAG: iv.getLeft(): 15
11-11 08:25:58.894 4497-4497/com.example.zhengjunchen.vsgdemo E/TAG: iv.getTop(): 15
11-11 08:25:58.894 4497-4497/com.example.zhengjunchen.vsgdemo E/TAG: iv.getRight(): 165
11-11 08:25:58.894 4497-4497/com.example.zhengjunchen.vsgdemo E/TAG: iv.getBottom(): 165
分析:
由于手机的分辨率是480 * 800 的,所以 10dp = 15px。 从log打印的日志可以看出 如果imageview 的坐标不是相对于父控件framelayout而言的话,那么打印的坐标值不会是 15px。
iv.getBottom() = iv.getTop() + iv.getMeasuredHeight();
iv.getRight() = iv.getLeft() + iv.getMeasuredWidth();