Simple implementation of surfaceview for Android Learning

Source: Internet
Author: User

The following is a smallProgramCodeSurfaceview is used to draw a circle on the screen. You can change the circle position by pressing the arrow key and touching the screen.

Mainactivity

 1   Package Com. view;
2
3 Import Android. App. activity;
4 Import Android. OS. Bundle;
5 Import Android. View. window;
6 Import Android. View. windowmanager;
7
8 Public Class Mainactivity Extends Activity {
9 /** Called when the activity is first created. */
10 @ Override
11 Public Void Oncreate (bundle savedinstancestate ){
12 Super . Oncreate (savedinstancestate );
13
14 // Hide Status Bar
15 This . Getwindow (). setflags (windowmanager. layoutparams. flag_fullscreen,
16 Windowmanager. layoutparams. flag_fullscreen );
17
18 // Remove the Activity title
19 Requestwindowfeature (window. feature_no_title );
20
21 // Set Layout
22 This . Setcontentview ( New Mysurfaceview ( This ));
23 }
24 }

Mainactivity mainly sets the screen full screen.

Mysurfaceview

 1   Package Com. view;
2
3 Import Android. content. context;
4 Import Android. Graphics. Canvas;
5 Import Android. Graphics. color;
6 Import Android. Graphics. paint;
7 Import Android. View. keyevent;
8 Import Android. View. motionevent;
9 Import Android. View. surfaceholder;
10 Import Android. View. surfaceholder. Callback;
11 Import Android. View. surfaceview;
12
13 Public Class MysurfaceviewExtends Surfaceview Implements Runnable, callback {
14
15 Private Surfaceholder mholder; // Used to control surfaceview
16
17 Private Thread t; // Declare a thread
18
19 Private Boolean Flag; // The ID of the thread running, used to control the thread
20
21 Private Canvas mcanvas; // Declare a canvas
22
23 Private Paint P; // Declare a paint brush
24
25 Private Int X = 50, y = 50, r = 10; // Coordinates and radius of the circle
26
27 Public Mysurfaceview (context ){
28 Super (Context );
29
30 Mholder = getholder (); // Get surfaceholder object
31 Mholder. addcallback ( This );// Add status listening for surfaceview
32 P = New Paint (); // Create a paint brush object
33 P. setcolor (color. White ); // Set the paint brush color to white
34 Setfocusable ( True ); // Set focus
35 }
36
37 /**
38 * Customize a method and draw a circle on the canvas
39 */
40 Public Void Draw (){
41 Mcanvas = mholder. lockcanvas (); // Get the canvas object and start painting the canvas.
42 Mcanvas. drawrgb (0, 0, 0 ); // Fill the canvas in black
43 Mcanvas. drawcircle (X, Y, R, P );// Draw a circle
44 Mholder. unlockcanvasandpost (mcanvas ); // Finish painting and display the canvas on the screen
45 }
46
47 /**
48 * This function is called when surfaceview is created.
49 */
50 @ Override
51 Public Void Surfacecreated (surfaceholder holder ){
52 T = New Thread ( This ); // Create a thread object
53 Flag = True ; // Set the thread running identifier to true.
54 T. Start (); // Start thread
55 }
56
57 /**
58 * This function is called when the surfaceview view changes.
59 */
60 @ Override
61 Public Void Surfacechanged (surfaceholder holder, Int Format, Int Width,
62 Int Height ){
63 }
64
65 /**
66 * This function is called when surfaceview is destroyed.
67 */
68 @ Override
69 Public Void Surfacedestroyed (surfaceholder holder ){
70 Flag = False ; // Set the thread running identifier to false.
71 }
72
73 /**
74 * Called when the screen is touched
75 */
76 @ Override
77 Public Boolean Ontouchevent (motionevent event ){
78 X = ( Int ) Event. getx (); // Obtain the x-axis coordinates of the screen when it is touched.
79 Y = ( Int ) Event. Gety (); // Obtain the Y axis coordinates of the screen when it is touched.
80 Return True ;
81 }
82
83 /**
84 * Called when a user presses a key
85 */
86 @ Override
87 Public Boolean Onkeydown ( Int Keycode, keyevent event ){
88 If (Keycode = keyevent. keycode_dpad_up ){ // When a user clicks the primary key
89 Y --; // Set Y axis minus 1
90 }
91 Return Super . Onkeydown (keycode, event );
92 }
93
94 @ Override
95 Public Void Run (){
96 While (FLAG ){
97 Draw (); // Call the custom painting Method
98 Try {
99 Thread. Sleep (50 ); // Let the thread rest for 50 milliseconds
100 } Catch (Interruptedexception e ){
101 E. printstacktrace ();
102 }
103 }
104 }
105
106 }

Mysurfaceview inherits surfaceview, implements runnable and callback interfaces, and overwrites the runnable run method and callback surfacecreated (surfaceholder holder ),Surfacechanged (surfaceholder holder, int format, int width,Int height), surfacedestroyed (surfaceholder holder) method, also implements the ontouchevent (motionevent event), onkeydown (INT keycode, keyevent event) method, detailed in the code has been commented out.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.