Android button suspended on Surfaceview

Source: Internet
Author: User

Implement button suspension on top of Surfaceview

Note: Your implementation of the Surfaceview and Android Button,editview is sibling, cannot put one contained in another

1. Create your own Surfaceview class, be sure to implement the 2 parameter of that function, because you want to use in XML, the second parameter refers to the custom component of some properties of the length of the width and so on.

     Public Gamesurfaceview (Context context, AttributeSet Attrs) {        super (context,attrs);    }
       <dk.game.gamesurfaceview android:id="@+id/gameview"           android:layout_width ="fill_parent"           android:layout_height="Fill_ Parent"/>

2. Handling events in Surfaceview must be in the activity. We define our Surfaceview and components in XML button, TextView and so on when they are the same level!! Instead of the button contained in the Surfaceview, so although in Surfaceview can be indexed according to the ID button but the binding is unable to find the button, only our Activitysetcontentview ( R.layout.main); The button is displayed, so you can only bind in the activity that shows it.

@Override Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Requestwindowfeature (Window.feature_no_title);        GetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);    Setcontentview (R.layout.main); }    /** * Next button event * @param view*/     Public voidnextbt (view view) {gamesurfaceview.cn=2; }    /** * Previous button event * @param view*/     Public voidprebt (view view) {gamesurfaceview.cn=1; }

3. Relative layouts in Layout

<?xml version="1.0"encoding="Utf-8"? ><relativelayout xmlns:android="http://schemas.android.com/apk/res/android"Android:id="@+id/relativelayout1"Android:layout_width="fill_parent"Android:layout_height="fill_parent"> <dk.game.gamesurfaceview android:id="@+id/gameview"Android:layout_width="fill_parent"Android:layout_height="fill_parent"/> <Button Android:id="@+id/nextbt"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:layout_alignparentright="true"Android:layout_alignparentbottom="true"Android:text="Next One"Android:onclick="NEXTBT"/> <Button Android:id="@+id/prebt"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:layout_toleftof="@id/NEXTBT"Android:layout_alignparentbottom="true"Android:text="the previous one"Android:onclick="prebt"/> </RelativeLayout>

The source code of Gamesurfaceview class is as follows

Package Dk.game;import Android.content.context;import android.content.res.resources;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.util.attributeset;import Android.util.Log; Import Android.view.surfaceholder;import Android.view.SurfaceView; Public classGamesurfaceview extends Surfaceview implements Runnable, surfaceholder.callback{PrivateThread Gameviewthread; PrivateSurfaceholder Surfaceholder; PrivatePaint paint; PrivateBoolean runflag=false;  Public Static intScreen_width,screen_height; protectedResources Resources; PrivateCanvas Canvas; PrivateBitmap bmp_bg1,bmp_bg2;  Public Static intcn=1;  PublicGamesurfaceview (Context context, AttributeSet Attrs) {super (context,attrs);    Init (); }             PublicGamesurfaceview (Context context) {super (context);    Init (); }        Private voidinit () {Surfaceholder=Getholder (); Surfaceholder.addcallback ( This); Resources=getresources (); Paint=NewPaint (); Paint.setantialias (true); BMP_BG1=Bitmapfactory.decoderesource (Getresources (), R.DRAWABLE.BG1); Bmp_bg2=Bitmapfactory.decoderesource (Getresources (), r.drawable.bg2); //setfocusable (TRUE); //Setfocusableintouchmode (TRUE); //setclickable (true);Setkeepscreenon (true); }         Public voidDraw (Canvas canvas,paint Paint) {canvas.drawcolor (color.black); if(cn==1) {Canvas.drawbitmap (BMP_BG1,0,0, paint); }Else if(cn==2) {Canvas.drawbitmap (bmp_bg2,0,0, paint); }} @Override Public voidrun () { while(runflag) {Try{Canvas=Surfaceholder.lockcanvas (); LongStarttime=System.currenttimemillis ();                Canvas.drawcolor (Color.Black);                Draw (Canvas,paint); LongEndtime=System.currenttimemillis (); if( -> (endtime-startTime)) {Thread.Sleep ( --(endtime-startTime)); }            }Catch(Exception e) {LOG.E ("Error","Error Refreshing screen! "+e); }finally{                if(canvas!=NULL) {surfaceholder.unlockcanvasandpost (canvas); } }} @Override Public voidsurfacecreated (Surfaceholder holder) {Screen_width=getwidth (); Screen_height=getheight (); Runflag=true; Gameviewthread=NewThread ( This);    Gameviewthread.start (); } @Override Public voidsurfacedestroyed (Surfaceholder holder) {Runflag=false; } @Override Public voidSurfacechanged (Surfaceholder holder,intFormatintWidthintheight) {}}

Android button suspended on Surfaceview

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.