Android Event handling-android Learning Journey (44)

Source: Internet
Author: User

Introduction to Androd Event handling


A simple example of controlling aircraft movement
Package Peng. Liu. Test;Import Android. App. ActionBar;Import Android. App. Activity;Import Android. Graphics. Color;Import Android. OS. Bundle;Import Android. Util. Displaymetrics;Import Android. View. Display;Import Android. View. KeyEvent;Import Android. View. Menu;Import Android. View. View;Import Android. View. Window;Import Android. View. WindowManager;public class Mainactivity extends activity{int speed =Ten;@Override protected void OnCreate (Bundle savedinstancestate) {Super. OnCreate(savedinstancestate);Setcontentview (R. Layout. Activity_main);Requestwindowfeature (Window. FEATURE_no_title);GetWindow (). SetFlags(WindowManager. Layoutparams. FLAG_fullscreen,windowmanager. Layoutparams. FLAG_fullscreen);Final Plane Plane = new Plane (this);Setcontentview (plane);Plane. Setbackgroundresource(Color. RED);WindowManager manager = Getwindowmanager ();Display display = Manager. Getdefaultdisplay();Displaymetrics metrics = new Displaymetrics ();Display. Getmetrics(metrics);Plane. CurrentX= Metrics. Widthpixels/2;Plane. CurrentY= Metrics. Heightpixels- +;Plane. Setonkeylistener(New View. Onkeylistener() {@Override public boolean onKey (view view, int i, keyevent keyevent) {switch (key Event. getKeyCode()) {case KeyEvent. KeyCode_s:plane. CurrentY+=speed;                         Break;Case KeyEvent. KeyCode_w:plane. CurrentY-=speed;                         Break;Case KeyEvent. KeyCode_a:plane. CurrentX-=speed;                         Break;Case KeyEvent. KeyCode_d:plane. CurrentX+=speed;                         Break;} plane. Invalidate();return True;}        });}}
 PackagePeng.liu.test;ImportAndroid.content.Context;ImportAndroid.graphics.Bitmap;ImportAndroid.graphics.BitmapFactory;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Paint;ImportAndroid.view.View;/** * Created by LPLP on 2015/6/10. * * Public  class Plane extends View {     Public floatCurrentx,currenty; PublicBitmap plane; Public Plane(Context context) {Super(context);        Plane = Bitmapfactory.decoderesource (Context.getresources (), r.drawable.ic_launcher); Setfocusable (true); }@Override    protected void OnDraw(Canvas canvas) {Super. OnDraw (canvas); Paint p =NewPaint ();    Canvas.drawbitmap (PLANE,CURRENTX,CURRENTY,P); }}

Android Event handling-android Learning Journey (44)

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.