It demonstrates a very good effect, similar to lightning, but with a lot of poor functions. Now we have implemented the plane's moving function. However, it looks pretty.
First, create a layout main. xml file.
View code
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:id="@+id/root"
4 android:orientation="vertical"
5 android:layout_width="fill_parent"
6 android:layout_height="fill_parent"
7 >
8 <ImageView
9 android:layout_width="fill_parent"
10 android:layout_height="fill_parent"
11 android:src="@drawable/back"
12 android:scaleType="fitXY"
13 />
14 </LinearLayout>
Step 2: Create an airplane class so that you can get a new plane in the main program.
View code
1 package cn.edu. zwu. Tel;
2
3 Import Android. content. context;
4 Import Android. Graphics. Bitmap;
5 import Android. Graphics. bitmapfactory;
6 Import Android. Graphics. Canvas;
7 Import Android. Graphics. paint;
8 Import Android. View. view;
9
10 public class planeview extends View
11 {
12 public float currentx;
13 public float currenty;
14 bitmap plane;
15
16 public planeview (context)
17 {
18 super (context );
19 // todo auto-generated constructor stub
20 // define aircraft Images
21 plane = bitmapfactory. decoderesource (context. getresources ()
22, R. drawable. Plane );
23 setfocusable (true );
24}
25 @ override
26 Public void ondraw (canvas)
27 {
28 super. ondraw (canvas );
29 // create a paint brush
30 paint P = new paint ();
31 // draw aircraft
32 canvas. drawbitmap (plane, currentx, currenty, P );
33}
34}
Third, modify the layoutkeyevent01.java file to control the plane's movements.
View code
1 package cn.edu. zwu. Tel;
2
3 Import Android. App. activity;
4 Import Android. OS. Bundle;
5 import Android. View. display;
6 Import Android. View. keyevent;
7 Import Android. View. view;
8 Import Android. View. window;
9 Import Android. View. windowmanager;
10 Import Android. View. View. onkeylistener;
11
12 public class layoutkeyevent01activity extends activity {
13/** called when the activity is first created .*/
14 private int speed = 12;
15
16 @ override
17 Public void oncreate (bundle savedinstancestate)
18 {
19 super. oncreate (savedinstancestate );
20 // remove the window title
21 requestwindowfeature (window. feature_no_title );
22 // full screen display
23 getwindow (). setflags (windowmanager. layoutparams. flag_fullscreen,
24 windowmanager. layoutparams. flag_fullscreen );
25 // create the planeview component
26 final planeview = new planeview (this );
27 setcontentview (planeview );
28 planeview. setbackgroundresource (R. drawable. Back );
29 // obtain the Window Manager
30 windowmanager = getwindowmanager ();
31 display = windowmanager. getdefadisplay display ();
32 // get screen width and height
33 int screenwidth = display. getwidth ();
34 int screenheight = display. getheight ();
35 // set the aircraft's initial position
36 planeview. currentx = screenwidth/2;
37 planeview. currenty = screenheight-40;
38 // bind a listener to the keyboard event of the draw component
39 planeview. setonkeylistener (New onkeylistener ()
40 {
41 @ override
42 Public Boolean onkey (View Source, int keycode, keyevent event)
43 {
44 // obtain the event triggered by the key
45 switch (event. getkeycode ())
46 {
47 // control plane down
48 case keyevent. keycode_dpad_down:
49 planeview. currenty + = speed;
50 break;
51 // control plane move-up
52 case keyevent. keycode_dpad_up:
53 planeview. currenty-= speed;
54 break;
55 // control plane left shift
56 case keyevent. keycode_dpad_left:
57 planeview. currentx-= speed;
58 break;
59 // control plane right shift
60 case keyevent. keycode_dpad_right:
61 planeview. currentx + = speed;
62 break;
63}
64 // notify the planeview component to redraw
65 planeview. invalidate ();
66 return true;
67}
68 });
69}
70}
Below is: