getRawX()、getRawY()、getX()、getY()、scrollTo(int x, int y)、scrollBy(int x, int y)

getRawX()、getRawY()、getX()、getY()

android的自定义控件,或者判断用户手势操作时,往往需要使用MotionEvent中的getRawX()、getRawY()与getX()、getY()取得触摸点在X轴与Y轴上的距离,这四个方法都返回一个float类型的参数,单位为像素(Pixel)
例:

<?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">
    <TextView
        android:id="@+id/button"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:gravity="center"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="50dp"
        android:background="@android:color/holo_blue_light"
        android:text="这是一个TextView"
        android:textColor="@android:color/white" />
</LinearLayout>
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        TextView textView= findViewById(R.id.button);
        textView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.d("MyLog","getRawX()=" + event.getRawX());
                Log.d("MyLog","getRawY()=" + event.getRawY());
                Log.d("MyLog","getX()=" + event.getX());
                Log.d("MyLog","getY()=" + event.getY());
                return false;
            }
        });
    }
}
2021-02-18 14:47:16.469 29472-29472/com.example.andy.mymeasure D/MyLog: getRawX()=453.0
2021-02-18 14:47:16.470 29472-29472/com.example.andy.mymeasure D/MyLog: getRawY()=701.0
2021-02-18 14:47:16.471 29472-29472/com.example.andy.mymeasure D/MyLog: getX()=153.0
2021-02-18 14:47:16.471 29472-29472/com.example.andy.mymeasure D/MyLog: getY()=148.0

在这里插入图片描述

结论:
getRawX()、getRawY()返回的是触摸点相对于屏幕的位置
getX()、getY()返回的则是触摸点相对于View的位置

getScrollX()、getScrollY()、scrollTo(int x, int y)、scrollBy(int x, int y)

例:

<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:layout_width="200dp"
            android:layout_height="100dp"
            android:gravity="center"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="20dp"
            android:background="@android:color/holo_blue_light"
            android:text="这是第一个TextView"
            android:textColor="@android:color/white" />
        <TextView
            android:layout_width="200dp"
            android:layout_height="100dp"
            android:gravity="center"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="20dp"
            android:background="@android:color/holo_blue_light"
            android:text="这是第二个TextView"
            android:textColor="@android:color/white" />
    </LinearLayout>
</HorizontalScrollView>
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        LinearLayout linearLayout = findViewById(R.id.linearLayout);
        Log.d("MyLog","getScrollX=" + linearLayout.getScrollX());
        Log.d("MyLog","getScrollY=" + linearLayout.getScrollY());
        linearLayout.scrollTo(-200, -300);
        Log.d("MyLog","getScrollX=" + linearLayout.getScrollX());
        Log.d("MyLog","getScrollY=" + linearLayout.getScrollY());
        linearLayout.scrollBy(100, 100);
        Log.d("MyLog","getScrollX=" + linearLayout.getScrollX());
        Log.d("MyLog","getScrollY=" + linearLayout.getScrollY());
    }
}
2021-02-18 12:12:21.238 20995-20995/com.example.andy.mymeasure D/MyLog: getScrollX=0
2021-02-18 12:12:21.238 20995-20995/com.example.andy.mymeasure D/MyLog: getScrollY=0
2021-02-18 12:12:21.238 20995-20995/com.example.andy.mymeasure D/MyLog: getScrollX=-200
2021-02-18 12:12:21.238 20995-20995/com.example.andy.mymeasure D/MyLog: getScrollY=-300
2021-02-18 12:12:21.238 20995-20995/com.example.andy.mymeasure D/MyLog: getScrollX=-100
2021-02-18 12:12:21.238 20995-20995/com.example.andy.mymeasure D/MyLog: getScrollY=-200

在这里插入图片描述

上面的3张图片显示了运行的过程:
第一张图片: 没有执行scrollTo()与scrollBy()前,getScrollX=0与getScrollY=0
第二张图片: 执行linearLayout.scrollTo(-200, -300)后, getScrollX=-200与getScrollY=-300
第三张图片: 执行linearLayout.scrollBy(100, 100)后,getScrollX=-100与getScrollY=-200

结论:
getScrollX与getScrollY必须在调用scrollTo(int x, int y)或scrollBy(int x, int y)后才由意义,否则是0
scrollTo(int x, int y)是将View(HorizontalScrollView)中的内容移动到指定坐标x、y处,是相对于View的左上角
scrollBy(int x, int y)则是改变View(HorizontalScrollView)中的内容移动到之前位置的相对位置

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值