TouchIdent
package com.example.touchident;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView vTime,vDuration,vGesture,vPressure,vSize,vLocationX,vLocationY,vVelocityX,vVelocityY;
private long eventStartTime;
private GestureDetector detector;
private GestureDetector.OnGestureListener listener = new GestureDetector.OnGestureListener() {
@Override
public boolean onDown(MotionEvent e) {
Log.i("MainActivity","onDown");
vTime.setText(""+e.getDownTime());
eventStartTime = e.getDownTime();
vPressure.setText(""+e.getPressure());
vSize.setText(""+e.getSize());
vLocationX.setText(""+e.getX());
vLocationY.setText(""+e.getY());
Log.i("MainActivity","onDown"+e.toString());
return false;
}
@Override
public void onShowPress(MotionEvent e) {
Log.i("MainActivity","onShowPress");
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
Log.i("MainActivity","onSingleTapUp");
vDuration.setText(""+(e.getEventTime() - eventStartTime));
vGesture.setText("SingleTapUp");
vVelocityX.setText("0.0");
vVelocityY.setText("0.0");
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
Log.i("MainActivity","onScroll");
return false;
}
@Override
public void onLongPress(MotionEvent e) {
vGesture.setText("LongPress");
vVelocityX.setText("0.0");
vVelocityY.setText("0.0");
vDuration.setText(""+(e.getEventTime() - eventStartTime));
Log.i("MainActivity","onLongPress"+e.toString());
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.i("MainActivity","onFling");
vDuration.setText(""+(e2.getEventTime() - eventStartTime));
vGesture.setText("Fling");
vVelocityX.setText(""+velocityX);
vVelocityY.setText(""+velocityY);
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ButterKnife.bind(this);
vTime = (TextView) findViewById(R.id.time_view);
vDuration =(TextView)findViewById(R.id.duration_view);
vGesture = (TextView) findViewById(R.id.gesture_view);
vPressure = (TextView) findViewById(R.id.pressure_view);
vSize = (TextView) findViewById(R.id.size_view);
vLocationX = (TextView) findViewById(R.id.locationX_view);
vLocationY = (TextView) findViewById(R.id.locationY_view);
vVelocityX = (TextView) findViewById(R.id.velocityX_view);
vVelocityY = (TextView) findViewById(R.id.velocityY_view);
// vTime.setText(""+System.currentTimeMillis());
detector = new GestureDetector(this,listener);
detector.isLongpressEnabled();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean b = detector.onTouchEvent(event);
if (b) {
return true;
}
return super.onTouchEvent(event);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/time_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello world!"
android:gravity="center"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" "
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- 标签-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="3">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="time:"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="gesture:"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="pressure:"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="size:"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="locationX:"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="locationY:"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="velocityX:"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="velocityY:"
android:textStyle="bold"/>
</LinearLayout>
<!-- 数据-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/duration_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello world!" />
<TextView
android:id="@+id/gesture_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello world!" />
<TextView
android:id="@+id/pressure_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello world!" />
<TextView
android:id="@+id/size_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello world!" />
<TextView
android:id="@+id/locationX_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello world!" />
<TextView
android:id="@+id/locationY_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello world!" />
<TextView
android:id="@+id/velocityX_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello world!" />
<TextView
android:id="@+id/velocityY_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello world!" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<!-- <LinearLayout-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:orientation="vertical">-->
<!-- <TextView-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:text="Hello world!"/>-->
<!-- <Button-->
<!-- android:id="@+id/create_database"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:text="Create database"/>-->
<!-- <Button-->
<!-- android:id="@+id/add_data"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:text="Add data" />-->
<!-- <Button-->
<!-- android:id="@+id/update_data"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:text="Update data" />-->
<!-- <Button-->
<!-- android:id="@+id/delete_data"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:text="Delete data" />-->
<!-- <Button-->
<!-- android:id="@+id/query_data"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:text="Query data" />-->
<!-- </LinearLayout>-->
<!--</LinearLayout>-->