Simple example of surfaceview usage in Android _android

Source: Internet
Author: User

This example describes the Surfaceview usage in Android. Share to everyone for your reference, specific as follows:

Here a small program code, mainly using Surfaceview to draw a circle on the screen, you can by pressing the arrow keys and touch the screen to change the position of the circle

Code:

Activity:

Package Com.view; 
Import android.app.Activity; 
Import Android.os.Bundle; 
Import Android.view.Window; 
Import Android.view.WindowManager; 
The public class Mainactivity extends activity {/** called the ' when ' is the ' The activity ' is a 
   -a-created 
   c void OnCreate (Bundle savedinstancestate) { 
     super.oncreate (savedinstancestate); 
     Hides the status bar 
     This.getwindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, 
         WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     Remove the title of the activity from 
     requestwindowfeature (window.feature_no_title); 
     Set Layout 
     This.setcontentview (new Mysurfaceview (this)); 
   } 

Surfaceview:

Package Com.view; 
Import Android.content.Context; 
Import Android.graphics.Canvas; 
Import Android.graphics.Color; 
Import Android.graphics.Paint; 
Import android.view.KeyEvent; 
Import android.view.MotionEvent; 
Import Android.view.SurfaceHolder; 
Import Android.view.SurfaceHolder.Callback; 
Import Android.view.SurfaceView; public class Mysurfaceview extends Surfaceview implements Runnable, Callback {private surfaceholder mholder;//To Control Su Rfaceview private Thread t; Declares a thread private Boolean flag; The identity of the thread run, used to control the thread private Canvas Mcanvas; Declare a canvas private Paint p; Declare a brush private int x =, y = 10; 
     The coordinates and radii of the circle are public mysurfaceview (context) {super (context); Mholder = Getholder (); Obtain the Surfaceholder object Mholder.addcallback (this); Add state listening for Surfaceview p = new Paint (); Create a Brush object P.setcolor (Color.White); Sets the color of the brush to white setfocusable (true); Set Focus}/** * Customize a method to draw a circle on the canvas * * Public VOID Draw () {Mcanvas = Mholder.lockcanvas ();//Get Canvas object, start drawing on canvas mcanvas.drawrgb (0, 0, 0) Vas.drawcircle (x, Y, R, p); Draw a round mholder.unlockcanvasandpost (Mcanvas); Finish drawing, show canvas on screen}/** * When Surfaceview is created, call this function/@Override public void surfacecreated (SURFACEH 
   Older holder) {t = new thread (this);//Create a Threading object flag = TRUE;//Set the identity of the thread running to true T.start ();//Start thread /** * When Surfaceview view changes, call this function */@Override public void surfacechanged (Surfaceholder holder, t format, int width, int height) {}/** * When Surfaceview is destroyed, call this function/@Override public voi 
   D surfacedestroyed (Surfaceholder holder) {flag = false;//Set the identity of the thread running to false}/** * Call when the screen is touched @Override public boolean ontouchevent (Motionevent event) {x = (int) event.getx ();///X coordinate y when the screen is touched = (int) event.gety (); 
   Gets the y-axis coordinate return true when the screen is touched; }
   /** * When the user presses the button call/@Override public boolean onKeyDown (int keycode, keyevent event) {if (keycode =  = keyevent.keycode_dpad_up) {//y--when user clicks on ↑ key; 
   Set y axis coordinates minus 1} return Super.onkeydown (KeyCode, event); @Override public void Run () {while (flag) {Draw ();//Call the custom drawing method try {Thread.s Leep (50); 
       Let the thread rest 50 milliseconds to catch (Interruptedexception e) {e.printstacktrace (); } 
     } 
   } 
}

Mysurfaceview first inherits Surfaceview, then implements Runnable and callback interfaces

Rewrite the runnable run method and the callback surfacecreated (Surfaceholder holder), surfacechanged (surfaceholder holder, int format, int width,int height), surfacedestroyed (Surfaceholder holder) method,

Also implemented Ontouchevent (Motionevent event), OnKeyDown (int keycode, KeyEvent event) method, detailed in the code has been commented.

I hope this article will help you with the Android program.

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.