/MoveBack/src/com/lxm/moveback/MainActivity.java
package com.lxm.moveback;
import java.util.Timer;
import java.util.TimerTask;
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.Menu;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
class MyView extends View{
final int BACK_HEIGHT = 1700;
private Bitmap plane;
private Bitmap back;
final int WIDTH = 320;
final int HEIGHT = 440;
private int startY = BACK_HEIGHT - HEIGHT;
public MyView(Context context) {
super(context);
plane = BitmapFactory.decodeResource(context.getResources(),
R.drawable.plane);
back = BitmapFactory.decodeResource(context.getResources(),
R.drawable.back_img);
final Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
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) {
Bitmap bitmap2 = Bitmap
.createBitmap(back, 0, startY, WIDTH, HEIGHT);
canvas.drawBitmap(bitmap2, 0, 0, null);
canvas.drawBitmap(plane, 160, 360, null);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
效果图: