初学Android,图形图像之游戏画面中飞行的飞机(三十)


上面的飞机移动背景是用定时器不断挖取源位图的块,从而形成背景移动的假象
控制图片背景不断下移,就会感觉飞机在不断的飞行
本程序有两个位图,一个背景图,高度远超过屏幕的高度,然后是一张飞机的位图

package WangLi.Graphics.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.View; public class MoveBack extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new MyView(this)); } class MyView extends View { //记录背景位图的实际高度 final int BACK_HEIGHT = 1700; //背景图片 private Bitmap back; private Bitmap plane; //背景图片的开始位置 final int WIDTH = 371; final int HEIGHT = 440; private int startY = BACK_HEIGHT - HEIGHT; public MyView(Context context) { super(context); // TODO Auto-generated constructor stub back = BitmapFactory.decodeResource(context.getResources() , R.drawable.back_img); plane = BitmapFactory.decodeResource(context.getResources() , R.drawable.plane); final Handler handler = new Handler() { public void handleMessage(Message msg) { if (msg.what == 0x123) { //重新开始移动 if (startY <= 0) { startY = BACK_HEIGHT - HEIGHT; } else { startY -= 3; } } invalidate(); } }; new Timer().schedule(new TimerTask() { @Override public void run() { handler.sendEmptyMessage(0x123); }}, 0 , 10); } @Override public void onDraw(Canvas canvas) { // 根据原始位图和Matrix创建新图片 Bitmap bitmap2 = Bitmap.createBitmap(back, 0, startY , WIDTH , HEIGHT); // 绘制新位图 canvas.drawBitmap(bitmap2, 0 , 0, null); // 绘制飞机 canvas.drawBitmap(plane, 160 , 380, null); } } }



【初学Android,图形图像之游戏画面中飞行的飞机(三十)】

    推荐阅读