Search code examples
androidbackgroundsurfaceviewimagefill

Android SurfaceView filling background with a picture


I'm a beginner/intermediate java-student and I just started working with android 2D programming. I'm working in surfaceView and want to set a backgroud, but the problem is I dont know how to make the background fill 100% of the screen, this is how I draw my background:

public GFXSurface(Context context) {
    super(context);
    mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.roadtest);
    getHolder().addCallback(this);
    mThread = new ViewThread(this);
}
public void doDraw(Canvas canvas){
    canvas.drawBitmap(mBitmap, 0, 0, null);
}

Solution

  • Something like that should do the trick, place it after obtaining your bitmap

    float scale = (float)mBitmap.getHeight()/(float)getHeight();
    int newWidth = Math.round(mBitmap.getWidth()/scale);
    int newHeight = Math.round(mBitmap.getHeight()/scale);
    Bitmap scaledBitmap = Bitmap.createScaledBitmap(mBitmap, newWidth, newHeight, true);
    

    then draw it like you already do :)