KeyEvent-----------control aircraft up and down flight--------------------

Source: Internet
Author: User

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--------------------

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.