Android Full screen displays custom view and moves

Source: Internet
Author: User

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

Related Article

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.