Mobile game Background

Source: Internet
Author: User

With the help of Bitmap's CreateBitmap () method, you can "dig" one piece of the source bitmap, so that you can "dig" the blocks of the source bitmap in different locations through the timer control in the program, giving the user a "false impression" of the background movement.

Import android.app.Activity;
Import Android.content.Context;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
Import Android.graphics.Canvas;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.os.Message;
Import Android.view.View;

public class Moveback extends activity{

@Override protected void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (New MyView (this));

}

Class MyView extends view{

Record the actual height of the background bitmap

Final int back_height = 1700;

Background image

Private Bitmap back;

Private Bitmap plane;

Start position of background picture

Final int WIDTG = 320;

Final int HEIGHT = 440;

private int starty = Back_height-height;

Public MyView (Context context) {

Super (context);

back = Bitmapfactory.decoderesource (Context.getresources (), r.drawable.back_img);

Plane = Bitmapfactory.decoderesource (Context.getresources (), R.drawable.plane);

Final Handler Handler = new Handler () {

@Override

public void Handlemessage (Message msg) {

if (msg.what = = 0x123) {

Start moving again

if (starty <= 0) {

Starty = Back_height-height;

}else{

Starty-= 3;

}

}

Invalidate ();

}

};

New Timer (). Schedule (new TimerTask () {

@Override

public void Run () {

Handler.sendemptymessage (0x123);

}

}, 0, 100);

}

@Override

protected void OnDraw (canvas canvas) {

Create a new picture based on the original bitmap and matrix

Bitmap bitmap2 = Bitmap.createbitmap (back, 0, Starty, WIDTG, HEIGHT);

Draw a new bitmap

Canvas.drawbitmap (bitmap2, 0, 0, NULL);

Drawing airplanes

Canvas.drawbitmap (plane, 380, NULL);

}

}

}

Mobile game 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.