Introduction to the android graphic framework

Source: Internet
Author: User

1. Introduction to the android graphic framework
The android graphic framework can be divided into two parts: the upper-layer view system used for application graphic UI display and the bottom-layer graphics driver used for graphic rendering.

The view/glsurfaceview system is used to build the graphical controls for the application UI. skia and OpenGL | es are the graphics drivers for actual graphic rendering. skia is a 2D graphics library, while OpenGL | ES is a 2D/3D graphics library. Canvas can be seen as an interface between view and skia. In fact, it is a set of 2D graphics APIs for application to draw 2D images.
 
II. Application of the android graphic framework
In Android, the graphics system is driven by the 2D graphics library skia and 2D/3D graphics library OpenGL | es.
For the 3D graphics library OpenGL | es, OpenGL ES 1.0 and 1.1 have been supported since android1.0, and OpenGL ES 2.2 and Android 2.0 have been supported since Android 3.0, android is added to the system. renderscript package and related technologies to support 3D rendering.
 
Several packages related to 2D graphics are: Android. Graphics. drawable, Android. view,
Android. View. animation, Android. Graphics, Android. Graphics. drawable. shapes.
 
Several packages related to 3D graphics are:
Android. OpenGL
Javax. microedition. khronos. EGL
Javax. microedition. khronos. opengles.
 
Different application scenarios use different graphics technologies. For example, if your application only needs to draw relatively static graphics and simple animations, 2d graphics technology can satisfy your needs. If your program is an interactive game animation, you may need to use 3D graphics technology.
 
1. 2D drawing
In Android, there are two ways to draw 2D images:
1) use the standard view control provided by the view SYSTEM IN THE SYSTEM
We know that in Android, one of the ways to draw the UI is to use various predefined standard view controls, such as textview, imageview, buttonview, and listview.
Draw 2D rows.
If the program only needs some static images and predefined animations, this method can meet the requirements.
 
2) use the 2D graphic interface canvas provided by the system
If method 1) does not meet the needs of 2D drawing, you can consider using this method.
When a program needs to draw custom graphics or animations, it may need to use a more flexible approach provided by canvas.
The canvas class provides a set of drawing APIs, such as drawbitmap, drawrect, and drawtext. With these basic drawing APIs, we can draw various custom images. Of course
The graphics drawn by the canvas-class drawing API must be applied to the view object to be displayed.
There are two ways to draw 2D images using canvas:
A. derive your own view class from the View class (on a view)
If an application does not need a high frame rate processing process, such as a chess game or a greedy Snake game or other slow games, you can use this method to create a custom image.
The typical steps for this method are:
(1) define a view: Class myview extends view {}
(2) The ondraw () method of the reload View class: public: void ondraw (canvas ){}
(3) Use the canvas drawing interface in the ondraw method to define your own drawing operations.

B. derive your own surfaceview class from the surfaceview class (on a surfaceview)
The difference between this method and A is that the surfaceview graph is drawn in a separate thread, while the figure in a is drawn in the UI thread (same as the activity thread).
In this way, graphics can be drawn independently from the UI thread. When complex graphics or animations are drawn, the UI thread will not be blocked.
The typical steps for this method are:
(1) define a surfaceview: Class mysurfaceview extends surfaceview implements surfaceholder. Callback (){}
(2) three methods for implementing the surfaceholder. Callback () interface: surfacecreated (), surfacechanged (), surfacedestroyed ().
(3) define your own drawing thread: Class mythread extends thread {}
(4) Reload the run function of the drawing thread.
This thread is generally defined in run and started in surfacecreate.

The plotting process is generally as follows:
Surfaceholder = getholder ();
Surfaceholder. addcallback (this)
Canvas canvas = surfaceholder. lockcanvas ();
// Perform the Drawing operation ..............................
Surfaceholder. unlockcanvasandpost ();
 
2. 3D drawing
Android supports 3D graphics API OpenGL ES in two ways: Android framework API and ndk.
1) Android framework API
The android framework provides two classes for application development to support 3D graphics API OpenGL ES: glsurfaceview and glsurfaceview. Renderer.
Through these two interface classes, we can develop 3D graphics based on OpenGL ES.
For detailed development Introduction, see another article "the first example of Android OpenGL programming".
 
2) ndk
This method is actually similar to 1), but ndk will put the implementation part of glsurfaceview. Renderer locally for implementation, and then implement communication between Java and C through JNI.
The main reason for using ndk for implementation is to improve efficiency, because the graphic rendering part is put locally and implemented using C/C ++.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.