Write a homemade background image with Surfaceview, and you can move it, plus the Backgroundmanager class in the Android Aircraft War (a), you can use it directly
Gameview Code:
public class Gameview extends Surfaceview implements surfaceholder.callback,runnable{
Private Surfaceholder Hd=null;
Private Canvas Canvas=null;
Private Backgroundmanager Backgroundmanager;
Public Gameview (Context context) {
Super (context);
This.setfocusable (TRUE);
Hd=this.getholder ();
Hd.addcallback (this);
}
public void Run () {
while (isrunning) {
Mydraw ();
try {
Thread.Sleep (100);//Refresh the screen every 100s
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
public void Mydraw () {
try{
if (hd!=null) {
Canvas=hd.lockcanvas ();
Canvas.drawcolor (Color.Blue);
Backgroundmanager.drawbackground (canvas);
}catch (Exception e) {
E.printstacktrace ();
}finally{
if (canvas!=null)
Hd.unlockcanvasandpost (canvas);
}
}
public void surfacechanged (Surfaceholder arg0, int arg1, int arg2, int arg3) {
TODO auto-generated Method Stub
}
public void surfacecreated (Surfaceholder arg0) {
Thread=new thread (this);//define a new thread
Thread.Start ();
Backgroundmanager=new Backgroundmanager (this);
}
@Override
public void surfacedestroyed (Surfaceholder arg0) {
}
Mainactivity Code:
public class Mainactivity extends Activity {
Private Gameview Gameview=null;
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Gameview=new Gameview (this);
Setcontentview (Gameview);
}
This allows the background of the loop to move, the layout of the XML file directly without the tube.
This article is from the "Software Learning Summary" blog, be sure to keep this source http://bigcrab.blog.51cto.com/10626858/1685030
Android Aircraft War (ii) Surfaceview to achieve a self-made background