פקודות מגע

באמצעות Android SDK אפשר לגשת לנתונים גולמיים מלוח המגע של Glass.

עם זאת, ה-GDK מספק מזהה תנועות שמזהה באופן אוטומטי תנועות נפוצות ב-Glass, כולל הקשה, החלקה וגלילה.

בנוסף, תנועות פשוטות מתורגמות לאירועים ב-D-pad כברירת מחדל לעיבוד קל, בלי להשתמש במזהה תנועה.

זיהוי תנועות כאירועים מרכזיים באמצעות מקשי החיצים (D-pad)

כברירת מחדל, מערכת Glass מתרגמת תנועות פשוטות לאירועים מרכזיים במקשי החיצים. כך אפשר להאזין לאירועים onKeyDown() ו-onKeyUp() בפעילויות או בתצוגות כדי לעבד את התנועות הבאות כמקשי החיצים (D-pad):

קטע הקוד הבא מזהה מתי משתמשים מקישים על לוח המגע:

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 בתצוגה המפורטת. כשמשתמשים מקישים על לוח המגע (מתבצע תרגום כקליק על לחצן במרכז מקשי החיצים) בזמן שהתצוגה מתמקדת בנושא, התצוגה יכולה לטפל באירוע באמצעות 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
    }
}

זיהוי תנועות באמצעות מזהה תנועות

גלאי תנועות מאפשרים לזהות תנועות פשוטות, כמו גם תנועות מורכבות יותר, כמו תנועות שמשתמשות במספר אצבעות או בגלילה. לתנועות מורכבות אין מפתח מקשי החיצים תואם.

ה-TrafficDetector מספק ממשקי האזנה שאפשר להטמיע כדי לקבל התראה על תנועה.

זיהוי תנועות ברמת הפעילות

זיהוי תנועות ברמת הפעילות מתאים כשלא חשוב לכם באיזה חלק בממשק המשתמש מתמקדים. לדוגמה, אם אתם רוצים להציג תפריט כשמשתמשים מקישים על לוח המגע, לא משנה באיזו תצוגה מפורטת התמקדתם, עליכם לטפל בפעולה MotionEvent במסגרת הפעילות.

הדוגמה הבאה:

  1. יצירת GestureDetector שמטמיעה מאזינים כדי לעבד תנועות מוכרות.
  2. מדיניות זו מבטלת את השיטה 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;
    }
}

זיהוי תנועות ברמת התצוגה המפורטת

זיהוי תנועות ברמת התצוגה מתאים כאשר רוצים לבצע פעולות שונות, בהתאם לתצוגה שבה המצלמה מתמקדת.

הדוגמה הבאה:

  1. יוצר תצוגה מותאמת אישית שעוקפת את השיטה dispatchGenericFocusedEvent(). כשמתרחש אירוע תנועה, השיטה הזו מעבירה את אירוע התנועה למזהה התנועה.
  2. מצהירה על תצוגה ניתנת למיקוד, כך שהיא תזהה אירועים כשהיא נמצאת במוקד.
  3. יצירת 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;
    }
}