Android Aircraft War (ii) Surfaceview to achieve a self-made background

Source: Internet
Author: User

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

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.