您将使用 JNI 在 c++ 和 java 之间建立通信。UI 部分应该用 Java 完成。请注意,您有一个 srcJava 文件夹和一个 res/layout 文件夹。如果您有 ADT eclipse 插件,您应该能够在 main_layout.xml 中添加一个简单的按钮。尝试类似:
<cc.openframeworks.OFGLSurfaceView android:id="@+id/of_gl_surface"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<!-- add here other views' layouts -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true" >
<LinearLayout
android:id="@+id/fullscreen_content_controls"
style="?buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:background="@color/black_overlay"
android:orientation="horizontal"
tools:ignore="UselessParent">
<Button
android:id="@+id/myButton"
style="?buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Bam!" />
<!-- the text should move to a string in the values.xml -->
</LinearLayout>
</FrameLayout>
</RelativeLayout>
并在 OFActivity.java 中尝试这样的事情:
package cc.openframeworks.androidGuiExample;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import cc.openframeworks.OFAndroid;
public class OFActivity extends cc.openframeworks.OFActivity implements OnClickListener{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
String packageName = getPackageName();
ofApp = new OFAndroid(packageName,this);
Button myButton = (Button) findViewById(R.id.myButton);//id from layout xml
myButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
System.out.println(v);
}
@Override
public void onDetachedFromWindow() {
}
@Override
protected void onPause() {
super.onPause();
ofApp.pause();
}
@Override
protected void onResume() {
super.onResume();
ofApp.resume();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (OFAndroid.keyDown(keyCode, event)) {
return true;
} else {
return super.onKeyDown(keyCode, event);
}
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (OFAndroid.keyUp(keyCode, event)) {
return true;
} else {
return super.onKeyUp(keyCode, event);
}
}
OFAndroid ofApp;
// Menus
// http://developer.android.com/guide/topics/ui/menus.html
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Create settings menu options from here, one by one or infalting an xml
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// This passes the menu option string to OF
// you can add additional behavior from java modifying this method
// but keep the call to OFAndroid so OF is notified of menu events
if(OFAndroid.menuItemSelected(item.getItemId())){
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onPrepareOptionsMenu (Menu menu){
// This method is called every time the menu is opened
// you can add or remove menu options from here
return super.onPrepareOptionsMenu(menu);
}
}
你主要是在最后几行之后onCreate
:
- 您使用 findViewById 从布局 xml 访问组件
- 你为事件设置了一个监听器——不同的组件有不同的监听器来实现。通常最好将 ui 的东西(GUI 线程)与完成的其余处理断开连接以避免冻结。