Simple use of Surfaceview for Android game development

Source: Internet
Author: User
Tags drawtext

Reference: zero-based Android game programming
  Related knowledge points: 1, callback    a surfaceview must implement a Surfaceholder.callback interface. To notify view when the underlying surface state changes, the interface needs to implement the following 3 methods:    public void surfacecreated ( Surfaceholder holder)  {}   public void surfacechanged (SurfaceHolder  Holder, int format, int width, int height) {}   public void  surfacedestroyed (Surfaceholder holder)  {}   2, surfaceholder        This is an interface for controlling surface, it provides control of surface  size, format, etc.    surfaceview Getholder () The function can get the Surfaceholder object,surface  within the Surfaceholder object.       3, specific code  MySurfaceView.javapackage cn.llbb.testsurfaceviewdemo; import android.content.context;import android.graphics.canvas;import android.graphics.color; Import android.graphics.paint;import android.graphics.path;import android.graphics.rect;import  android.graphics.rectf;import android.view.surfaceholder;import android.view.surfaceholder.callback; import android.view.surfaceview;public class mysurfaceview extends surfaceview  Implements callback {private surfaceholder sfh;private canvas canvas;private  paint paint;public mysurfaceview (Context context)  {super (Context);sfh =  Getholder (); Sfh.addcallback (this);p aint = new paint ();p aint.setcolor (color.red); SetFocusable ( true);} Public void surfacecreated (Surfaceholder holder)  {mydraw ();} Public void surfacechanged (Surfaceholder holder, int format, int width,  int height)  {}public void surfacedestroyed (surfaceholder holder)  {}private  void mydraw () {Try{canvas = sfh.lockcanvas (); if (canvas != null) {Canvas.drawColor (Color.White); Canvas.drawtext ("DrawText",  10,  10, paint); Canvas.drawpoint (10, 20, paint); Canvas.drawpoints (new float[]{ 10,30,30,30}, paint); Canvas.drawline (10,40,50,40, paint); Canvas.drawlines (new float[]{ 10,50,50,50,70,50,110,50}, paint); Canvas.drawrect (10,60,40,100, paint); Rect rect = new rect (10,110,60,130); Canvas.drawrect (Rect, paint); RECTF RECTF = NEW RECTF (10,140,60,170); Canvas.drawroundrect (rectf, 20, 20,  paint); canvas.drawcircle (20, 200, 20, paint); Canvas.drawarc (NEW RECTF (150,20,200,70),  0, 230, true, paint); Canvas.drawoval (NEW RECTF (150,80,180,100),  paint); Path path = new path ();p Ath.moveto (160,150);p Ath.lineto (200,150);p Ath.lineto (180,200); Path.close (); Canvas.drawpath (Path, paint); Path pathcircle = new path ();p athcircle.addcircle (130, 260, 20,  Path.Direction.CCW); Canvas.drawtextonpath ("Pathtext", Pathcircle, 10, 20, paint);}} catch (exception e) {}finally{sfh.unlockcanvasandpost (canvas);}}} mainactivity.javapackage cn.llbb.testcanvasdemo;import android.app.activity;import  android.os.bundle;import android.view.menu;import android.view.menuitem;import  Android.view.window;import android.view.windowmanager;public class mainactivity extends  activity {protected void oncreate (bundle savedinstancestate)  {super.onCreate ( Savedinstancestate); requestwindowfeature (Window.feature_no_title);//getwindow (). SetFlags ( Windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); SetContentView ( New mysurfaceview (this));}}:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/73/58/wKioL1X7HHvD_vsZAAB3MMV3SIA492.jpg "title=" Android.jpg "alt=" Wkiol1x7hhvd_vszaab3mmv3sia492.jpg "/>

This article from "Leboit" blog, declined reprint!

Simple use of Surfaceview for Android game development

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.