码迷,mamicode.com
首页 > 移动开发 > 详细

Android_(游戏)打飞机02:游戏背景滚动

时间:2018-08-10 01:20:25      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:res   bsp   处理   www   jin   class   arraylist   rgb   prot   

 

  Android_(游戏)打飞机01:前言  传送门

  Android_(游戏)打飞机02:游戏背景滚动  传送门

 

游戏背景滚动效果

技术分享图片

 

技术分享图片
package com.example.administrator.myapplication;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/8/9.
 */

public class dafeijiGameView extends SurfaceView implements
        SurfaceHolder.Callback,Runnable{

    private Bitmap my;//自己
    private Bitmap baozha;//爆炸
    private Bitmap bg;//背景
    private Bitmap diren;//敌人
    private Bitmap zidan;//子弹
    private Bitmap erjihuancun;//二级缓存
    private WindowManager windowManager;//获得界面长宽高
    private int display_w;  //界面的宽
    private int display_h;  //界面的高
    private List<GameImage> gameImage = new ArrayList();

    public dafeijiGameView(Context context) {
        super(context);
        getHolder().addCallback(this);


    }

    private void init(){
        //加载照片
        my= BitmapFactory.decodeResource(getResources(),R.drawable.my);
        baozha= BitmapFactory.decodeResource(getResources(),R.drawable.baozha);
        bg= BitmapFactory.decodeResource(getResources(),R.drawable.bg);
        diren= BitmapFactory.decodeResource(getResources(),R.drawable.diren);
        zidan= BitmapFactory.decodeResource(getResources(),R.drawable.zidan);

        erjihuancun=Bitmap.createBitmap(display_w,display_h, Bitmap.Config.ARGB_8888);
        gameImage.add(new BeijingImage(bg));    //先加入背景照片
    }


    private interface GameImage{
        public Bitmap getBitmap();
        public int getX();
        public int getY();
    }

    //负责背景照片的处理
    private class BeijingImage implements GameImage{
        private Bitmap bg;
        private BeijingImage(Bitmap bg){
            this.bg=bg;
            newBitmap = Bitmap.createBitmap(display_w,display_h, Bitmap.Config.ARGB_8888);
        }

        private Bitmap newBitmap = null;
        private int height = 0;

        public Bitmap getBitmap(){
            Paint p = new Paint();
            Canvas canvas = new Canvas(newBitmap);
            canvas.drawBitmap(bg,
                    new Rect(0,0,bg.getWidth(),bg.getHeight()),
                    new Rect(0,height,display_w,display_h+height),p);

            canvas.drawBitmap(bg,
                    new Rect(0,0,bg.getWidth(),bg.getHeight()),
                    new Rect(0,-display_h+height,display_w,height),p);

            height++;
            if(height==display_h){
                height=0;
            }
            return newBitmap;
        }

        public int getX(){
            return 0;
        }

        public int getY(){
            return 0;
        }
    }


    private boolean state = false;
    private SurfaceHolder holder;

    //绘画中心
    public void run() {
        Paint p1 = new Paint();
        try{
            while(true){
                Canvas newCanvas = new Canvas(erjihuancun);

                for(GameImage image:gameImage){
                    newCanvas.drawBitmap(image.getBitmap(),image.getX(),image.getY(),p1);
                }

                Canvas canvas = holder.lockCanvas();
                canvas.drawBitmap(erjihuancun,0,0,p1);
                holder.unlockCanvasAndPost(canvas);
                Thread.sleep(10);
            }
        }catch(Exception e){
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
    }



    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        state = false;
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        //得到屏幕的宽和高
        display_w=width;
        display_h=height;
        init();
        this.holder=holder;
        state = true;
        new Thread(this).start();
    }

}
dafeijiView.java

 

技术分享图片
package com.example.administrator.myapplication;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;


public class MainActivity extends AppCompatActivity{


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //去掉标题
       // requestWindowFeature(Window.FEATURE_NO_TITLE);
          dafeijiGameView view =  new dafeijiGameView(this);

        setContentView(view);
    }


}
MainActivity.java

 

实现过程

 

  使用线程绘画游戏

