Android Application Development BASICS (8) -- surfaceview

Source: Internet
Author: User

I. Overview

Surfaceview is also a component used for drawing. However, because of its high efficiency, surfaceview is generally used in game programming and camera programming, another big difference between it and view is that it can be painted in a non-UI thread. In the following implementation, we will mention several notes for using surfaceview to draw images.


Ii. Requirements

Surfaceview is used for drawing.


Iii. Implementation

Create a project mysurface, modify the/RES/layout/Main. xml file, and add a button and a surfaceview. The complete main. xml file is as follows:

1 <? XML version = "1.0" encoding = "UTF-8"?>
2 <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
3 Android: layout_width = "fill_parent"
4 Android: layout_height = "fill_parent"
5 Android: Orientation = "vertical">
6
7
8 <button
9 Android: Id = "@ + ID/button"
10 Android: layout_width = "fill_parent"
11 Android: layout_height = "wrap_content"
12 Android: text = "Draw line"
13/>
14
15 <surfaceview
16 Android: Id = "@ + ID/surface"
17 Android: layout_width = "fill_parent"
18 Android: layout_height = "fill_parent"
19/>
20
21 </linearlayout>

Modify the mysurfaceactivity. Java file to implement button listening and surfaceholder. Callback interfaces. The complete content is as follows:

 

1 package com. Nan. surface;
2
3
4 Import Android. App. activity;
5 import Android. Graphics. Canvas;
6 Import Android. Graphics. color;
7 Import Android. Graphics. paint;
8 Import Android. Graphics. rect;
9 Import Android. OS. Bundle;
10 Import Android. View. surfaceholder;
11 import Android. View. surfaceview;
12 Import Android. View. view;
13 Import Android. widget. Button;
14
15
16 public class mysurfaceactivity extends Activity
17 {
18 private int oldx = 0;
19 private int Oldy = 0;
20 private button mbutton = NULL;
21 private surfaceview msurfaceview = NULL;
22 private surfaceholder msurfaceholder = NULL;
23 private paint mpaint = NULL;
24
25 @ override
26 Public void oncreate (bundle savedinstancestate)
27 {
28 super. oncreate (savedinstancestate );
29 setcontentview (R. layout. Main );
30
31 mbutton = (button) findviewbyid (R. Id. Button );
32 msurfaceview = (surfaceview) findviewbyid (R. Id. Surface );
33 msurfaceholder = msurfaceview. getholder ();
34 msurfaceholder. addcallback (New myholder ());
35
36 mpaint = new paint ();
37 // paint brush color
38 mpaint. setcolor (color. Red );
39 // paint brush width
40 mpaint. setstrokewidth (10.0f );
41 // button listening
42 mbutton. setonclicklistener (New View. onclicklistener ()
43 {
44
45 public void onclick (view V)
46 {
47 // todo auto-generated method stub
48 // lock the whole surfaceview
49 canvas mcanvas = msurfaceholder. lockcanvas ();
50 mcanvas. drawline (oldx, Oldy, oldx + 10, Oldy + 10, mpaint );
51 // X-axis increase
52 oldx = oldx + 10;
53 // increase the ordinate Value
54 Oldy = Oldy + 10;
55 // after the painting is completed, release the canvas and submit the modification.
56 msurfaceholder. unlockcanvasandpost (mcanvas );
57 // lock again
58 // msurfaceholder. lockcanvas (New rect (0, 0, 0, 0 ));
59 // msurfaceholder. unlockcanvasandpost (mcanvas );
60}
61 });
62}
63
64 // define a class to implement the callback Interface
65 public class myholder implements surfaceholder. Callback
66 {
67
68 public void surfacechanged (surfaceholder holder, int format, int width,
69 int height)
70 {
71 // todo auto-generated method stub
72 // Add your code
73}
74
75 public void surfacecreated (surfaceholder holder)
76 {
77 // todo auto-generated method stub
78 // Add your code
79}
80
81 public void surfacedestroyed (surfaceholder holder)
82 {
83 // todo auto-generated method stub
84 // Add your code
85}
86
87}
88
89}

Run the program and click the "Draw line" button. The effect is as follows:

As you can see, the drawn line is intermittent. You can remove the comments in line 58,59 of the above program and run the command again. The effect is as follows:

 

We can see that the line has not been broken.

Note that if you want the program to be able to draw a picture while running in the background and do not report errors, You need to modify it to the following format:

 

1 // lock the whole surfaceview
2 canvas mcanvas = msurfaceholder. lockcanvas ();
3 try
4 {
5 If (mcanvas! = NULL)
6 {
7 mcanvas. drawline (oldx, Oldy, oldx + 10, Oldy + 10, mpaint );
8 // the x-axis is increased.
9 oldx = oldx + 10;
10 // increase the ordinate Value
11 Oldy = Oldy + 10;
12 // after the painting is completed, release the canvas and submit the modification.
13 msurfaceholder. unlockcanvasandpost (mcanvas );
14}
15}
16 catch (exception E)
17 {
18 E. printstacktrace ();
19}
20 finally
21 {
22 if (mcanvas! = NULL)
23 {
24 // lock again
25 msurfaceholder. lockcanvas (New rect (0, 0, 0, 0 ));
26 msurfaceholder. unlockcanvasandpost (mcanvas );
27}
28}

Run the command again, and the effect is the same as that of the 2nd images.



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.