我有一個應用程序,它具有一個創建表面視圖的活動,並在表面視圖內放置一個OpenGL渲染器。我正在處理OpenGL渲染器中的對象拾取。我想要做的是當一個用戶選擇一個特定的對象時,顯示一個文本塊和一個我已經存儲在文件中的圖像。看起來好像Android的PopupWindow類可以做到這一點。我能否在OpenGL渲染器上疊加彈出窗口?還是我接近這一切?Android PopUpWindow
感謝
我有一個應用程序,它具有一個創建表面視圖的活動,並在表面視圖內放置一個OpenGL渲染器。我正在處理OpenGL渲染器中的對象拾取。我想要做的是當一個用戶選擇一個特定的對象時,顯示一個文本塊和一個我已經存儲在文件中的圖像。看起來好像Android的PopupWindow類可以做到這一點。我能否在OpenGL渲染器上疊加彈出窗口?還是我接近這一切?Android PopUpWindow
感謝
好了,我的朋友,我有同樣的問題與您聯繫。我將解釋我做了什麼,我認爲它適合你的問題。
我有一個渲染器,顯示網格。用戶可以在網格上放置註釋。和我一樣,我也會進行光線拾取。當用戶點擊評論時,會彈出一個窗口。
基本上,你需要做的是在你的渲染器中調用一個方法,它將在UI線程中運行彈出窗口。 (OpenGl在自己的線程上運行)。
更具體地講,你必須發送消息到處理程序將顯示彈出式
YourRenderer.java
@Override
public void onDrawFrame(GL10 gl) {
.... code that displays the mesh ...
if(userRequestedPopup) {
Message message = new Message();
message.what = DialogHandler.OPEN_ANNOTATION;
// Here you can create a Bundle in order to pass more data inside the Message
dialogHanlder.sendMessage(message);
userRequestedPopup = false;
}
....
}
YourGLSurfaceView.java在這裏你應該創建處理程序
public class DialogHandler extends Handler {
... other constants ...
public static final int OPEN_ANNOTATION = 2;
...
@Override
public void handleMessage(Message msg) {
if (msg.what == OPEN_ANNOTATION) {
new YourPopup(context, msg);
}
}
}
在創建彈出窗口之前,您應該ld在xml中設計你自己的佈局(或者如果你願意,可以用編程方式創建它)。爲了理解代碼,我應該提到,我打電話給我的佈局popup_layout
。 此外,您應該向glsurfaceview的佈局中添加一個空佈局(它應該在寬度和高度上填充父項佈局),比方說一個LinearLayout,它將保存您的彈出窗口。我把它叫做popupHolder
YourPopup.java
public Popup(Context context, Message message) {
this.context = context;
Activity parent = (Activity) context;
// here you can have the instance of the holder from glSurfaceView
LinearLayout parentLayout = (LinearLayout) parent.findViewById(R.id.popupHolder);
// inflate your custom popup layout or create it dynamically
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.popup_layout, null);
// Now you should create a PopupWindow
PopupWindow popupWindow = new PopupWindow(view,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
// you can change the offset values or the Gravity. This will diaply the popup in the center
// of your glSurfaceView
popupWindow.showAtLocation(parentLayout, Gravity.CENTER, 0, 0);
}
我覺得這是你所需要的,因爲該解決方案爲我工作得很好。
希望我幫助......
爲您的彈出式窗口中的XML,然後在您的Java文件中使用此代碼:
public void popupWindow() {
PopupWindow pw;
LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup_window, (ViewGroup) findViewById(R.id.txtPusdNotR));
pw = new PopupWindow(layout, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
// Get your Buttons and other tags here
}