Android NDK入门、提高和实战
2010-08-21 06:22:00 来源:WEB开发网//步骤3
jfloat ax = (*env)->GetFloatField(env, a, field_x);
jfloat ay = (*env)->GetFloatField(env, a, field_y);
jfloat bx = (*env)->GetFloatField(env, b, field_x);
jfloat by = (*env)->GetFloatField(env, b, field_y);
//步骤4
return sqrtf(powf(bx-ax, 2) + powf(by-ay, 2));
}
#include
#include
#include
jfloat Java_chroya_demo_ndk_Main_distance(JNIEnv* env, jobject thiz, jobject a,jobject b)
{
//步骤1
jclass point_class = (*env)->FindClass(env, "chroya/demo/ndk/Point");
if(point_class == NULL) {
//printf("class not found");
__android_log_write(ANDROID_LOG_INFO, "MyNdkDemo", "class Point not found");
return 0;
} else {
__android_log_write(ANDROID_LOG_INFO, "MyNdkDemo", "found class Point");
}
//步骤2
jfieldID field_x = (*env)->GetFieldID(env, point_class, "x", "F");
jfieldID field_y = (*env)->GetFieldID(env, point_class, "y", "F");
//步骤3
jfloat ax = (*env)->GetFloatField(env, a, field_x);
jfloat ay = (*env)->GetFloatField(env, a, field_y);
jfloat bx = (*env)->GetFloatField(env, b, field_x);
jfloat by = (*env)->GetFloatField(env, b, field_y);
//步骤4
return sqrtf(powf(bx-ax, 2) + powf(by-ay, 2));
}
然后在Java里面调用:
Java代码
public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(getApplicationContext());
Point a = new Point();
a.x = 3;
a.y = 3;
Point b = new Point();
更多精彩
赞助商链接