The following is 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; Used to control Surfaceview 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 ();
Get 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 the Canvas object and start drawing on the canvas Mcanvas.drawrgb (0, 0, 0); Fill the canvas with black mcanvas.drawcircle (x, Y, R, p); Draw a round mholder.unlockcanvasandpost (Mcanvas);
Finish drawing and display the canvas on the screen}/** * Call this function when Surfaceview is created * * @Override public void surfacecreated (Surfaceholder holder) {t = new thread (this);//Create a Thread object flag = true; Sets the identity of the thread running to true T.start (); Start thread}/** * When the Surfaceview view changes, call this function/* @Override P ublic void Surfacechanged (surfaceholder holder, int format,int width, int height) {}/** * call this function when Surfaceview is destroyed /@Override public void surfacedestroyed (Surfaceholder holder) {flag = false;///Set the identity of the thread running Set to False}/** * Call/@Override public boolean Ontou when the screen is touched Chevent (Motionevent event) {x = (int) event.getx ();//x coordinate y = (int) event.gety () when the screen is touched ;
Gets the y-axis coordinate return true when the screen is touched; /** * Call/@Override public boolean OnKeyDown when the user presses the button (int keycod E, KeyEvent event) {if (keycode = = keyevent.keycode_dpad_up) {//when user clicks on ↑ key y--;
Set y axis coordinates minus 1} return Super.onkeydown (KeyCode, event); @Override public void Run () {while (flag) {Draw ();//Call Custom Righteousness paintingMethod try {Thread.Sleep (50);//Let thread rest 50 milliseconds} catch (Interruptedexce
Ption 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.