use jni method to draw

1,209 views
Skip to first unread message

NDK Programing

unread,
Aug 31, 2011, 9:51:02 PM8/31/11
to android-ndk
Now I try to paint in native code.And my thought is:
To transfer a java Surface object to C code,then use skia lib to draw.

My code :
Java:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SurfaceView surfaceview=new SurfaceView(this);
Surface surface=surfaceview.getHolder().getSurface();
Init(surface);
BeginDraw();
}
public native void Init(Surface surface);
public native void BeginDraw();
}
static {
System.loadLibrary("WinTest");
}

C code:
void JNICALL Java_com_example_WinTest_Init
(JNIEnv *env, jobject thiz, jobject Surface)
{
win=ANativeWindow_fromSurface(env,Surface);
if(win==NULL)LOGI("Init error.");
else LOGI("Init ok.");
}


I feel so disappointed that the win is null.
Can somebody tell me why?

rod.d...@gmail.com

unread,
Sep 1, 2011, 8:42:40 AM9/1/11
to andro...@googlegroups.com
You need to wait until the window has been initialized. In C code
this is when the APP_CMD_INIT_WINDOW command happens. Before that the
window is not initialized.

Rodney

> --
> You received this message because you are subscribed to the Google Groups "android-ndk" group.
> To post to this group, send email to andro...@googlegroups.com.
> To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.
>
>

rod.d...@gmail.com

unread,
Sep 1, 2011, 8:45:49 AM9/1/11
to andro...@googlegroups.com
Just a followup:

The visible lifetime of an activity happens between a call to
onStart() until a corresponding call to onStop(). During this time the
user can see the activity on-screen, though it may not be in the
foreground and interacting with the user. Between these two methods
you can maintain resources that are needed to show the activity to the
user. For example, you can register a BroadcastReceiver in onStart()
to monitor for changes that impact your UI, and unregister it in
onStop() when the user an no longer see what you are displaying. The
onStart() and onStop() methods can be called multiple times, as the
activity becomes visible and hidden to the user.

taken from:

http://developer.android.com/reference/android/app/Activity.html


So it looks like you don't have a window until onStart is called.

Rodney

Reply all
Reply to author
Forward
0 new messages