android - How to update View inflated from XML file in a SurfaceView's onDraw(Canvas canvas) method? -
so making pong game has activity sets contentview() surfaceview.
public class pong_activity extends activity { private static final string tag = pong_activity.class.getsimplename(); static int screenwidth; static int screenheight; public view image; layoutinflater inflater; relativelayout mainview; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); getwindow().addflags(windowmanager.layoutparams.flag_fullscreen); display screen = getwindowmanager().getdefaultdisplay(); rect rect_screen = new rect(); screen.getrectsize(rect_screen); screenwidth = rect_screen.width(); screenheight = rect_screen.height(); log.d(tag, "the size of screen width: " + screenwidth + "height: " + screenheight); // set content view surfaceview (the game panel) setcontentview(new maingamepanel(this)); log.d(tag, "surfaceview maingamepanel added"); }
within surfaceview have overrided ondraw() , call continuously through inner thread class make graphics move. know if not repaint canvas previous position of graphics retained usally call canvas.drawcolor() rid of that. want add in image @drawables/ place in background of surfaceview , refresh when ondraw() called, not seem drawing view (background) onto canvas.
public maingamepanel(context context ) { super(context); getholder().addcallback(this); // create thread game loop thread = new gamethread(getholder(), this); rectplayer = new player_rect(leftx,false); otherplayer = new player_rect(rightx,true); gameball = new ball(); inflater = (layoutinflater)context.getsystemservice(context.layout_inflater_service); background = inflater.inflate(r.layout.basic_game_screen, null); setfocusable(true); log.d(tag, "maingame panel, thread created"); } @override protected void ondraw(canvas canvas) { //canvas.drawcolor(color.black); background.draw(canvas); gameball.bordercollision(); gameball.rectanglecollison(rectplayer); gameball.rectanglecollison(otherplayer); otherplayer.compterresponse(gameball); if (gameball.cloneball != null) { gameball.cloneball.draw(canvas); } gameball.draw(canvas); rectplayer.draw(canvas); otherplayer.draw(canvas); }
this draw() called, inner class of maingamepanel
private class gamethread extends thread { private boolean running; private surfaceholder surfaceholder; private maingamepanel gamepanel; private canvas canvas; private static final string tag = "gamethread"; public gamethread(surfaceholder holder, maingamepanel panel) { super("gamethread"); this.surfaceholder = holder; this.gamepanel = panel; } public void setrunning(boolean running) { this.running = running; } @override public void run() { long threadruntime = 0l; log.d(tag, "starting game loop through game thread class."); while (running) { canvas = null; try { canvas = this.surfaceholder.lockcanvas(); // 1 accessible 1 thread @ time synchronized (surfaceholder) { // update canvas calling surface view methods. this.gamepanel.ondraw(canvas); } } { // if canvas exists reupdate drawn objects // canvas through if (canvas != null) { surfaceholder.unlockcanvasandpost(canvas); } } threadruntime++; } log.d(tag, "the thread ran " + threadruntime + "times"); } }
Comments
Post a Comment