Android research-Android plotting method [zz]

Source: Internet
Author: User

GUIer should be curious about how to implement the Android GUI. It must have been searched. For example, it is easy to find TextView first, draw interface, onDraw interface, paint interface, and onPaint interface. Through these interfaces, we can further find the draw interface actually drawn by Drawable, and then find that it was drawn by the Canvas interface, next, we found that jni has reached a local interface such as SkCanvas. SkCanas was originally the core class of skia (similar to cairo), a small number of 3D graphics engines in pure 2D, the problem is transferred to the skia mechanism and implementation.

This article is a Daniel's article, introduced the Android graphics you want to know something, very full, very system, address: http://blog.csdn.net/arm10504/article/details/5483971

Android ApkThere are two types of drawing: 2D and 3D: 2D is composedSkiaIn the Framework diagram.OpenglThe 3D part is implemented by OpenGL | ES, and OpenGL | ES is an embedded version of Opengl. First, let's take a look at several painting methods of Android apk, then let's take a look at how this entire graphic system was built.
First, drawing isApplicationProgram memory FillingData,

I have never studied whether an Activity corresponds to a surface at the underlying layer, but it should all be performed on this surface memory. To put it bluntly, we can either call 2D

Or call the 3D API to draw images, and save the drawn images in the memory, finally, the content in the memory will be rendered by Opengl and then become pixel messages that can be displayed on the screen.
.

1. the Apk application is mainly concerned with the use of these Apis. There are two ways to draw images in the Android apk [2D]:
1. Simple Graphics in View
Just
Is a number of painting operations that have been directly implemented by Android, such as images, shapes, colors, pre-defined
Animation and so on. These simple drawing operations are actually 2D graphic operations provided by skia. With these predefined operations, we can add a background image and draw a simplified image.
Single place shape to implement some simple operations such as animation. Here, we can understand this simply, that is, we have not constructed a graph in a simple way. we just put ourGraphicResources are placed in the View systemSystemTo draw these Graphic. For example, we now bind an ImageView to the Activity. We canSetThis

The image View content is our picture, and then we can make the overall color of this picture blue, and then we can add this ImageView

Enter a pre-defined animation, so that when the system wants to display this View, our picture will be displayed, and there will be an animation with a blue tone, we didn't define drawing operations by ourselves,

Instead, the content is put into the View, and the system will draw the content. This method can only be used to draw static or extremely simple 2D images. For highly real-time animations, high-quality games cannot be implemented.
.
2. Canvas
First, we need to understand that Canvas is a 2D concept defined in Skia. That is to say, in this way, we can still draw 2D images.
. We can understand this Canvas as a system.
A memory area provided by the system (but in fact it is only a set of drawing APIs, the real memory is the Bitmap below ), it also provides a complete set of operations on this memory area,

All these operations are drawing APIs. That is to say, in this way, we can draw exactly what we need, or use Graphic to draw exactly what we need.

Control. This method can be divided into two types based on the environment: canvas painting with common View, and canvas painting with special SurfaceView.

The main difference between the two is that you can define a special thread in SurfaceView to complete the drawing work. The application does not need to wait for the View to be refreshed to improve the performance. The previous one is suitable for processing
Animation with a relatively small volume and a relatively small frame rate, such as a chess game; the latter is mainly used in the aspect of games and high-quality animations. Below are the typical sequence of the two methods:
2.1 View canvas
(1) define your own View: class your_view extends View {};
(2) Reload the onDraw method of the View: protected void onDraw (Canvas canvas ){};
(3) define your own painting operations in the onDraw method;

Ps: You can define your own Btimap:
Bitmap B = Bitmap. createBitmap (100,100, Bitmap. Config. ARGB_8888 );
Canvas c = new Canvas (B );
However, this Bitmap must be placed in the View canvas, and the drawn image can be displayed: public void drawBitmap (Bitmap bitmap, Matrix matrix, Paint paint );
After the invalidate () function is called, the system will re-call the onDraw () function in the near future. This time is short but not predictable. I used to call onDraw () write an if statement and use validate to let the system call onDraw in an endless loop to fl two images. The two images are almost flushed together.

