Overview
When writing an application, it is important to properly determine your graphics needs. different graphic tasks correspond to different technologies. for example, the implementation of graphics and animation for a static application must be very different from that for an interactive game. here, we will discuss some operations for Drawing Images on android and their most suitable tasks.
Canvas and Drawables
Android provides a series of View components to provide common functions for most user interfaces. you can also extend these components to modify their appearance and behavior. in addition, you can use the Canvas class method to draw your own 2D display or create a Drawable object for something like a texture button or a frame-by-frame display animation.
Hardware acceleration
Since Android3.0, You can hard accelerate most painting Jobs completed with CanvasAPI to greatly improve their performance.
OpenGL
Android supports OpenGLES 1.0 and 2.0, and Android framework APIs and (NDK) are also supported. when CanvasAPI is not supported, it is recommended to use the framework API to add some graphic enhancement functions. or you do not care about high performance, but expect it to be platform-independent. using framework APIs has lower performance than NDK, so it is best to use NDK for many graphic interaction applications such as games (although you still get enough performance using framework APIs. for example, Google's main application is developed using the framework API ). when you have a lot of native code to be transplanted to android, OpenGL in NDK is still very useful.
Canvas
The Android framework APIs provides a series of 2D paintings. APIs allows you to draw your own images on a canvas or modify existing views to customize their appearances. when drawing a 2D image, you will typically use one of the following two methods:
A. draw your graphics or animations to a View in your layout. your drawing is processed by the system's standard View Painting Process-you only need to define the drawing to enter the View.
B. draw a graph directly in a Canvas. to use this method, you must call the onDraw () method of the appropriate class (pass it to your Canvas), or draw of the Canvas... () one of the methods (such as drawPicture ()). in doing so, you can also control the animation at will.
Option "a," is the best choice when you want to draw simple images that do not need to be dynamically changed and are not part of the game with high performance requirements. for example, you should draw a picture to the View when you want to display a static image or a pre-defined animation.
Option "B," Draw to Canvas. It is a better choice when your application needs to redraw itself on a regular basis. apps such as video games should be painted on Canvas. There are more than one way to do this:
In your UIActivity thread, you create a custom View component, call invalidate (), and process the onDraw () callback.
Alternatively, in different threads, you manage a SurfaceView and execute the painting action to the Canvas as quickly as possible (you do not need to execute invalidate ()).
Painting with Canvas
When you are writing an application where you want to perform special painting and/or control graphic animation, you should use Canvas to paint. one is actually only an intermediate layer, but an interface, which represents the actual drawing to the surface-it bears all the painting calls. using Canvas, your rendering is actually executed to a background Bitmap, which is placed in the window.
The onDraw () callback method in response to the painting event provides you with a Canvas, so you only need to pass your painting call to it. when processing a SurfaceView object, you can use SurfaceHolder. lockCanvas () gets a Canvas. however, if you need to create a new Canvas, you must define the Bitmap in which the image is actually drawn. bitmap is always needed by Canvas. you can create a new Canvaslike this as follows:
Bitmap B = Bitmap. createBitmap (100,100, Bitmap. Config. ARGB_8888 );
Canvas c = new Canvas (B );
Now your Canvas will be painted on the defined Bitmap. after using Canvas, you can use any Canvas. drawBitmap (Bitmap ,...) method to move your Bitmap to another Canvas. we recommend that you still use View. onDraw () or SurfaceHolder. lockCanvas () provides Canvas painting for you.
There are many ways to draw a Canvas class, such as drawBitmap (...), drawRect (...), drawText (...) and so on. other classes also have the draw () method. for example, you have a Drawable object to be placed on the Canvas. Drawable has its own draw () method. You only need to pass your Canvas as a parameter to this method.
Draw to View
If your application does not require a lot of processing operations or frame rate assurance (it may be a chess game, a snake game, or another slow animation application ), then you should consider creating a custom View component and then in its View. draw with Canvas in onDraw. in this way, the Android framework provides you with a predefined Canvas.
In this case, the onDraw () callback method is defined from the View (or its descendant. this method will be called by the Android framework, requiring your View to draw its own appearance. this is where you use Canvas to execute all the painting actions.
The Android Framework calls onDraw () only when needed (). every time your application is ready to be drawn, you must call invalidate () to request that your View is invalid. this indicates that you want your View to be drawn. Therefore, Android will call your onDraw () method (although this cannot guarantee that the callback method will be executed immediately ).
In onDraw () of your View component, use your Canvas for all painting work: Use a variety of Canvas. draw... () method, or use your Canvas as a parameter to call the draw () method of other classes. once your onDraw () ends, the Android framework uses your Canvas to draw a Bitmap, which is managed by the system.
Note: in order that the view requested by a thread other than the thread where the master Acitivity is located is invalid, you must call postInvalidate ().
You can go to the SDK routine directory: <your-sdk-directory>/samples/Snake/To see the example of a Snake game.
Draw to SurfaceView
SurfaceView is a special subclass of View. It provides a special painting interface in the View derived tree to provide this painting interface to the second thread of an application, therefore, the application does not need to wait for the system's View derived class to prepare the painting for other work, but another thread references a SurfaceView, which can be painted on its Canvas at its own speed.
To use it, you must first create a new class from SurfaceView. this class must implement SurfaceHolder. callback interface. this interface will notify you of the background surface information, such as when the surface is created, changed or destroyed. these events are important because you can know when you can start painting and whether you need to adjust the attributes of the new surface, and when to stop painting and possibly to kill some tasks. in your SurfaceView class, it is also a good place to define your second Thread class. This Thread class will execute all the painting processes on your Canvas.
You should use a SurfaceHolder to operate your surface object instead of directly operating it. therefore, after your SurfaceView is initialized, you can call getHolder () to obtain SurfaceHolder. you should also call addCallback () (pass this to it) to notify SurfaceHolder that you want to receive the SurfaceHolder callback (from SurfaceHolder. callback ). finally, rewrite SurfaceHolder in your SurfaceView class. every method of Callback.
To draw a Canvas on the surface in your second thread, you must pass your SurfaceHandler to the second thread and use lockCanvas () to obtain the Canvas. you can use Canvas for painting now. once you have finished painting, call unlockCanvasAndPost () to pass your Canvas object to it. Now, the Canvas will be drawn based on what you have given it. every time you want to paint, perform the canvas lock and unlock steps.
Note: Each time you get the Canvas from SurfaceHolder, the last status of the Canvas will be retained. you must completely redraw your surface each time. for example, you can fill the color with drawColor () or set a background image through drawBitmap () to clear the last state of the Canvas. otherwise, you will see traces of your last painting.
For example, you can go to the LunarLander game. In the SDKsamples folder, <your-sdk-directory>/samples/LunarLander /. alternatively, you can browse only the source code in SampleCode (this chapter is not yet available, so stay tuned ).
From the column of nkmnkm