2016-04-20 13 views
1

Ich schrieb Code mit Anleitung, um meinen Hintergrund im Spiel zu bewegen, und es funktioniert, aber die Größe meines Hintergrunds ist original und ich möchte die Größe meines bewegten Hintergrunds im Vollbild, bitte, hilf mir))Wie mache ich Hintergrund im Vollbildmodus?

Code:

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); 
    } 

} 

}

Beispiel:

example

Antwort

0

Sie sollten versuchen, eine Transformationsmatrix zu verwenden, die für ScaleToFit.CENTER erstellt wurde. Zum Beispiel:

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

danke, aber wo sollte ich diesen Teil des Codes hinzufügen? – En1q0d

Verwandte Themen