How to create a native GUI application

1,780 views
Skip to first unread message

Felipe Monteiro de Carvalho

unread,
Aug 6, 2011, 6:43:19 AM8/6/11
to android-ndk
Hello,

I would like to create a native application with a GUI, of course I
know that it will need to be custom drawn. Now I read that I can use
either the Surface API for that or OpenGL, so I'd like to try the
Surface API since I don't know much about OpenGL, but I am totally
lost about how the architecture should look like. Should I build a
minimal Java GUI and place a SurfaceView on it and then access this
SurfaceView from native code? Or can I create everything from native
code?

Which APIs exactly can access the Surface API and where is the
documentation for that?

The Surface API is for the SurfaceView class? How to get a native
handle from this Java class?

I tryed googling, but I couldn't find anything to help me...

thanks,

Felipe Monteiro de Carvalho

Dianne Hackborn

unread,
Aug 7, 2011, 12:03:36 PM8/7/11
to andro...@googlegroups.com
The first question is -- why do you want to do this?  Normal UIs on Android very much revolve around the view hierarchy in the framework; building your own thing separate from that is going to be a tremendous amount of work and result in something that is inconsistent with other Android applications.

Given all that, the best APIs for this are the native activity APIs introduced in 2.3.  Those let your native code completely take over drawing to the activity window and processing its events.


--
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.




--
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails.  All such questions should be posted on public forums, where I and others can see and answer them.

Felipe Monteiro de Carvalho

unread,
Aug 7, 2011, 4:43:16 PM8/7/11
to android-ndk
On Aug 7, 6:03 pm, Dianne Hackborn <hack...@android.com> wrote:
> The first question is -- why do you want to do this?  Normal UIs on Android
> very much revolve around the view hierarchy in the framework; building your
> own thing separate from that is going to be a tremendous amount of work and
> result in something that is inconsistent with other Android applications.

I don't mind using View's to have my controls, but the problem is that
I need to implement the drawing in native code, not in Java, and I
need fast pixel access for that.

That's basically my question: If I have a View, can I use the Surface
API to draw on it?

> Given all that, the best APIs for this are the native activity APIs
> introduced in 2.3.  Those let your native code completely take over drawing
> to the activity window and processing its events.

Which ones exactly??? And where are they documented? Any links for the
docs?

thanks =D

Felipe Monteiro de Carvalho

Stephen Williams

unread,
Aug 7, 2011, 10:04:29 PM8/7/11
to andro...@googlegroups.com
You might consider drawing a bitmap or OpenGL texture natively (using jnigraphics or just updating a Java byte array with the bitmap), but processing events at the Java level.  This can be made to work back to 2.2 for OpenGL ES 2 or jnigraphics and 1.6 for OpenGL ES 1 or passing bitmaps.  Or using the native application GUI API from 2.3 on.  However, those just call the Java equivalents, so you don't gain anything other than operating in a single language.

Stephen


Felipe Monteiro de Carvalho

--
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.




--
Stephen D. Williams s...@lig.net scie...@gmail.com LinkedIn: http://sdw.st/in
V:650-450-UNIX (8649) V:866.SDW.UNIX V:703.371.9362 F:703.995.0407
AIM:sdw Skype:StephenDWilliams Resume: http://sdw.st/gres
Personal: sdw.st facebook.com/sdwlig twitter.com/scienteer

Olivier Guilyardi

unread,
Aug 8, 2011, 9:01:53 AM8/8/11
to andro...@googlegroups.com
On 08/07/2011 10:43 PM, Felipe Monteiro de Carvalho wrote:

> I don't mind using View's to have my controls, but the problem is that
> I need to implement the drawing in native code, not in Java, and I
> need fast pixel access for that.
>

This is exactly what the jnigraphics native library is meant for. It is used to
directly access/modify the pixel data of a Java Bitmap object. This Bitmap
object can in turn be used in View.onDraw(). See "The 'jnigraphics' library" in
section IV of ndk/docs/STABLE-APIS, and bitmap-plasma in ndk/samples/.

--
Olivier

Felipe Monteiro de Carvalho

unread,
Aug 8, 2011, 11:30:13 AM8/8/11
to android-ndk
On Aug 8, 3:01 pm, Olivier Guilyardi <l...@samalyse.com> wrote:
> This is exactly what the jnigraphics native library is meant for. It is used to
> directly access/modify the pixel data of a Java Bitmap object. This Bitmap
> object can in turn be used in View.onDraw(). See "The 'jnigraphics' library" in
> section IV of ndk/docs/STABLE-APIS, and bitmap-plasma in ndk/samples/.

Thanks, but I would prefer a method which doesn't use JNI.

In my application the Java side executes a native application and then
communicates with it via pipes. The Java application obeys commands
from the Pascal native application and sends any events to it. I can
create controls with it, respond to events, etc, and everything works
fine, but I fear that drawing could be slow in this method, that's why
I am searching for something here, but still no luck =(

I only know at the moment vaguely that I could use either OpenGL or
the Surface API to solve my problem, but still no idea how that would
integrate with the Android API Views...

niko20

unread,
Aug 8, 2011, 12:51:02 PM8/8/11
to android-ndk
You have to create a Java wrapper somewhere, no way around it. At
least a GLSurfaceView. Then you can use OpenGL for your drawing. You
can never do everything 100% native. You want that, go write for iOS.

-niko

On Aug 8, 10:30 am, Felipe Monteiro de Carvalho

Felipe Monteiro de Carvalho

unread,
Aug 8, 2011, 3:53:06 PM8/8/11
to android-ndk
On Aug 8, 6:51 pm, niko20 <nikolatesl...@yahoo.com> wrote:
> You have to create a Java wrapper somewhere, no way around it. At
> least a GLSurfaceView. Then you can use OpenGL for your drawing. You
> can never do everything 100% native. You want that, go write for iOS.

Ok, finally we are getting somewhere! =) It's not a problem for me to
obtain a GLSurfaceView, as I said my application has a Java machine
which obeys commands and creates classes, calls methods, reads fields,
etc. I was missing the details, for example that a GLSurfaceView is
required on the Java side, then I suppose I can obtain a native OpenGL
handle and draw in the native code.

Now, is there an alternative to GLSurfaceView? An alternative which
doesn't use OpenGL? I heard that there is something called "Surface
API", which would be some kind of API to draw 2D graphics which
Android has and could be easier to use then OpenGL. Does that really
exist? Which class would I use in the Java side for that, equivalent
to what GLSurfaceView is for OpenGL?

Tim Mensch

unread,
Aug 8, 2011, 4:13:39 PM8/8/11
to andro...@googlegroups.com
On 8/8/2011 1:53 PM, Felipe Monteiro de Carvalho wrote:
> Now, is there an alternative to GLSurfaceView? An alternative which
> doesn't use OpenGL? I heard that there is something called "Surface
> API", which would be some kind of API to draw 2D graphics which
> Android has and could be easier to use then OpenGL. Does that really
> exist? Which class would I use in the Java side for that, equivalent
> to what GLSurfaceView is for OpenGL?

It was already mentioned twice in this thread, but the alternative is jnigraphics, which is documented in STABLE-APIS.html, and there's an example in the bitmap-plasma sample.

Please read the replies to your questions, since they frequently contain the answers.

Tim
Reply all
Reply to author
Forward
0 new messages