我正在尝试向我的GDK应用添加菜单。我正在使用沉浸感,并按照此处的说明进行操作:https://developers.google.com/glass/develop/gdk/ui/immersion-menus
所有内容编译好,但实际的选项菜单不起作用。当我点击触摸板时没有任何反应。事实上,这里似乎是一个额外的副作用,现在没有按键工作。我甚至无法向下滑动以关闭应用程序。我的玻璃基本上冻结,直到我强行用adb删除应用程序。
这是我的onKeyDown方法:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
openOptionsMenu();
return true;
}
return false;
}
我做错了什么?我完全从文档中复制了它。我是否需要将事件传递给超类或其他什么?
答案 0 :(得分:0)
我刚刚将相机预览和缩放项目上传到https://github.com/xjefftang/smartcamera的github,其中包含一个带菜单的活动。你可能想看一下。或者查看源代码以了解如何实现它:
public class ImageViewActivity extends Activity {
public static String TAG = "ImageViewActivity";
ImageView mImageview;
private GestureDetector mGestureDetector;
File mPictureFilePath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.imageview);
Bundle extras = getIntent().getExtras();
mPictureFilePath = (File)extras.get("picturefilepath");
Log.v(TAG, "pictureFilePath=" + mPictureFilePath.getAbsolutePath());
mImageview = (ImageView) findViewById(R.id.picture);
Bitmap myBitmap = BitmapFactory.decodeFile(mPictureFilePath.getAbsolutePath());
int h = (int) ( myBitmap.getHeight() * (640.0 / myBitmap.getWidth()) );
Bitmap scaled = Bitmap.createScaledBitmap(myBitmap, 640, h, true);
mImageview.setImageBitmap(scaled);
mGestureDetector = new GestureDetector(this);
mGestureDetector.setBaseListener(new GestureDetector.BaseListener() {
@Override
public boolean onGesture(Gesture gesture) {
if (gesture == Gesture.TAP) {
Log.v(TAG, "TAP");
openOptionsMenu();
return true;
} else if (gesture == Gesture.TWO_TAP) {
Log.v(TAG, "TWO_TAP");
return true;
} else if (gesture == Gesture.SWIPE_RIGHT) {
Log.v(TAG, "SWIPE_RIGHT");
return true;
} else if (gesture == Gesture.SWIPE_LEFT) {
return true;
} else if (gesture == Gesture.LONG_PRESS) {
Log.v(TAG, "LONG_PRESS");
return true;
} else if (gesture == Gesture.SWIPE_DOWN) {
Log.v(TAG, "SWIPE_DOWN");
return false;
} else if (gesture == Gesture.SWIPE_UP) {
Log.v(TAG, "SWIPE_UP");
return true;
} else if (gesture == Gesture.THREE_LONG_PRESS) {
Log.v(TAG, "THREE_LONG_PRESS");
return true;
} else if (gesture == Gesture.THREE_TAP) {
Log.v(TAG, "THREE_TAP");
return true;
} else if (gesture == Gesture.TWO_LONG_PRESS) {
Log.v(TAG, "TWO_LONG_PRESS");
return true;
} else if (gesture == Gesture.TWO_SWIPE_DOWN) {
Log.v(TAG, "TWO_SWIPE_DOWN");
return false;
} else if (gesture == Gesture.TWO_SWIPE_LEFT) {
Log.v(TAG, "TWO_SWIPE_LEFT");
return true;
} else if (gesture == Gesture.TWO_SWIPE_RIGHT) {
Log.v(TAG, "TWO_SWIPE_RIGHT");
return true;
} else if (gesture == Gesture.TWO_SWIPE_UP) {
Log.v(TAG, "TWO_SWIPE_UP");
return true;
}
return false;
}
});
}
public boolean onGenericMotionEvent(MotionEvent event) {
if (mGestureDetector != null) {
return mGestureDetector.onMotionEvent(event);
}
return false;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.imageview, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.upload:
Uri imgUri = Uri.parse("file://" + mPictureFilePath.getAbsolutePath());
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setText("Share image taken by Glass")
.setType("image/jpeg")
.setStream(imgUri )
.getIntent()
.setPackage("com.google.android.apps.docs");
startActivity(shareIntent);
return true;
case R.id.delete:
mPictureFilePath.delete();
Toast.makeText(ImageViewActivity.this, "Deleted", Toast.LENGTH_SHORT).show();
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
答案 1 :(得分:0)
这是一个最小的实现,它既可以打开菜单,也可以向下滑动以关闭当前的Activity。如果您有一个Activity Immersion,它将关闭您的应用程序。
// Display menu when user taps on touchpad or dismisses this activity if user swipes down
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
openOptionsMenu();
return true;
} else if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
}
return false;
}