我有2个问题。 首先为什么背景不是全屏但我有这个空间的底部? 第二个原因是为什么在没有绘制背景的情况下这种奇怪的船舶延迟?
package com.caccagame;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
public class GamePanel extends SurfaceView implements Callback {
private MainThread thread;
private boolean pause_game;
private Background background;
private float shipSpeed;
private Ship ship;
private int x;
private int y;
public GamePanel(Context context, Game game, int screenWidth, int screenHeight ) {
super(context);
getHolder().addCallback(this);
thread=new MainThread(getHolder(), this);
background=new Background(BitmapFactory.decodeResource(getResources(), R.drawable.game_fon), screenWidth, screenHeight, this);
setFocusable(true);
ship=new Ship(BitmapFactory.decodeResource(getResources(), R.drawable.player), screenWidth, screenHeight);
x=100; //posizione iniziale della nave X
y=screenHeight/2;
shipSpeed=screenWidth/2.f;
}
/*public boolean onTouchEvent(MotionEvent event){
if(event.getAction()==MotionEvent.ACTION_DOWN)
ship.setUp(true);
if(event.getAction()==MotionEvent.ACTION_UP){
ship.setUp(false);
}
return true;
}*/
public boolean onTouchEvent(MotionEvent event){
if(event.getAction()==MotionEvent.ACTION_UP){
return true;
}
if(event.getAction()==MotionEvent.ACTION_MOVE){
x=(int)event.getX();
y=(int)event.getY();
}
if(event.getAction()==MotionEvent.ACTION_DOWN){
return true;
}
return true;
}
public void draw(Canvas canvas){
if(!pause_game)
if(canvas!=null){
background.draw(canvas);
ship.draw(canvas);
}
}
public void update(float dt){
background.update(dt);
ship.update(dt, x, y);
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
thread.setRunning(true);
thread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
boolean retray=true;
while(retray){
try{
thread.join();
retray=false;
}catch(InterruptedException e){
System.out.println("errore thread");
}
}
}
public boolean getPauseGame(){
return this.pause_game;
}
public void setPauseGame(boolean isGamePause){
this.pause_game=isGamePause;
}
public float getShipSpeed() {
return shipSpeed;
}
public void setThread(boolean isRunning){
thread.setRunning(isRunning);
}
}
package com.caccagame;
import java.util.ArrayList;
import android.graphics.Bitmap;
import android.graphics.Canvas;
public class Ship {
private Bitmap bitmap;
private int x;
private int y;
private int speed;
private int screenWidth;
private int screenHight;
private ArrayList<Bitmap> boom;
private boolean death;
//private boolean up;
//private float verticalSpeed;
private float animationTime;
private float animationTotalTime;
private float numFrame; //e' uguale alla lunghezza dell'array
public Ship(Bitmap decodeResource, int screenWidth, int screenHight) {
this.bitmap=decodeResource;
this.x=0;
this.y=0;
this.screenWidth=screenWidth;
this.screenHight=screenHight;
speed=1;
death=false;
boom=null;
animationTime=0;
animationTotalTime=1;
//verticalSpeed=0;
}
public void setBoomAnimation(ArrayList<Bitmap> boomBitmap){
this.boom=new ArrayList<Bitmap> (boomBitmap);
numFrame=boom.size()+1;
}
public void draw(Canvas canvas){
if(!death){
canvas.drawBitmap(bitmap, x-bitmap.getWidth()/2, y-bitmap.getHeight()/2, null);
}
else{
int index=(int)(animationTime/animationTotalTime*numFrame);
if(index<numFrame){
canvas.drawBitmap(boom.get(index), x-bitmap.getWidth()/2, y-bitmap.getHeight()/2, null);
}
}
}
public void update(float dt, int x, int y){
if(death){
animationTime+=dt;
}
else{
this.x=x;
this.y=y;
}
}
/*public void draw(Canvas canvas){
if(!death){
canvas.drawBitmap(bitmap, x-bitmap.getWidth()/2, y-bitmap.getHeight()/2, null);
}
else{
int index=(int)(animationTime/animationTotalTime*numFrame);
if(index<numFrame){
canvas.drawBitmap(boom.get(index), x-bitmap.getWidth()/2, y-bitmap.getHeight()/2, null);
}
}
}*/
/*public void update(float dt){
if(death){
animationTime+=dt;
}
else{
verticalSpeed+=screenHight/2*dt;
if(up)
verticalSpeed-=screenHight*dt*2;
y+=verticalSpeed*dt;
if(y-(bitmap.getHeight()/2)>screenWidth)
y=0-(bitmap.getHeight()/2);
}
}*/
/*public void setUp(boolean b) {
this.up=b;
} */
}
package com.caccagame;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
public class Background {
Bitmap backBitmap;
int x;
int y;
int screenWidth;
int countBackground;
GamePanel gamePanel;
Paint paint=new Paint();
public Background(Bitmap bm, int screen_w, int screen_h, GamePanel gamePanel){
this.backBitmap=bm;
this.screenWidth=screen_w;
this.x=0;
this.y=0;//(screen_h- backBitmap.getHeight())/2 ;
this.countBackground=screenWidth/backBitmap.getWidth()+1;
this.gamePanel=gamePanel;
paint.setFilterBitmap(true);
}
public void draw(Canvas canvas){
for(int i=0; i<countBackground+1; i++){
if(canvas!=null)
canvas.drawBitmap(backBitmap, backBitmap.getWidth()*i+x, y, paint);
}
if(Math.abs(x)>backBitmap.getWidth())
x=x+backBitmap.getWidth();
}
public void update(float dt){
x=(int)(x-gamePanel.getShipSpeed()*dt);
}
}
答案 0 :(得分:0)
您绘制的位图很可能是错误的大小。尝试使用
drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint);
src
为null
RectF dest = new RectF(backBitmap.getWidth() * i + x, y, backBitmap.getWidth() * i + x + screen_w,y + screen_h);
canvas.drawBitmap(backBitmap, null, dest, paint);