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