2.2. Surface View Canvas
(1) define a SurfaceView: class your_surfaceview extends SurfaceView implements SurfaceHolder. Callback (){};
(2) surfaceCreated () surfaceChanged () surfaceDestroyed ();
(3) define your own focus on drawing Thread: class your_thread extends Thread (){};
(4) The run () function of the overload thread [generally defines the Drawing operation in run and starts this thread in surfaceCreated]
(5) The drawing process is generally as follows:
SurfaceHolder surfaceHolder = getHolder (); // Get holder. This holder is mainly applicable to surface operations, and users do not have permissions to perform surface operations.
SurfaceHolder. addCallback (this); // register the implemented callback
Canvas canvas = surfaceHolder. lockCanvas (); // obtain the Canvas for drawing.
/* ----------------------------------- Draw
** ---------------------------------- Finish drawing */
SurfaceHolder. unlockCanvasAndPost (); // submit and display

Some examples of drawing in the previous three methods are available in the SDK and are clearly written. I will not talk about it here. Here I will write some little experience in the code process, it should mainly involve Activity, which should be used in the future:
The first is about Eclipse:
(1) ctrl + shift + O can automatically add the required dependency package. This function is quite practical, and alt +/is a complete syntax;
(2) There are many useful functions in the code. What I remember is the declaration of F3 searching for classes, and the inheritance relationship of F4 searching for classes;
(3) breakpoint debugging is more convenient. You can choose to read the code in the top-right corner of Eclipse, as well as debug mode. I now use two log playing methods:
Log. e ("class", "value:" + classname); // checks whether the class is a null pointer.
Log. e (this. getClass (). getName (), "notice message ");
Then there is Activity:
(1) First, try to put the UI design in XML for implementation, rather than in code for implementation, so as to facilitate design, modification and transplantation;
(2) All components used must be declared in manifest. Otherwise, an error will be reported if the corresponding conponet cannot be found in the program;
(3) Generally, each Activity corresponds to a class and a corresponding layout file xml;
(4) Each Activity is displayed only after setContentView () is bound to the content, and you can obtain the required elements from this content (such as xml;
(5) The difference between res/drawable and res/raw is that the element pixels in drawable may be optimized by the system, but not in raw;
(6) When multiple activities obtain the same element from res/drawable, if one of them modifies its attributes, the corresponding attributes of this element in all other activities will be changed;
(7) res/anim stores animation-related xml;

The following is a summary of the packages used in 2D drawing:
Android. view // picture is in View
Android. view. animation // defines some simple Animation effects, such as Tween Animation and Frame. animation.
Android. graphics // defines APIs commonly used for drawing, such as canvas, paint, and bitmap.
Android. graphics. drawable // defines the corresponding Drawable, such as BitmapDrawable and PictureDrawable.
Android. graphics. drawable. shapes // defines some shapes.

II,
After learning about 2D, let's take a look at the 3D drawing method. The 3D drawing SDK is very simple. It just introduces a general method, that is, inheriting a View and obtaining
The principle of drawing with Opengl handles should be the same as that of 2D. The difference is that 2D APIs are used for drawing, and 3D is used for drawing. But because 3D
OpenGl | ES has a set of operating mechanisms, such as rendering process control. Therefore, Android provides us with a dedicated 3D drawing
GLSurfaceView. This class is placed in a separate package android. opengl, which implements operations not available in other views:
(1) It has OpenGL | errors tracking and checking tools during elasticsearch calling, which facilitates debugging in Opengl programming;
(2) All plotting is performed on a special Surface, which can be finally combined into the android View system;
(3) It can select its own buffer type based on The EGL configuration, such as RGB565 and depth = 16 (in this case, SurfaceHolder is SURFACE_TYPE_GPU, is the memory allocated from EGL ?)
(4) All painting operations are provided through render, and render calls Opengl in a separate thread.
(5) the Opengl running cycle can be coordinated with the Activity life cycle.
Next let's take a look at a typical Sequence that uses GLSurface to draw 3D images.
(1) select your EGL configuration (that is, the buffer type required for drawing) [optional]:
SetEGLConfigChooser (boolean)
SetEGLConfigChooser (EGLConfigChooser)
SetEGLConfigChooser (int, int)
(2) Select whether Debug information is required [optional]:
SetDebugFlags (int)
SetGLWrapper (GLSurfaceView. GLWrapper ).
(3) register an renderer: setRenderer (GLSurfaceView. Renderer) for GLSurfaceView)
(4) set reander mode, which can be continuous rendering orCommandThe default value is continuous rendering [optional]: setRenderMode (int)
This
It is important to note that the Opengl operation and Activity lifecycle must be bound together, that is, the Activity
Pause is required for opengl rendering. In addition, GLSurfaceView provides a very practical function for inter-thread interaction.
QueueEvent (Runnable) can be used for interaction between the main thread and the render thread. The following is an example provided by the SDK:
Class MyGLSurfaceView extends GLSurfaceView {
Private MyRenderer mMyRenderer;
Public void start (){
MMyRenderer = ...;
SetRenderer (mMyRenderer );
}
Public boolean onKeyDown (int keyCode, KeyEvent event ){
If (keyCode = KeyEvent. KEYCODE_DPAD_CENTER ){
QueueEvent (new Runnable (){
// This method will be called on the rendering
// Thread:
Public void run (){
MMyRenderer. handleDpadCenter ();
}});
Return true;
}
Return super. onKeyDown (keyCode, event );
}
}
GLSurfaceView is a very worthwhile feature provided by Android.LearningClass, it is actually an example of how to add a drawing thread to the View, howJavaExamples of using threads, how to add event queues, a classic Sequence drawing using SurfaceView, and how to define Debug information, I think I can learn a lot by reading it.KnowledgeThe specific source code is:/framworks/base/opengl/java/android/opengl/GLSurfaceView. java.
The 3D content is basically finished here, and the rest is mainly about how to use the Opengl API. Let's look at the simple cube in the API demo, for details about the complexity, see the implementation of the cube. Next we will summarize the packages required for 3D drawing:
Android. opengl // mainly defines GLSurfaceView
Javax. microedition. khronos. egl // egl interface of java Layer
Javax. microedition. khronos. opengles // opengl API

