gpt4 book ai didi

实例详解用户输入 i. 检测常用手势

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章实例详解用户输入 i. 检测常用手势由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

效果:一个手指实现(所有手势事件)和(部分事件的); 。

A. 所有手势 。

activity_main.xml 。

?
1
2
3
4
5
6
7
8
9
10
11
<TextView
android:id= "@+id/gesture"
android:layout_width= "match_parent"
android:layout_height= "150dp"
android:gravity= "center"
android:text= "单击手势变化" /><TextView
android:id= "@+id/doubleTap"
android:layout_width= "match_parent"
android:layout_height= "150dp"
android:gravity= "center"
android:text= "双击手势变化" />

MainActivity.java 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
public class MainActivity extends Activity
{
private static final String LOG_TAG = "HelloGesture" ;
private GestureDetector mGestureDetector = null ;
private TextView mGestureTextView = null ;
private TextView mDoubleTapTextView = null ;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGestureTextView = (TextView) findViewById(R.id.gesture);
mDoubleTapTextView = (TextView) findViewById(R.id.doubleTap);
// 构造GestureDetector对象,传入监听器对象
mGestureDetector = new GestureDetector( this , mOnGestureListener);
// 传入双击监听器对象
mGestureDetector.setOnDoubleTapListener(mDoubleTapListener);
}
@Override public boolean onTouchEvent(MotionEvent event)
{
// 在onTouchEvent方法中将事件传递给手势检测对象,否则手势监听对象中的回调函数是不会被调用的
mGestureDetector.onTouchEvent(event);
return super .onTouchEvent(event);
}
private OnGestureListener mOnGestureListener = new OnGestureListener()
{
@Override
public boolean onSingleTapUp(MotionEvent e)
{
Log.i(LOG_TAG, "onSingleTapUp: " + e.toString());
mGestureTextView.setText( "onSingleTapUp: " );
return false ;
}
@Override
public void onShowPress(MotionEvent e)
{
Log.i(LOG_TAG, "onShowPress: " + e.toString());
mGestureTextView.setText( "onShowPress: " );
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY)
{
Log.i(LOG_TAG, "onScroll: " + e1.toString() + ", " + e2.toString());
mGestureTextView.setText( "onScroll " );
return false ;
} @Override
public void onLongPress(MotionEvent e)
{
Log.i(LOG_TAG, "onLongPress: " + e.toString());
mGestureTextView.setText( "onLongPress: " );
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY)
{
Log.i(LOG_TAG, "onFling: " + e1.toString() + ", " + e2.toString());
mGestureTextView.setText( "onFling " );
return false ;
}
@Override
public boolean onDown(MotionEvent e)
{
Log.i(LOG_TAG, "onDown: " + e.toString());
mGestureTextView.setText( "onDown: " );
return false ;
}
}; private OnDoubleTapListener mDoubleTapListener = new OnDoubleTapListener()
{
@Override
public boolean onSingleTapConfirmed(MotionEvent e)
{
Log.i( "LOG_TAG" , "onSingleTapConfirmed: " + e.toString());
mDoubleTapTextView.setText( "onSingleTapConfirmed: " );
return false ;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e)
{
Log.i( "LOG_TAG" , "onDoubleTapEvent: " + e.toString());
mDoubleTapTextView.setText( "onDoubleTapEvent: " );
return false ;
}
@Override
public boolean onDoubleTap(MotionEvent e)
{
Log.i( "LOG_TAG" , "onDoubleTap: " + e.toString());
mDoubleTapTextView.setText( "onDoubleTap: " );
return false ;
}
};}

B. 部分手势 。

如果你仅仅只想处理几种手势,你可以选择继承GestureDetector.SimpleOnGestureListener类,而不是实现 GestureDetector.OnGestureListener接口 。

MainActivity.java 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class MainActivity extends Activity
{
private GestureDetector mGestureDetector = null ;
private TextView mGestureTextView = null ;
private TextView mDoubleTapTextView = null ;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGestureTextView = (TextView) findViewById(R.id.gesture);
mDoubleTapTextView = (TextView) findViewById(R.id.doubleTap);
// 构造GestureDetector对象,传入监听器对象
mGestureDetector = new GestureDetector( this , new MyGestureListener());
// 传入双击监听器对象
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
// 在onTouchEvent方法中将事件传递给手势检测对象,否则手势监听对象中的回调函数是不会被调用的
this .mGestureDetector.onTouchEvent(event);
return super .onTouchEvent(event);
}
class MyGestureListener extends GestureDetector.SimpleOnGestureListener
{
private static final String DEBUG_TAG = "Gestures" ;
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY)
{
Log.d(DEBUG_TAG, "onFling: " + e1.toString() + ", " + e2.toString());
mGestureTextView.setText( "onFling " );
return false ;
}
@Override
public boolean onDown(MotionEvent e)
{
Log.d(DEBUG_TAG, "onDown: " + e.toString());
mGestureTextView.setText( "onDown: " );
return false ;
}
}}

最后此篇关于实例详解用户输入 i. 检测常用手势的文章就讲到这里了,如果你想了解更多关于实例详解用户输入 i. 检测常用手势的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com