Android游戏动态背景

        让一张图滚动,实现动态效果,只要不断地改变x轴或y轴的坐标。

 

        在工程中新建一个类MySurfaceView,继承SurfaceView,然后把MainActivity中的标题栏,和提示栏去掉,并且设置显示SurfaceView实例。

        

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//隐去标题栏,状态栏
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(new MySurfaceView(this));
	}
}

 

 

     

 然后在MySurfaceView中初始化:

    // 声明一张位图
    private Bitmap bitmap;
    //声明x,y坐标
    private int x,y;

 

视图的构造函数:

public void surfaceCreated(SurfaceHolder holder) {
                         bitmap=BitmapFactory.decodeResource(this.getResources(),R.drawable.background);
//使其下部正好填满整个屏幕
 X=this.getWidth()-bitmap.getWidth();
Y=this.getHeight()-bitmap.getHeight();
}

 

绘制图片

private void myDraw() {
		Canvas canvas=sfh.lockCanvas();//获取canvas
		
		canvas.drawBitmap(bitmap, X, Y, paint);//画背景
		canvas.drawBitmap(bitmap, X, Y-bitmap.getHeight(), paint);//画背景连续
		//当第一幅画被显示完,将坐标恢复原值,循环显示
		if(Y>this.getHeight()){
			Y=this.getHeight()-bitmap.getHeight();
		}
		sfh.unlockCanvasAndPost(canvas);//解锁画布和提交
	}

 

修改y值,实现动态

public void logic(){
		Y+=3;
}

 

最后在线程中调用之前的两个函数,实现页面的刷新,使图片动起来。

public void run() {
		while(flag){
			myDraw();
			logic();
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

 一张图片背景动态显示就完成啦。

 

 

下面是整体的MySurfaceView中的代码

package com.example.game_myview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
/*
 * 通过 SurfaceHolder获取本页的canvas,然后再进行绘制,所以没有onDraw方法,要自定义myDraw
 */
public class MySurfaceView extends SurfaceView implements Callback,Runnable{
	private SurfaceHolder sfh;
	private Paint paint;
	private Bitmap bitmap;
	private int bitX,bitY;
	private Thread th;
	private static boolean flag;
	public MySurfaceView(Context context) {
		super(context);
		sfh=this.getHolder();
		sfh.addCallback(this);//添加状态监听
		
		paint=new Paint();
		paint.setColor(Color.WHITE);
		
		setFocusable(true);
	}
	/*	视图建立,响应这个函数
	 *	 获取位图,调用onDraw
	 */
	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		bitmap=BitmapFactory.decodeResource(this.getResources(),R.drawable.use3);
		//位图初始换坐标填充屏幕
		bitX=this.getWidth()-bitmap.getWidth();
		bitY=this.getHeight()-bitmap.getHeight();
		flag=true;
		th=new Thread(this);
		th.start();
	}
	/*
	 * 自定义绘图函数
	 */
	private void myDraw() {
		Canvas canvas=sfh.lockCanvas();//获取canvas
		
		canvas.drawBitmap(bitmap, bitX, bitY, paint);//画背景
		canvas.drawBitmap(bitmap, bitX, bitY-bitmap.getHeight(), paint);//画背景连续
		//当第一幅画被显示完,将坐标恢复原值,循环显示
		if(bitY>this.getHeight()){
			bitY=this.getHeight()-bitmap.getHeight();
		}
		sfh.unlockCanvasAndPost(canvas);//解锁画布和提交
	}
	//逻辑函数
	public void logic(){
		bitY+=5;
	}
	@Override
	public void run() {
		while(flag){
			myDraw();
			logic();
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
	}
	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		flag=false;
	}
}

 上传了飞机大战的图片


Android游戏动态背景
 

 

你可能感兴趣的:(android)