-
Notifications
You must be signed in to change notification settings - Fork 157
Expand file tree
/
Copy pathHoverEmulatorButton.java
More file actions
99 lines (87 loc) · 2.94 KB
/
Copy pathHoverEmulatorButton.java
File metadata and controls
99 lines (87 loc) · 2.94 KB
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
92
93
94
95
96
97
98
99
package com.examples.customtouch;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Button;
/**
* Created by Dave Smith
* Xcellent Creations, Inc.
* Date: 12/5/12
* HoverEmulatorButton
* Button that transforms single pointer events into hovers and multitouch events into taps
*/
public class HoverEmulatorButton extends Button {
public HoverEmulatorButton(Context context) {
super(context);
}
public HoverEmulatorButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public HoverEmulatorButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getPointerCount()) {
case 1:
handleSingleTouchEvent(event);
break;
default:
handleMultitouchEvent(event);
break;
}
return true;
}
private void handleSingleTouchEvent(MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
event.setAction(MotionEvent.ACTION_HOVER_ENTER);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
event.setAction(MotionEvent.ACTION_HOVER_EXIT);
break;
case MotionEvent.ACTION_MOVE:
event.setAction(MotionEvent.ACTION_HOVER_MOVE);
break;
default:
//Ignore unknown events
return;
}
//Forward the converted event to the proper callback
super.dispatchGenericMotionEvent(event);
}
/*
* Forward all events with multiple fingers to super as if they were single pointer
* touch events
*/
private void handleMultitouchEvent(MotionEvent event) {
//Construct a new event that only contains a single pointer
MotionEvent newEvent = MotionEvent.obtain(
event.getDownTime(),
event.getEventTime(),
0,
event.getX(),
event.getY(),
event.getMetaState()
);
switch (event.getActionMasked()) {
case MotionEvent.ACTION_POINTER_DOWN:
newEvent.setAction(MotionEvent.ACTION_DOWN);
break;
case MotionEvent.ACTION_POINTER_UP:
case MotionEvent.ACTION_CANCEL:
newEvent.setAction(MotionEvent.ACTION_UP);
break;
case MotionEvent.ACTION_MOVE:
//Leave this even unchanged
newEvent.setAction(MotionEvent.ACTION_MOVE);
break;
default:
//Ignore unknown events
return;
}
//Forward the new event to super
super.onTouchEvent(newEvent);
}
}