Two java. The first one is responsible for drawing the aircraft, the main activity is responsible for controlling the aircraft's initial coordinates and flight, without layout.
------------------Draw the plane---------------------------
Planeview.java
package Com.example.admin.webchanjian;
Import Android.content.Context;
Import Android.graphics.Bitmap;
Import android.graphics.BitmapFactory;
Import Android.graphics.Canvas;
Import Android.graphics.Paint;
Import Android.view.View;
/**
* Created by admin in 2016/10/6.
*/
public class Plainview extends View {
Public float Curre NtX;
Public float currenty;
Bitmap plane;
Public Plainview (context context) {
Super (context);
Plane= Bitmapfactory.decoderesource (Context.getresources (), R.drawable.plane);
Setfocusable (TRUE);
}
@Override
public void OnDraw (canvas canvas) {
Super.ondraw (canvas);
Paint p=new paint ();
Canvas.drawbitmap (plane,currentx,currenty,p);
}
}
-----------------------------control aircraft Flight--------------------
Mainactivity.java
-------------------Playing Plane------------------------------
private int speed=10;
@Override
protected void OnCreate (@Nullable Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Remove Window Caption
Requestwindowfeature (Window.feature_no_title);
Full Screen display
GetWindow (). SetFlags (Windowmanager.layoutparams.flag_fullscreen,windowmanager.layoutparams.flag_fullscreen);
Creating Planeview Components
Final Plainview Plainview=new Plainview (this);
Setcontentview (Plainview);
Plainview.setbackgroundresource (R.drawable.back);
Get window Manager
WindowManager Windowmanager=getwindowmanager ();
Display Display=windowmanager.getdefaultdisplay ();
Displaymetrics displaymetrics=new displaymetrics ();
Get screen width and height
Display.getmetrics (Displaymetrics);
Set the initial position of the plane
PLAINVIEW.CURRENTX=DISPLAYMETRICS.WIDTHPIXELS/2;
plainview.currenty=displaymetrics.heightpixels-40;
The keyboard event binding listener for the Planeview component
Plainview.setonkeylistener (New View.onkeylistener () {
@Override
public boolean OnKey (View v, int keycode, keyevent event) {
Switch (Event.getkeycode ()) {
Move Down
Case KEYEVENT.KEYCODE_S:
Plainview.currenty+=speed;
Break
Move Up
Case KEYEVENT.KEYCODE_W:
Plainview.currenty-=speed;
Break
Move left
Case KEYEVENT.KEYCODE_A:
Plainview.currentx-=speed;
Break
Case KEYEVENT.KEYCODE_D:
Plainview.currentx+=speed;
Break
}
Notifies the Planeview component to redraw
Plainview.invalidate ();
return true;
}
});
}
KeyEvent-----------control aircraft up and down flight--------------------