Android 自定义View之getLeft() / getX() / getRawX()的区别

    在自定义View的时候,如果涉及到滑动改变控件的位置或者手动设置控件的位置,那么必然会接触到getLeft()  getX() getRawX()这三个方法,那么这3个方法有什么区别呢。我们先看一张图

我们再看看View.java源码注释
 

/**
     * Left position of this view relative to its parent.
     *
     * @return The left edge of this view, in pixels.
     */
    @ViewDebug.CapturedViewProperty
    public final int getLeft() {
        return mLeft;
    }


/**
     * The visual x position of this view, in pixels. This is equivalent to the
     * {@link #setTranslationX(float) translationX} property plus the current
     * {@link #getLeft() left} property.
     *
     * @return The visual x position of this view, in pixels.
     */
    @ViewDebug.ExportedProperty(category = "drawing")
    public float getX() {
        return mLeft + getTranslationX();
    }

    从源码注释中我们可以知道,View.getLeft()得到的是该View距离其ParentView左边的距离。getX()得到的是该View距离其ParentView左边的距离 + 该View相对于ParentView左边的偏移量,初始状态下偏移量为0,所以初始状态下getLeft()和getX()的值相同
    我们再来看看,MotionEvent.java里面getX() getRawX()的注释

/**
     * {@link #getX(int)} for the first pointer index (may be an
     * arbitrary pointer identifier).
     *
     * @see #AXIS_X
     */
    public final float getX() {
        return nativeGetAxisValue(mNativePtr, AXIS_X, 0, HISTORY_CURRENT);
    }

/**
     * Returns the X coordinate of this event for the given pointer
     * <em>index</em> (use {@link #getPointerId(int)} to find the pointer
     * identifier for this index).
     * Whole numbers are pixels; the 
     * value may have a fraction for input devices that are sub-pixel precise. 
     * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
     * (the first pointer that is down) to {@link #getPointerCount()}-1.
     *
     * @see #AXIS_X
     */
    public final float getX(int pointerIndex) {
        return nativeGetAxisValue(mNativePtr, AXIS_X, pointerIndex, HISTORY_CURRENT);
    }

/**
     * Returns the original raw X coordinate of this event.  For touch
     * events on the screen, this is the original location of the event
     * on the screen, before it had been adjusted for the containing window
     * and views.
     *
     * @see #getX(int)
     * @see #AXIS_X
     */
    public final float getRawX() {
        return nativeGetRawAxisValue(mNativePtr, AXIS_X, 0, HISTORY_CURRENT);
    }

    这里我们可以看到getRawX()得到的是触摸点在手机屏幕中的X坐标,那么getX()好像不是很好理解,其实它就是触摸点相对于自身左边界的距离。为了证明这点,我特意写了一个Demo测试,就是自定义View,一个RelativeLayout里面有两个ImageView,在OnTouchEvent里面打印getX和getRawX的值,自定义View代码如下

package com.shadow.shadowwalker;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;

public class CustomView extends RelativeLayout {
    private final String TAG = "CustomView";
    private View iv_left, iv_right;

    public CustomView(Context context) {
        this(context, null);
    }

    public CustomView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        View view = View.inflate(getContext(), R.layout.custom_view, this);
        iv_left = view.findViewById(R.id.iv_left);
        iv_right = view.findViewById(R.id.iv_rigth);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.e(TAG,"event.getX() : " + event.getX() + "event.getRawX() : " + event.getRawX());
        return true;
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/iv_left"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_centerVertical="true"
        android:scaleType="centerCrop"
        android:src="@mipmap/ic_launcher" />

    <ImageView
        android:id="@+id/iv_rigth"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:scaleType="centerCrop"
        android:src="@mipmap/ic_launcher" />
</RelativeLayout>

    测试的时候将这个自定义View放在MainActivity的xml文件中,代码如下

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.shadow.shadowwalker.MainActivity">

    <com.shadow.shadowwalker.CustomView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_marginLeft="100dp" />
</android.support.constraint.ConstraintLayout>

    这里我是在1920*1080的手机上测试,这个CustomView代码里面设置的layout_marginLeft = "100dp"也就是该CustomView距离屏幕左边应该是300像素,那么我们通过getRawX()获取到的值应该要比getX()大300,到底是不是这样呢,我们看测试结果

    那么当我们把CustomView的layout_marginLeft设置为0的时候,getX()和getRawX()获取到的值应该是一样的,我们直接看测试结果

    总结:1.View.java里面 getLeft()获取到的是View相对于父控件左边的距离, getX()获取到的是View相对于父控件左边的距离(初始Left) + View相对于父控件左边的偏移量
               2.MotionEvent.java里面 getX()获取到的是触摸点相对于该View左边界的距离,getRawX()获取到的是触摸点在屏幕中的绝对位置x值

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值