Android View坐标getLeft, getRight, getTop, getBottom解惑
来源:互联网 发布:软件页面设计规范 编辑:程序博客网 时间:2024/06/03 14:04
1 引起疑惑
分析视图invalidate流程的过程中发现view的left, right, top, bottom跟自己理解的不一样,现在想分析一下这几个值具体的含义。
2 理解坐标,位置概念
这里涉及坐标系的概念:
坐标系在二维视图中通过X轴和Y轴两个数字为组合表示某个点的绝对坐标。
例如(30, 100) 通常表示X轴30, Y轴100交叉的一个点
在Android中可以把left相当于X轴值, top相当于Y轴值, 通过这两个值Android系统可以知道视图的绘制起点,在通过Wdith 和 Height 可以得到视图上下左右具体值,就可以在屏幕上绝对位置绘制视图。right 与 bottom计算如下:
right = left + width;
bottom = top + height;
3 相应API
视图左侧位置 view.getLeft()
视图右侧位置 view.getRight()
视图顶部位置 view.getTop();
视图底部位置 view.getBottom();
视图宽度 view.getWidth();
视图高度 view.getHeight()
4 实例分析
按照我的理解:
蓝色区域位置 left = 0, top = 0 坐标(0, 0 )
黄色区域位置 left = 60, top = 115 坐标(60, 115)
绿色区域位置 left = 115, top = 170 坐标(115, 170)
绿色区域,这里理解错误,我认为绿色区域的位置是针对于蓝色区域的(0, 0)坐标的值,从上图的右下角打印出的坐标值就可以看出与下方我列出的值不一致,看看下面的图就明白了
总结: 视图的left , top , right , bottom 的值是针对其父视图的相对位置, 绿色区域是针对其父视图(即黄色区域为(0, 0)点)的坐标,不应该是(115, 170 ) 而是 (55, 55)
在此加入我自己写的xml文件以及代码
<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main1" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/holo_blue_light" tools:context="com.example.myapplication.MainActivity"> <FrameLayout android:id="@+id/activity_main2" android:layout_width="300dp" android:layout_height="300dp" android:layout_gravity="center" android:background="@android:color/holo_green_light"> <FrameLayout android:id="@+id/activity_main3" android:layout_width="150dp" android:layout_height="150dp" android:layout_gravity="center" android:background="@android:color/holo_red_light"> </FrameLayout> </FrameLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="100dp" android:layout_gravity="bottom" android:gravity="center" android:orientation="horizontal"> <TextView android:id="@+id/tv1" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:lineSpacingExtra="8px" /> <TextView android:id="@+id/tv2" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:lineSpacingExtra="8px" /> <TextView android:id="@+id/tv3" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:lineSpacingExtra="8px" /> </LinearLayout></FrameLayout>MainActivity代码
package com.example.myapplication;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.FrameLayout;import android.widget.TextView;public class MainActivity extends AppCompatActivity { private FrameLayout mFrameLayout1, mFrameLayout2, mFrameLayout3; private TextView tv1, tv2, tv3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mFrameLayout1 = (FrameLayout) findViewById(R.id.activity_main1); mFrameLayout2 = (FrameLayout) findViewById(R.id.activity_main2); mFrameLayout3 = (FrameLayout) findViewById(R.id.activity_main3); tv1 = (TextView) findViewById(R.id.tv1); tv2 = (TextView) findViewById(R.id.tv2); tv3 = (TextView) findViewById(R.id.tv3); mFrameLayout3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { tv1.setText("蓝色区域的坐标\n" + "getLeft(): " + mFrameLayout1.getLeft() + "\n" + "getTop(): " + mFrameLayout1.getTop()); tv2.setText("绿色区域的坐标\n" + "getLeft(): " + mFrameLayout2.getLeft() + "\n" + "getTop(): " + mFrameLayout2.getTop()); tv3.setText("红色区域的坐标\n" + "getLeft(): " + mFrameLayout3.getLeft() + "\n" + "getTop(): " + mFrameLayout3.getTop()); } }); }}
原文地址:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2013/0412/1156.html
- Android View坐标getLeft, getRight, getTop, getBottom解惑
- Android View坐标getLeft, getRight, getTop, getBottom解惑
- Android View坐标getLeft, getRight, getTop, getBottom解惑
- Android View坐标getLeft, getRight, getTop, getBottom解惑
- Android View坐标getLeft, getRight, getTop, getBottom解惑
- Android View坐标getLeft, getRight, getTop, getBottom解惑
- Android View坐标getLeft, getRight, getTop, getBottom解惑
- Android View坐标getLeft, getRight, getTop, getBottom
- Android View坐标getLeft, getRight, getTop, getBottom
- Android View坐标getLeft, getRight, getTop, getBottom
- Android View坐标getLeft, getRight, getTop, getBottom
- Android View坐标getLeft, getRight, getTop, getBottom
- Android View坐标getLeft, getRight, getTop, getBottom
- Android View坐标getLeft, getRight, getTop, getBottom
- Android View坐标getLeft, getRight, getTop, getBottom
- Android View坐标getLeft, getRight, getTop, getBottom
- Android View坐标getLeft, getRight, getTop, getBottom
- Android View坐标getLeft, getRight, getTop, getBottom
- 避免在指针的容器上使用类似remove的算法造成的内存泄漏
- URL参数对称加密---PHP语言版本
- Pandas 合并数据集
- 2016年个人工作总结、生活总结 和 2017年个人工作计划、生活计划
- 在ASHX中使用GZip压缩输出的文字流
- Android View坐标getLeft, getRight, getTop, getBottom解惑
- 移动平台对 meta 标签的定义
- neutron(2)
- 阅读图像显著性检测论文四:Saliency Filters Contrast Based Filtering for Salient Region Detection
- 我的go语言学习之旅----序
- NDK/SDK/JNI
- 6.RocketMQ 存储特点
- request:fail错误(含https解决方案)(真机预览问题)
- 合唱队