Android SurfaceView Study Notes

Source: Internet
Author: User

I have been looking for some examples of SurfaceView image painting recently. I am a beginner. At the beginning, I don't know how to start drawing images on SurfaceView. I finally learned the video tutorial, then, sort out the notes in the learning process and release them as follows. The following two views are inherited from SurfaceView and are one or two custom views. When you View the results, you can create an Activity and reference it in the onCreate method of the Activity, that is, in setContentView (new GameView (); or setContentView (new GameView1 );

Com. thinkive. ziyouxing. views; import android. content. context; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. view. keyEvent; import android. view. surfaceHolder; import android. view. surfaceView; import com. thinkive. adf. log. logger;/*** file name: com. thinkive. ziyouxing. views. gameView * Author: Xiong jie * Date: 14-4-14 * Time: * Development Tool: IntelliJ IDEA 12.0.4 * development language: Java, Android * development framework: * version: v0.1 *Description*

*/Public class GameView extends SurfaceView implements SurfaceHolder. callback, Runnable {private SurfaceHolder surfaceHolder; private Canvas canvas; private Paint paint; // private boolean isRunning = true is always running by default; // increase the radius by private int x = 0; public GameView (Context context) {super (context); surfaceHolder = this. getHolder (); surfaceHolder. addCallback (this) ;}@ Override public void surfaceCreate D (SurfaceHolder surfaceHolder) {new Thread (this ). start () ;}@ Override public void surfaceChanged (SurfaceHolder surfaceHolder, int I, int i2, int i3) {}/ *** executed when SurfaceView is destroyed, when the application exits, the current SurfaceView is destroyed, and the sub-thread is still drawing on the interface during running. * No View is available, how can we draw on it? Therefore, an exception occurs during the running process. *

* When the View is destroyed, the thread should not be drawn. ** @ Param surfaceHolder */@ Override public void surfaceDestroyed (SurfaceHolder surfaceHolder) {isRunning = false;} @ Override public void run () {while (isRunning) {try {drawView (); Thread. sleep (100);} catch (InterruptedException e) {e. printStackTrace () ;}}/ *** draw with thread *

* During game development, we need to regularly refresh the screen, that is, the screen will be drawn every 100 milliseconds in the online process. When will the thread start? *

* After SurfaceView is created successfully, an interface is displayed. When a startup thread is created, the interface is drawn every 100 milliseconds. *

* What should I do if I only draw borders without filling in the colors in them? *

* Paint. setStyle (Paint. style. STROKE); * when the interface is destroyed, first the Canvas is destroyed, and then the SurfaceHolder is destroyed */private void drawView () {try {if (null! = SurfaceHolder) {canvas = surfaceHolder. lockCanvas (); paint = new Paint (); paint. setColor (Color. RED); paint. setStyle (Paint. style. STROKE); paint. setStrokeWidth (3); canvas. drawCircle (200,200, x ++, paint) ;}} catch (Exception e) {Logger.info (this. getClass (), "Drawing exception", e) ;}finally {if (null! = Canvas) {surfaceHolder. unlockCanvasAndPost (canvas) ;}}/*** let the sub-thread end early. when the key is clicked, the push event is first responded. After the response is complete, the interface exits. That is, there is still a period of time from the execution of the event to the destruction of the interface. If the thread is terminated in this method * when the interface is actually destroyed, the sub-thread has ended. * @ Param keyCode * @ param event * @ return * // @ Override // public boolean onKeyDown (int keyCode, KeyEvent event) {// if (keyCode = KeyEvent. KEYCODE_BACK) {// isRunning = false; //} // return true ;//}}



Package com. thinkive. ziyouxing. views; import android. content. context; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. view. surfaceHolder; import android. view. surfaceView;/*** file name: com. thinkive. ziyouxing. views. gameView * Author: Xiong jie * Date: 14-4-14 * Time: * Development Tool: IntelliJ IDEA 12.0.4 * development language: Java, Android * development framework: * version: v0.1 *Description*

*/Public class GameView1 extends SurfaceView {// it can be understood as a pointer to SurfaceView. It can be analogous to a controller that controls the TV set, while SurfaceView is a TV set. If you want to control the TV set in the future, // you only need to operate the remote controller. Private SurfaceHolder surfaceHolder; // panel object private Canvas canvas; // Paint brush private paint Paint;/*** after the object is created successfully, the constructor will execute the object, however, when the constructor is executed, the interface is not displayed. The object is there, but the interface is not actually painted. We should wait for the current screen to be painted. * I will lock the screen again, I will get a panel, and the Panel will be painted again, because the object has just been created, the interface has not yet been displayed, and the obtained panel is empty, so it cannot be drawn. How can I determine how to execute the drawing, * Is this View created successfully? * SurfaceView has a helper class. The callback class sh. addCallBack () is similar to registering a listener. For example, you can click a button to listen, touch a listener, or press a button to listen. * That is to say, register a listener on SurfaceHolder. When the SurfaceHolder status changes, the methods in the listener are automatically executed. * The SurfaceHolder state changes, which is actually a state change of SurfaceView. * When we write the event listening, basically all the event listening methods are involved, all are callback methods. ** What is a callback method? ** That is, these methods are not called by the system. * @ Param context */public GameView1 (Context context) {super (context); surfaceHolder = this. getHolder (); surfaceHolder. addCallback (new GameViewCallBack ();} class GameViewCallBack implements SurfaceHolder. callback {// when SurfaceView is successfully created and executed @ Override public void surfaceCreated (SurfaceHolder surfaceHolder) {// after the Panel is locked, a Canvas is obtained and the Panel is locked, I can draw, for example, draw a circle, draw a straight line... canvas canvas = surfaceHolder. lockCanvas (); Paint = new Paint (); paint. setColor (Color. RED); canvas. drawLine (100,100, paint); // after the painting is finished, unlock the Panel and send the painted panel to the screen. Where can I send the Panel? Sent to the interface for display. We say that it comes with a second-level cache, which shows the second-level cache. // I first lock the cache, and first lock it without any operation. After the entire painting, the display will be ready. SurfaceHolder. unlockCanvasAndPost (canvas) ;}@ Override public void surfaceChanged (SurfaceHolder surfaceHolder, int format, int width, int height) {}@ Override public void surfaceDestroyed (SurfaceHolder surfaceHolder ){}}}


Related Article

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.