在制作游戏时,背景可以移动,原理就是 两张图片的循环移动。
package com.example.backgroundtest;
import com.example.hundouluo.R;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.View;
public class BackGround {
private Bitmap background=null;
private Rect secrect,desrect,desrect2;
private int dy1,dy2;
private int x,y;
public BackGround(View view){
x=view.getWidth();
y=view.getHeight();
dy2=-view.getHeight();
background=BitmapFactory.decodeResource(view.getResources(), R.drawable.bg_level_2);
secrect=new Rect(0,0,background.getWidth(),background.getHeight());
desrect=new Rect(0,dy1,x,y);
desrect2=new Rect(0,dy2,x,y);
}
public void drawbackground(Canvas canvas){
dy1+=3;
dy2+=3;
desrect.set(0,dy1,x,y+dy1);
desrect2.set(0,dy2,x,y+dy2);
canvas.drawBitmap(background,secrect,desrect,null);
canvas.drawBitmap(background,secrect,desrect2,null);
if(dy1>=y)
dy1=-y+3;
if(dy2>=y)
dy2=-y+4;
}
}
这样就能实现背景循环移动啦!!!
如果还有不明白的,可以下载我的源码demo,很简单的。