- package com.sarnath.activity;
- 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.graphics.Matrix;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.view.Display;
- import android.view.View;
- public class GbdemoActivity extends Activity {
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(new MyView(this));
- }
- class MyView extends View {
- // 得到容器分辨率 高,宽
- Display display = getWindowManager().getDefaultDisplay();
- // 记录背景图片实际的高度
- final int BACK_HEIGHT = 960;
- // 背景图片
- private Bitmap back;
- private Bitmap plane;
- private Bitmap map_01;
- // 背景图片的开始位置 320 480
- final int WIDTH = display.getWidth();
- final int HEIGHT = display.getHeight();
- private int startY = BACK_HEIGHT - HEIGHT;
- public MyView(Context context) {
- super(context);
- // 获取背景图片
- back = BitmapFactory.decodeResource(context.getResources(),
- R.drawable.map_02);
- plane = BitmapFactory.decodeResource(context.getResources(),
- R.drawable.plan_1);
- final Handler handler = new Handler() {
- public void handleMessage(Message msg) {
- if (msg.what == 0x123) {
- // 重新开始移动
- if (startY <= 0) {
- startY = BACK_HEIGHT - HEIGHT;
- } else {
- startY -= 10;
- }
- }
- //该函数的作用是使整个窗口客户区无效。窗口的客户区无效意味着需要重绘
- invalidate();
- }
- };
- new Timer().schedule(new TimerTask(){
- @Override
- public void run() {
- handler.sendEmptyMessage(0x123);
- }
- }, 0,100);
- }
- @Override
- protected void onDraw(Canvas canvas) {
- // 根据原始位图和Matrix创建新图片
- Bitmap bitmap2 = Bitmap.createBitmap(back, 0, startY, WIDTH,HEIGHT);
- //设置拉伸的比例 实现 等腰梯形
- float[] src1=new float[] { 0, 0, WIDTH, 0, WIDTH, HEIGHT, 0, HEIGHT };
- float[] dst=new float[] { 100, 0, 200, 0, 500, 450, -150, 450 };
- //获取Matrix对象 实现 图片拉伸
- Matrix mMatrix=new Matrix();
- mMatrix.setPolyToPoly(src, 0, dst, 0, src.length >> 1);
- // 绘制新位图
- canvas.drawBitmap(bitmap2, mMatrix, null);
- //绘制小飞机
- canvas.drawBitmap(plane, 160, 380 ,null);
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/291020136732.html
来源: http://www.codesnippet.cn/detail/291020136732.html