View坐标体系

来源:互联网 发布:python 图片相似度 编辑:程序博客网 时间:2024/05/17 03:00

View的官方解释:

This class represents the basic building block for user interface components. A View occupies a rectangular area on the screen and is responsible for drawing and event handling. View is the base class for widgets, which are used to create interactive UI components (buttons, text fields, etc.). TheViewGroup subclass is the base class for layouts, which are invisible containers that hold other Views (or other ViewGroups) and define their layout properties.

View是用户接口组件的基本构建块;View在屏幕上占据一个矩形的区域;View主要负责绘制和事件处理。View是所有控件的基类;用来创建可交互的UI组件(buttons, text fields, etc.)。

ViewGroup是View的子类,是布局的基类,是一个用来存放其他Views的看不见的容器,并用来定义它们的布局属性。

详解

View是用户接口组件的基本构建块:

一个用户与一个应用的交互,其实就是与这个应用中的许许多多的View的交互。

View在屏幕上占据一个矩形的区域:

用户在这个区域内和应用进行交互。

怎么交互的呢?

View主要负责绘制和事件处理:

方法一:绘制自己

改变自己的模样,也就是通过绘制自己与用户交互,比如:当用户点击自己的时候,就改变自己的背景颜色等等

方法二:事件处理

比如:当用户点击View时,就完成了一次任务,然后弹出一个Toast,告诉用户该View完成了什么任务,这样,用户也就知道这次交互的结果如何。

View树的结构:

View是Android所有控件的基类,而ViewGroup内部包含了许多个控件,即一组View。ViewGroup也继承了View,这就意味着View本身就可以是单个控件也可以是由多个控件组成的一组控件,通过这种关系就形成了View树的结构。

View的位置参数:

View的位置主要由它的四个顶点来决定,分别对应于View的四个属性:top、left、right、bottom,其中top是左上角的纵坐标,left是左上角的横坐标,right是右下角的横坐标,bottom是右下角的纵坐标。

注意:这些坐标都是相对于View的父容器来说的,因此它是一种相对坐标。


我们很容易得出View的宽高和坐标的关系:

width = right - left = getWidth()

height = bottom - top = getHeight()

那么如何获得这四个参数呢?

Left = getLeft();

Right = getRight();

Top = getTop();

Bottom = getBottom();

Android3.0开始,View增加了额外的几个参数:x,y,translationX和translationY。

x  ---------  View左上角的横坐标

y  ---------View左上角的纵坐标

translationX ---------View左上角相对于父容器横坐标方向的偏移量

translationY ---------View左上角相对于父容器纵坐标方向的偏移量

x = getX() = left + translationX

y = getY() = top + translationY

说明:translationX和translationY默认值为0

特别注意:

View平移过程中,top和left表示的是原始左上角的信息,其值并不会发生改变,此时发生改变的是x,y,translationX和translationY这四个参数。

下面看一个简单的demo:


直接上代码:

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    >    <Button        android:id="@+id/btn"        android:layout_width="100dp"        android:layout_height="50dp"        android:layout_marginLeft="10dp"        android:layout_marginTop="10dp"        />    <TextView        android:id="@+id/tv_left"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:padding="5dp"        android:text="View左上角横坐标"/>    <TextView        android:id="@+id/tv_top"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:padding="5dp"        android:text="View左上角纵坐标"/>    <TextView        android:id="@+id/tv_right"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:padding="5dp"        android:text="View右下角横坐标"/>    <TextView        android:id="@+id/tv_bottom"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:padding="5dp"        android:text="View右下角纵坐标"/>    <TextView        android:id="@+id/tv_x"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:padding="5dp"        android:text="View左上角横坐标X"/>    <TextView        android:id="@+id/tv_y"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:padding="5dp"        android:text="View左上角纵坐标Y"/>    <TextView        android:id="@+id/tv_translationX"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:padding="5dp"        android:text="View左上角X方向相对于父容器的偏移量"/>    <TextView        android:id="@+id/tv_translationY"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:padding="5dp"        android:text="View左上角Y方向相对于父容器的偏移量"/></LinearLayout>

public class MainActivity extends AppCompatActivity {    private Button btn;    private int    left;    private int    right;    private int    top;    private int    bottom;    private float  x;    private float  y;    private TextView tvLeft;    private TextView tvRight;    private TextView tvTop;    private TextView tvBottom;    private TextView tvX;    private TextView tvY;    private TextView tvTranslationX;    private TextView tvTranslationY;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        btn = (Button) findViewById(R.id.btn);        tvLeft = (TextView) findViewById(R.id.tv_left);        tvRight = (TextView) findViewById(R.id.tv_right);        tvTop = (TextView) findViewById(R.id.tv_top);        tvBottom = (TextView) findViewById(R.id.tv_bottom);        tvX = (TextView) findViewById(R.id.tv_x);        tvY = (TextView) findViewById(R.id.tv_y);        tvTranslationX = (TextView) findViewById(R.id.tv_translationX);        tvTranslationY = (TextView) findViewById(R.id.tv_translationY);        //确保View测量完成        btn.post(new Runnable() {            @Override            public void run() {                left = btn.getLeft();                right = btn.getRight();                top = btn.getTop();                bottom = btn.getBottom();                x = btn.getX();                y = btn.getY();            }        });        btn.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                tvLeft.setText("left =" + left);                tvTop.setText("top =" + top);                tvRight.setText("right =" + right);                tvBottom.setText("bottom =" + bottom);                tvX.setText("x =" + x);                tvY.setText("y =" + y);                tvTranslationX.setText("TranslationX =" + btn.getTranslationX());                tvTranslationY.setText("TranslationY =" + btn.getTranslationY());            }        });    }}

1 0
原创粉丝点击