Java 修改C+中的变量+;在openframeworks中使用android UI编程?
我正在用openframeworks为android开发一个应用程序。 我希望在我的应用程序中使用android用户界面进行导航。 但我在如何在openframeworks的代码中与android ui进行交互的问题上遇到了障碍。 我读了很多关于JNI的文章,但是我现在理解它允许使用java的C++代码,但是我不知道如何使它与我的代码的其余部分交互。 当我编写我的JNI/C++函数时,我需要调用它:Java 修改C+中的变量+;在openframeworks中使用android UI编程?,java,android,c++,java-native-interface,openframeworks,Java,Android,C++,Java Native Interface,Openframeworks,我正在用openframeworks为android开发一个应用程序。 我希望在我的应用程序中使用android用户界面进行导航。 但我在如何在openframeworks的代码中与android ui进行交互的问题上遇到了障碍。 我读了很多关于JNI的文章,但是我现在理解它允许使用java的C++代码,但是我不知道如何使它与我的代码的其余部分交互。 当我编写我的JNI/C++函数时,我需要调用它: extern "C" { ... } 但是我无法修改openframeworks程序的
extern "C" {
...
}
但是我无法修改openframeworks程序的任何变量。
例如,我如何将一个布尔值或int值从android用户界面输入到openframeworks中?
也许我是在问一些不可能的事情,或者我漏掉了一个点? < p>你会使用JNI来建立C++和java之间的通信。 UI部分应该用Java完成。请注意,您有一个srcJava文件夹和一个res/layout文件夹。 如果您有ADT eclipse插件,您应该能够在main_layout.xml中添加一个简单的按钮。尝试以下方法: 您主要关注的是
onCreate
中的最后几行:
- 您可以使用findViewById从布局xml访问组件
- 您为事件设置了一个侦听器-不同的组件要实现不同的侦听器。通常,最好将ui内容(GUI线程)与其余处理断开连接,以避免冻结
<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>
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);
}
}