ניתן לגשת לנתונים גולמיים מלוח המגע של Glass באמצעות ערכת ה-SDK של Android.
עם זאת, ה-GDK מספק מזהה תנועה, מזהה באופן אוטומטי תנועות נפוצות ב-Glass, כולל הקשה, החלקה וגלילה.
בנוסף, תנועות פשוטות מתורגמות אירועי D-pad כברירת מחדל לעיבוד קל ללא באמצעות גלאי תנועה.
זיהוי תנועות כאירועים מרכזיים בלחצני החיצים
כברירת מחדל, מערכת Glass מתרגמת תנועות פשוטות לאירועים מרכזיים בלחצני החיצים. הזה
מאפשרת להאזין
onKeyDown()
ו-onKeyUp()
אירועים שקשורים לפעילויות או לתצוגות כדי לעבד את התנועות הבאות כמקשי החיצים:
- מקישים על 'תרגום ל
KEYCODE_DPAD_CENTER
'. - לחיצה על לחצן המצלמה מתורגמת ל-
KEYCODE_CAMERA
. - החלקה למטה תתרגם ל
KEYCODE_BACK
.
קטע הקוד הבא מזהה מתי משתמשים מקישים על לוח המגע:
public class MyActivity extends Activity {
...
@Override
public boolean onKeyDown(int keycode, KeyEvent event) {
if (keycode == KeyEvent.KEYCODE_DPAD_CENTER) {
// user tapped touchpad, do something
return true;
}
...
return super.onKeyDown(keycode, event);
}
}
שיטה לזיהוי הקשות על תצוגות ספציפיות היא להטמיע
OnClickListener
לצפייה. כשמשתמשים מקישים על לוח המגע (מתורגם כלחיצה על לחצן בלחצני החיצים (D-pad)
כשהתצוגה בפוקוס, התצוגה יכולה לטפל באירוע
OnClickListener
public final class MyActivity extends Activity implements OnClickListener {
View cardView;
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
cardView = new Card(this).setText("Tap to carry out an action").getView();
// To receive touch events from the touchpad, the view should be focusable.
cardView.setOnClickListener(this);
cardView.setFocusable(true);
cardView.setFocusableInTouchMode(true);
setContentView(cardView);
}
@Override
protected void onResume() {
// To receive touch events from the touchpad, the view should have focus.
cardView.requestFocus();
super.onResume();
}
@Override
public void onClick(View v) {
// perform desired action
}
}
זיהוי תנועות באמצעות גלאי תנועות
זיהוי תנועות מאפשר לזהות גם תנועות פשוטות כמו תנועות מורכבות יותר, כמו אלו שמשתמשות באצבעות מרובות או בגלילה. התוצאות מורכבות מדי לתנועות אין מקש תואם בלחצני החיצים (D-pad).
התכונה GestureDetector מספק ממשקי האזנה שאפשר להטמיע כדי לקבל התראה על תנועה.
זיהוי תנועות ברמת הפעילות
ראוי לזהות תנועות ברמת הפעילות
כשלא חשוב לכם איזה חלק בממשק המשתמש
להתמקד. לדוגמה, אם רוצים להציג תפריט
כשהמשתמשים מקישים על לוח המגע, ללא קשר לסוג התצוגה שבו מתמקדים,
צריך לטפל ב-MotionEvent
בתוך הפעילות.
דוגמה:
- ייווצר
GestureDetector
שמטמיעה מאזינים לעיבוד תנועות מוכרות. - ביטול השיטה
onGenericMotionEvent()
של הפעילות כדי להעביר את אירועי התנועה אל מזהה התנועהonMotionEvent()
.
כשמתרחש אירוע תנועה, המערכת מעבירה אותו לגלאי התנועה. אם המיקום מזהה התנועה מודיע למאזין המתאים לעבד האירוע.
public class MainActivity extends Activity {
private GestureDetector mGestureDetector;
// ...
@Override
protected void onCreate(Bundle savedInstanceState) {
// ...
mGestureDetector = createGestureDetector(this);
}
private GestureDetector createGestureDetector(Context context) {
GestureDetector gestureDetector = new GestureDetector(context);
//Create a base listener for generic gestures
gestureDetector.setBaseListener( new GestureDetector.BaseListener() {
@Override
public boolean onGesture(Gesture gesture) {
if (gesture == Gesture.TAP) {
// do something on tap
return true;
} else if (gesture == Gesture.TWO_TAP) {
// do something on two finger tap
return true;
} else if (gesture == Gesture.SWIPE_RIGHT) {
// do something on right (forward) swipe
return true;
} else if (gesture == Gesture.SWIPE_LEFT) {
// do something on left (backwards) swipe
return true;
}
return false;
}
});
gestureDetector.setFingerListener(new GestureDetector.FingerListener() {
@Override
public void onFingerCountChanged(int previousCount, int currentCount) {
// do something on finger count changes
}
});
gestureDetector.setScrollListener(new GestureDetector.ScrollListener() {
@Override
public boolean onScroll(float displacement, float delta, float velocity) {
// do something on scrolling
}
});
return gestureDetector;
}
/*
* Send generic motion events to the gesture detector
*/
@Override
public boolean onGenericMotionEvent(MotionEvent event) {
if (mGestureDetector != null) {
return mGestureDetector.onMotionEvent(event);
}
return false;
}
}
זיהוי תנועות ברמת התצוגה
חשוב לזהות תנועות ברמת התצוגה, כאשר רוצים לעשות דברים שונים, בהתאם לתצוגה שבה מתמקדים.
דוגמה:
- יוצר תצוגה מותאמת אישית שמחליפה את
dispatchGenericFocusedEvent()
. כשמתרחש אירוע תנועה, השיטה הזו מעבירה את אירוע התנועה למזהה התנועה. - הצהרה על כך שניתן להתמקד בה כדי שהיא תזהה אירועים כשהיא מתמקדת.
- יצירת
GestureDetector
עם הטמעה מאזינים לעיבוד תנועות שזוהו.
כשזיהוי התנועה מזהה תנועה בזמן שהתצוגה ממוקדת, היא קוראת למאזינים המתאימים.
/**
* TextView that handles touchpad input (currently only TAP).
*/
public class TouchpadHandlingTextView extends TextView
implements OnAttachStateChangeListener{
private final GestureDetector mTouchDetector;
public TouchpadHandlingTextView(Context context, AttributeSet attrs) {
super(context, attrs);
mTouchDetector = createGestureDetector(context);
// must set the view to be focusable
setFocusable(true);
setFocusableInTouchMode(true);
}
public TouchpadHandlingTextView(Context context) {
this(context, null);
}
@Override
public void onViewAttachedToWindow(View v) {
requestFocus();
}
@Override
public void onViewDetachedFromWindow(View v) {
}
/**
* Pass a MotionEvent into the gesture detector
*/
@Override
public boolean dispatchGenericFocusedEvent(MotionEvent event) {
if (isFocused()) {
return mTouchDetector.onMotionEvent(event);
}
return super.dispatchGenericFocusedEvent(event);
}
/**
* Create gesture detector that triggers onClickListener. Implement
* onClickListener in your Activity and override
* onClick() to handle the "tap" gesture.
*/
private GestureDetector createGestureDetector(Context context) {
GestureDetector gd = new GestureDetector(context);
gd.setBaseListener(new GestureDetector.BaseListener() {
@Override
public boolean onGesture(Gesture gesture) {
if (gesture == Gesture.TAP) {
return performClick();
}
return false;
}
});
return gd;
}
}