Android中关于touch事件解释与案例

1、什么是touch事件?

一个Touch事件在用户点击屏幕(ACTION_DOWN)时产生,抬起手指(ACTION_UP)时结束,而Touch事件又被封装到MotionEvent中。

2、Touch事件可以分为一下几类:

事件类型说明
ACTION_DOWN手指按下
ACTION_UP手指抬起
ACTION_MOVE手指移动
ACTION_POINTER_DOWN多个手指按下
ACTION_POINTER_UP多个手指抬起
ACTION_CANCEL取消事件
ACTION_OUTSIDE触碰到了UI边界

获取Action常见的方式就是使用MotionEvent的getAction()方法,getAction()方法可以获得ACTION_DOWN、ACTION_UP、ACTION_MOVE、ACTION_CANCEL等事件,而ACTION_POINTER_DOWN、ACTION_PRINTER_UP则需要和ACTION_MASK相遇才能得到。

3、常见事件方法:

  • getX():获得事件发生时,触摸的中间区域相对view的触摸位置坐标(不会超出view的长度和宽度)。
  • getRawX():和上面的getX()不同的是,此方法获得是相对屏幕的位置坐标。
  • getX(int pointerIndex):在多点触控中,用来获取第pointerIndex个触控点的x位置坐标。
  • getPointerCount():获取触控点的数量。
  • getPressure(nID):LCD可以感应出用户手的压力(与物理硬件有关)。
  • getDownTime():按下开始时间。
  • getEventTime():事件结束时间。
  • getDownTime() - getEventTime():总共按下时花费的时间。
  • getEdgeFlags():当事件类型是ActionDown时通过此方法获得手指触控开始边界,如果是的话,有如下几种值:EDGE_LEFT、EDGE_TOP、EDGE_RIGHT、EDGE_BOTTOM。

4、在Touch事件中如何获取MotionEvent对象:

当用户触摸屏幕时,将创建一个MotionEvent对象,MotionEvent包含了关于发生触摸的位置、时间信息、以及触摸事件的其他细节。
获取MotionEvent对象的方法有:

  • 在View或Activity的onTouchEvent方法中:public boolean onTouchEvent(MotionEvent event) { }
  • 实现OnTouchListener接口后在onTouch方法中:public boolean onTouch(View v, MotionEvent event) { }

5、一个关于Touch事件的小案例:

设计一个显示用户触摸持续时间的小程序,在layout文件夹的布局文件activity_main.xml中设计如下界面(2个TextView、1个EditText和1个ImageView),触摸ImageView,在TextView中显示触摸时间。
界面布局activity_main.xml代码:

<?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">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/tv1"
            android:layout_width="wrap_content"
            android:layout_height="20dp"
            android:text="触摸持续时间:"
            />
        <EditText
            android:id="@+id/et"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            />
        <TextView
            android:id="@+id/tv2"
            android:layout_width="wrap_content"
            android:layout_height="20dp"
            android:text="毫秒" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView
            android:id="@+id/iv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/background"
            android:scaleType="fitXY"
            />
    </LinearLayout>

</LinearLayout>

逻辑功能实现MainActivity.java代码如下:

package com.example.test_6;



import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import java.sql.Time;

public class MainActivity extends Activity {

    private ImageView iv;
    private EditText et;

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        iv = findViewById(R.id.iv);
        et = findViewById(R.id.et);

        iv.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                long time = event.getEventTime() - event.getDownTime();
                String time_1 = String.valueOf(time);
                if(event.getActionMasked() == MotionEvent.ACTION_UP){
                    et.setText(time_1);

                }
                return  true;
            }
        });


    }


}

实验截图如下:

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值