Surfaceview learning in Android

Source: Internet
Author: User
Tags getcolor gety

The obvious difference between surfaceview and view is that the surface does not need to update the view through a thread, but you must use the lockcanvas method to lock the canvas before painting, and then draw the canvas, after that, use the unlockcanvasandpost method to unlock the canvas. The surfaceview class handles the same event as the View class.

First, let's look at a simple framework.

Drawing interface class:

Package COM. example. bonusball; import android. content. context; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. rectf; import android. util. attributeset; import android. view. surfaceholder; import android. view. surfaceview; public class canvasview extends surfaceview implements surfaceholder. callback {private surfaceholder myholder; private Pai NT ballpaint; // paint used to draw the cannonballprivate int screenwidth; // width of the screenprivate int screenheight; // height of the screenprivate int ballradius; private canvasthread mythread; // control loop private Boolean isloop; Public canvasview (context) {super (context); // todo auto-generated constructor stubmyholder = This. getholder (); myholder. addcallback (this); ballpaint = new paint (); ballp Aint. setcolor (color. blue); isloop = true;} public void fireball (float startx, float starty) {system. out. println ("fire") ;}@ overridepublic void surfacechanged (surfaceholder holder, int format, int width, int height) {// todo auto-generated method stub} @ overrideprotected void onsizechanged (int w, int H, int oldw, int oldh) {super. onsizechanged (W, H, oldw, oldh); screenwidth = W; // store the widthscreenheig Ht = H; // store the heightballradius = W/10;} @ overridepublic void surfacecreated (surfaceholder holder) {// todo auto-generated method stubmythread = new canvasthread (); system. out. println ("surfacecreated! "); Mythread. start () ;}@ overridepublic void surfacedestroyed (surfaceholder holder) {// todo auto-generated method stub // stop loop isloop = false;} public void drawgameelements (canvas) {canvas. drawcircle (100,100, ballradius, ballpaint);} private class canvasthread extends thread {@ overridepublic void run () {While (true) {synchronized (myholder) {canvas = myholder. lockcanvas (null); // obtain the canvas drawgameelements (canvas); myholder. unlockcanvasandpost (canvas); // unlock the canvas and submit the painted image. // system. out. println ("Run ");}}}}}

Event processing class:

Package COM. example. bonusball; import android. OS. bundle; import android. app. activity; import android. view. menu; import android. content. context; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. rect; import android. view. gesturedetector; import android. view. gesturedetector. simpleongesturelistener; import android. view. motionevent; import android. view. surfaceholder; import android. view. surfaceview; import android. widget. toast; public class ballactivity extends activity {private gesturedetector mygesturedetector; // listener gesture private canvasview mycanvas; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); mycanvas = new canvasview (this); setcontentview (mycanvas); mygesturedetector = new gesturedetector (this, new mygesturelistener ());} @ override public Boolean ontouchevent (motionevent event) {return mygesturedetector. ontouchevent (event);} private class mygesturelistener extends simpleongesturelistener {public Boolean ondown (motionevent E1) {toast. maketext (getapplicationcontext (), "ondown", toast. length_short ). show (); Return true ;}@ override public Boolean onfling (motionevent E1, motionevent E2, float velocityx, float velocityy) {system. out. println ("Fling"); Return true ;}@ override public Boolean oncreateoptionsmenu (menu) {getmenuinflater (). inflate (R. menu. activity_ball, menu); Return true ;}}

Several concepts are explained:

Callback interface:

You only need to inherit the surfaceview class and implement surfaceholder. the callback interface can implement a custom surfaceview, surfaceholder. callback notifies view and surfaceholder when the underlying surface status changes. callback has the following interfaces:

  • Surfacecreated (surfaceholder
    Holder): This function is called immediately after the first surface is created. The program can perform initialization work related to the drawing interface in this function. Generally, the interface is drawn in another thread, so do not draw the surface in this function.
  • Surfacechanged (surfaceholder
    Holder, int format, int width, int height): This function is called when the surface state (size and format) changes, this function will be called at least once after surfacecreated is called.

Surfaceholder class:

It is an interface used to control the surface. It provides an interface to control the surface size, format, and pixels above, that is, to monitor its changes.

The getholder () function of surfaceview can obtain the surfaceholder object, surface
It is in the surfaceholder object. Although the surface saves the pixel data of the current window, it does not directly deal with the surface during use. The canvas lockcanvas () of surfaceholder or canvas lockcanvas () function to get the canvas object, and modify the data in the surface by drawing the content on the canvas. If the surface cannot be edited or you have not created it, calling this function will return NULL. The surface content in unlockcanvas () and lockcanvas () is not cached, so you need to completely repaint the surface content, to improve efficiency, you can call
The lockcanvas (rect) function is used to specify a rect region, so that content outside the region is cached. After the lockcanvas function is called to obtain the canvas, surfaceview obtains a synchronization lock of the surface until the unlockcanvasandpost (canvas) function is called to release the lock, the synchronization mechanism here ensures that it will not be changed (destroyed or modified) during surface painting ).

Finally, let's look at a complicated example that combines the previous gesture operations.

The result is: fling operations on the screen will generate a moving ball in the direction of the hand sliding, and the speed is the speed of the gesture sliding.

The ball is random in size, color, and rebound when it hits the boundary.

Running result:



Code List:

A ball is mainly used to set several attributes, as well as several set and get methods.

package com.example.bonusball;public class Ball {private float posX;private float posY;private float velocityX;private float velocityY;private float radius;private int color;public Ball(int rgb,float r,float pX,float pY,float vX,float vY){this.color=rgb;this.radius=r;this.posX=pX;this.posY=pY;this.velocityX=vX;this.velocityY=vY;}public float getRadius(){return radius;}public int getColor(){return color;}public float getX(){return posX;}public float getY(){return posY;}public float getVX(){return velocityX;}public float getVY(){return velocityY;}public void setPosX(float newX){this.posX=newX;}public void setPosY(float newY){this.posY=newY;}public void setVX(float newVX){this.velocityX=newVX;}public void setVY(float newVY){this.velocityY=newVY;}}

Master class:

Monitors touch time.

Package COM. example. bonusball; import android. OS. bundle; import android. app. activity; import android. view. menu; import android. content. context; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. rect; import android. view. gesturedetector; import android. view. gesturedetector. simpleongesturelistener; import android. view. motionevent; import android. view. surfaceholder; import android. view. surfaceview; import android. widget. toast; public class ballactivity extends activity {private gesturedetector mygesturedetector; // listener gesture private canvasview mycanvas; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); mycanvas = new canvasview (this); setcontentview (mycanvas); mygesturedetector = new gesturedetector (this, new mygesturelistener ());} @ override public Boolean ontouchevent (motionevent event) {return mygesturedetector. ontouchevent (event);} private class mygesturelistener extends simpleongesturelistener {public Boolean ondown (motionevent E1) {// toast. maketext (getapplicationcontext (), "ondown", toast. length_short ). show (); // mycanvas. fireball (e1.getrawx (), e1.getrawy (),); Return true ;}@ override public Boolean onfling (motionevent E1, motionevent E2, float velocityx, float velocityy) {system. out. println ("Fling"); Final int fling_min_distance = 100, fling_min_velocity = 200; If (math. ABS (e1.getx ()-e2.getx ()> fling_min_distance & math. ABS (velocityx)> fling_min_velocity) mycanvas. fireball (e1.getrawx (), e1.getrawy (), velocityx/4, velocityy/4); Return true ;}@ override public Boolean oncreateoptionsmenu (menu) {getmenuinflater (). inflate (R. menu. activity_ball, menu); Return true ;}}

Canvas type:

Draws images.

Package COM. example. bonusball; import Java. util. arraylist; import Java. util. list; import Java. util. random; import Java. util. concurrent. copyonwritearraylist; import android. content. context; import android. graphics. bitmap; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. rectf; import android. util. attributeset; import android. view. surfaceholder; Import android. view. surfaceview; public class canvasview extends surfaceview implements surfaceholder. callback {private surfaceholder myholder; private paint ballpaint; // paint used to draw the cannonballprivate int screenwidth; // width of the screenprivate int screenheight; // height of the screenprivate int maxballradius; private canvasthread mythread; private list <ball> balllist; private paint bac Kgroundpaint; private random mrandom; // control loop private Boolean isloop; Public canvasview (context) {super (context); // todo auto-generated constructor stubmyholder = This. getholder (); myholder. addcallback (this); ballpaint = new paint (); backgroundpaint. setcolor (color. black); isloop = true; balllist = new copyonwritearraylist <ball> (); mrandom = new random ();} public void fireball (FL Oat startx, float starty, float velocityx, float velocityy) {int rancolor = 0xff000000 | mrandom. nextint (0x00ffffff); float randomradius = mrandom. nextint (maxballradius); float tmpradius = maxballradius/5.0> randomradius? Maxballradius: randomradius; balllist. add (new ball (rancolor, tmpradius, startx, starty, velocityx, velocityy); system. out. println ("fire") ;}@ overridepublic void surfacechanged (surfaceholder holder, int format, int width, int height) {// todo auto-generated method stub} @ overrideprotected void onsizechanged (int w, int H, int oldw, int oldh) {super. onsizechanged (W, H, oldw, oldh); screenwidth = W; // store the W Idthscreenheight = H; // store the heightmaxballradius = W/10;} @ overridepublic void surfacecreated (surfaceholder holder) {// todo auto-generated method stubmythread = new canvasthread (); system. out. println ("surfacecreated! "); Mythread. start () ;}@ overridepublic void surfacedestroyed (surfaceholder holder) {// todo auto-generated method stub // stop loop isloop = false;} public void drawgameelements (canvas) {canvas. drawrect (0, 0, canvas. getwidth (), canvas. getheight (), backgroundpaint); For (ball B: balllist) {ballpaint. setcolor (B. getcolor (); canvas. drawcircle (B. getx (), B. gety (), B. getradius (), ballpaint) ;}} private void updatepos Itions (double elapsedtimems) {// todo auto-generated method stubfloat interval = (float) (elapsedtimems/1000.0); For (ball B: balllist) {B. setposx (B. getx () + B. getvx () * interval); B. setposy (B. gety () + B. getvy () * interval); If (B. getx () + B. getradius ()> screenwidth) {B. setvx (-1 * B. getvx (); // fixed the boundary B. setposx (screenWidth-b.getRadius ();} If (B. getx ()-B. getradius () <0) {B. setvx (-1 * B. getvx (); B. setposx (B. getradius ();} If (B. gety () + B. getradius ()> screenheight) {B. setvy (-1 * B. getvy (); B. setposy (screenHeight-b.getRadius ();} If (B. gety ()-B. getradius () <0) {B. setvy (-1 * B. getvy (); B. setposy (B. getradius () ;}} private class canvasthread extends thread {@ overridepublic void run () {canvas = NULL; long previusframetime = system. currenttimemillis (); While (isloop) {try {canvas = myholder. lockcanvas (null); // get the canvas Synchro Nized (myholder) {canvas. drawcolor (color. black); long currenttime = system. currenttimemillis (); double elapsedtimems = currenttime-previusframetime; updatepositions (elapsedtimems); // update game events (canvas); previusframetime = currenttime; // update previous time // system. out. println ("Run") ;}} finally {If (canvas! = NULL) myholder. unlockcanvasandpost (canvas); // unlock the canvas and submit the painted image} // end finally }}}}


One unsolved problem

In theory, surfaceview is dual-buffered, but in practice, the image still flashes.

Trying to write a new process to process image rendering failed.

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.