3. After learning about the basic 2D and 3D drawing methods, let's look back at the calling hierarchies of Opengl and Skia for Android.
3.1,
First, let's take a look at 2D. 2D is the main graphics engine. After all, 3D is limited by its high hardware requirements and is still relatively small on mobile phones. Skia can also partially achieve the effect similar to 3D,
Therefore, SKia implements the vast majority of graphics work on the Android platform. Next we will look at the call relationship between the application layer and the underlying layer to skia:

The call to skia by Android is a comparisonClassicDuring the call process, several packages of the application are provided in the SDK; JNI is placed in the Graphic directory under the JNI directory of the Framework; skia is placed under the external directory as a third-party component. Let's take a look at the skia structure:

The three databases involved are as follows:
Libcorecg. so contains part of/skia/src/core. For example, the Region and Rect are the basic unit for calculating the Region in SurfaceFlinger.
Libsgl. so contains the/skia/src/core | effects | images | ports | utils part and all content. This achieves most of skia's graphic effects and codec of the graphic format.
Libskiagl. so contains the content in/skia/src/gl, which is mainly used to call opengl for partial effect.
In addition, I have seen two directories in/skia/src: animator and view, which are not written into the makefile compilation path. I think these two directories are very important, I don't know whether Android is not used yet, or it is loaded in other ways.
To use skia interfaces to draw images at the underlying layer, we need to fully understand skia's entire set of mechanisms. In fact, it is not long before skia is open-source, and the information we can find on the Internet is very rough, if you really need to work in this area in the future, it will definitely require a certain amount of work.
3.2,
Android's 3D call has puzzled me for a while, because the directory framewoks/base/core/jni has never been found to be consistent with opengl.
After reading the content in opengl, we can see that Android has implemented opengl locally. JNI and java interfaces are all stored in/frameworks.
/Base/opengl, and it also includes a tool to generate JNI code.

Let's take a look at the opengl directory structure:
/Include all headers of egl and glesFile
The/java/android/opengl directory contains the GLSurfaceView to be used for 3D drawing.
The/java/com/google/android/gles_jni directory contains some automatically generated files.
/Java/javax/microedition/khronos/egl This Is The egl interface used by the application layer.
/Java/javax/microedition/khronos/opengl this is the opengl interface used by the application layer.
/Libagl. This is the main implementation of opengl.
/Libs contains the implementation of two libraries, one is libegl. so and the other is libGL | ES_CM.so
/Tools in my understanding, this is a jni generation tool.
Who knows Opengl programming is a big project. I think the current demand for 3D should be very low, and skia can also be used for many effects. So I think we should focus on skia in the future.

 

This article is complete.

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.