Create a new class, Inherit view
PackageCom.topcrab.mygame;ImportAndroid.content.Context;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;ImportAndroid.graphics.Canvas;ImportAndroid.util.AttributeSet;ImportAndroid.view.View;ImportAndroid.view.WindowManager;/*** Created by Administrator on 2017-07-24.*/ Public classGameviewextendsView {Bitmap Bitmap; intMapwidth; intMapheight; Public intGetrunleft () {returnRunleft; } Public voidSetrunleft (intrunleft) { This. Runleft =Runleft; } Private intRunleft; Public intGetruntop () {returnRuntop; } Public voidSetruntop (intruntop) { This. Runtop =Runtop; } Private intRuntop; PublicGameview (context context, AttributeSet set) {Super(context, set); Bitmap=Bitmapfactory.decoderesource (Getresources (), r.drawable.fly); Mapheight=bitmap.getheight (); Mapwidth=bitmap.getwidth (); } @Overrideprotected voidOnDraw (canvas canvas) {canvas.drawbitmap (bitmap, Runleft-MAPWIDTH/2, RUNTOP-MAPHEIGHT/2,NULL); Super. OnDraw (canvas); }}
Load page by default
PackageCom.topcrab.mygame;Importandroid.app.Activity;ImportAndroid.content.Context;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.util.DisplayMetrics;Importandroid.view.MotionEvent;ImportAndroid.view.Window;ImportAndroid.view.WindowManager; Public classMainactivityextendsActivity {gameview Gameview=NULL; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); //No caption on screenrequestwindowfeature (Window.feature_no_title); //Full Screen displayGetWindow (). SetFlags (Windowmanager.layoutparams.flag_fullscreen,windowmanager.layoutparams.flag_fullscreen ); Setcontentview (R.layout.activity_main); Gameview=(Gameview) Findviewbyid (R.id.gameid); //Get screen width and heightWindowManager Manager = This. Getwindowmanager (); Displaymetrics Outmetrics=NewDisplaymetrics (); Manager.getdefaultdisplay (). Getmetrics (Outmetrics); intwidth =Outmetrics.widthpixels; intHeight =Outmetrics.heightpixels; //position when setting the initial state of the aircraftGameview.setruntop (height-100); Gameview.setrunleft (Width/2); } //Screen Touch Events@Override Public Booleanontouchevent (Motionevent event) {floatleft=Event.getx (); floattop=event.gety (); Gameview.setrunleft ((int) left); Gameview.setruntop ((int) top); Gameview.invalidate (); return Super. Ontouchevent (event); }}
XML file
<?XML version= "1.0" encoding= "Utf-8"?><Android.support.constraint.ConstraintLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:app= "Http://schemas.android.com/apk/res-auto"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Tools:context= "Com.topcrab.mygame.MainActivity"> <Com.topcrab.mygame.GameViewAndroid:id= "@+id/gameid"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" /></Android.support.constraint.ConstraintLayout>
Android Full screen displays custom view and moves