public void run() {
        Paint p1 = new Paint();
        try{
            while(true){
                Canvas newCanvas = new Canvas(erjihuancun);

                for(GameImage image:gameImage){
                    newCanvas.drawBitmap(image.getBitmap(),image.getX(),image.getY(),p1);
                }

                Canvas canvas = holder.lockCanvas();
                canvas.drawBitmap(erjihuancun,0,0,p1);
                holder.unlockCanvasAndPost(canvas);
                Thread.sleep(10);
            }
        }catch(Exception e){
        }
    }

 

 

  绘画移动的背景图片

 public Bitmap getBitmap(){
            Paint p = new Paint();
            Canvas canvas = new Canvas(newBitmap);
            canvas.drawBitmap(bg,
                    new Rect(0,0,bg.getWidth(),bg.getHeight()),
                    new Rect(0,height,display_w,display_h+height),p);

            canvas.drawBitmap(bg,
                    new Rect(0,0,bg.getWidth(),bg.getHeight()),
                    new Rect(0,-display_h+height,display_w,height),p);

            height++;
            if(height==display_h){
                height=0;
            }
            return newBitmap;
        }

 

  surfaceCreated:创建时初始化图片并开启线程绘画游戏

  @Override
    public void surfaceCreated(SurfaceHolder holder) {
    }



    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        state = false;
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        //得到屏幕的宽和高
        display_w=width;
        display_h=height;
        init();
        this.holder=holder;
        state = true;
        new Thread(this).start();
    }

 

  初始化函数。加载图片

    private void init(){
        //加载照片
        my= BitmapFactory.decodeResource(getResources(),R.drawable.my);
        baozha= BitmapFactory.decodeResource(getResources(),R.drawable.baozha);
        bg= BitmapFactory.decodeResource(getResources(),R.drawable.bg);
        diren= BitmapFactory.decodeResource(getResources(),R.drawable.diren);
        zidan= BitmapFactory.decodeResource(getResources(),R.drawable.zidan);

        erjihuancun=Bitmap.createBitmap(display_w,display_h, Bitmap.Config.ARGB_8888);
        gameImage.add(new BeijingImage(bg));    //先加入背景照片
    }

  

  线程中使用二级缓存绘画背景,每隔10ms绘画一次

                Canvas canvas = holder.lockCanvas();
                canvas.drawBitmap(erjihuancun,0,0,p1);
                holder.unlockCanvasAndPost(canvas);
                Thread.sleep(10);

 

 

  图片坐标的移动

           Paint p = new Paint();
            Canvas canvas = new Canvas(newBitmap);

       //第一张图片 canvas.drawBitmap(bg,
new Rect(0,0,bg.getWidth(),bg.getHeight()), new Rect(0,height,display_w,display_h+height),p);
       //第二张图片 canvas.drawBitmap(bg,
new Rect(0,0,bg.getWidth(),bg.getHeight()), new Rect(0,-display_h+height,display_w,height),p); height++;

  宽度不发生改变,长度发生变化,两张图片的高不断升高(height++)

 

canvas.drawBitmap(bitmap, srcRect, dstRect, paint) :在图片上绘制图片  传送门

  1参:要绘制的图片

  2参: 是对图片进行裁截,可以传null,传null则表示显示整个图片

  3参:目标区域,即把原区域改变参数,得到自己想要的图片及位置,是图片在Canvas画布中显示的区域

  4参:paint画笔,可以为null

 

Rect(left,top,right,bottom) :用于表示坐标系中的一块矩形区域   

  left:左

  top:上

  right:右

  bottom:下

 

            if(height==display_h){
                height=0;
            }

  其实只是两张照片拼接而成,两张图片不断的向上移动。开始时,第一张在手机屏幕中间,第二张在手机屏幕下方,第一张图片超出手机最上方的范围时候拼接到第一张图片下面,下一张图片不断往上运动形成新的第一张图片在手机屏幕中间,两张背景图片就这样不断的进行循环

 

 

在MainActivity.java中添加这一句可以去掉标题

       requestWindowFeature(Window.FEATURE_NO_TITLE);    

 

 

 

 

Android_(游戏)打飞机02:游戏背景滚动

标签:res   bsp   处理   www   jin   class   arraylist   rgb   prot   

原文地址:https://www.cnblogs.com/1138720556Gary/p/9452487.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!