2016-04-20 189 views
1

我写了代码与指导移动我的背景在游戏中,它的工作,但我的背景的大小是原创的,我想让我的移动背景的大小,请帮助我))如何使全屏幕背景?

代码:

public class Background extends SurfaceView implements 
    SurfaceHolder.Callback { 
private Bitmap backGround; 

public Background(Context context) { 
    super(context); 
    backGround = BitmapFactory.decodeResource(context.getResources(), 
      R.drawable.cold_planet); 
    setWillNotDraw(false); 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    doDrawRunning(canvas); 
    invalidate(); 
} 

@Override 
public void surfaceChanged(SurfaceHolder holder, int format, int width, 
          int height) { 

} 

@Override 
public void surfaceCreated(SurfaceHolder holder) { 

} 

@Override 
public void surfaceDestroyed(SurfaceHolder holder) { 

} 

/** 
* Draws current state of the game Canvas. 
*/ 

private int mBGFarMoveX = 0; 
private int mBGNearMoveX = 0; 

private void doDrawRunning(Canvas canvas) { 

    // decrement the far background 
    mBGFarMoveX = mBGFarMoveX - 1; 

    // decrement the near background 
    mBGNearMoveX = mBGNearMoveX - 4; 

    // calculate the wrap factor for matching image draw 
    int newFarX = backGround.getWidth() - (-mBGFarMoveX); 

    // if we have scrolled all the way, reset to start 
    if (newFarX <= 0) { 
     mBGFarMoveX = 0; 
     // only need one draw 
     canvas.drawBitmap(backGround, mBGFarMoveX, 5000, null); 

    } else { 
     // need to draw original and wrap 
     canvas.drawBitmap(backGround, mBGFarMoveX, 0, null); 
     canvas.drawBitmap(backGround, newFarX, 0, null); 
    } 

} 

}

实施例:

example

回答

0

您应该尝试使用为ScaleToFit.CENTER构建的转换矩阵。例如:

Matrix m = new Matrix(); 
m.setRectToRect(new RectF(0, 0, b.getWidth(), b.getHeight()), new RectF(0, 0, reqWidth, reqHeight), Matrix.ScaleToFit.CENTER); 
return Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true); 
+0

谢谢,但我应该在哪里添加这部分代码? – En1q0d