0

I would like to set up a background image in my SurfaceView, but I can't get it to scale to the size of the screen. How can I set that up?

My Current Code:

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MySurface extends SurfaceView implements Runnable {

SurfaceHolder ourHolder;
Thread ourThread = null;
boolean isRunning = true;

Bitmap Background;
Bitmap clouda;



public MySurface(Context context) {
    super(context);
    init(context);

}

public MySurface(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);
}

public MySurface(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init(context);
}

private void init(Context context) {
    // do stuff that was in your original constructor...
    ourHolder = getHolder();
    ourThread = new Thread(this);
    ourThread.start();

    Background = BitmapFactory.decodeResource(getResources(), R.drawable.island);
    clouda = BitmapFactory.decodeResource(getResources(), R.drawable.clouda);




}



public void pause(){

}

public void resume(){

}

@Override
public void run() {
    // TODO Auto-generated method stub
    while(isRunning){
        if (!ourHolder.getSurface().isValid())
            continue;

        Canvas canvas = ourHolder.lockCanvas();

        canvas.drawBitmap(Background, 0, 0, null);

        canvas.drawBitmap(clouda, 0, 0, null);



        ourHolder.unlockCanvasAndPost(canvas);

      }

  }

}

I have tried to apply the top answer of this, but can't seem to figure out how to use the matrix.

Community
  • 1
  • 1
user3720864
  • 5
  • 1
  • 6
  • @zgc7009 Sorry, just added the code. I have tried a few things I've seen around, but I can't figure out how exactly to use them. – user3720864 Jul 25 '14 at 02:41
  • Use BitmapDrawable and set the bounds to fit the size of the surface – Mohammad Ersan Jul 25 '14 at 02:45
  • don't scale Bitmaps! use Canvas.drawBitmap(Bitmap, Matrix, Paint) instead – pskink Jul 25 '14 at 04:26
  • @pskink I saw in another post to use that, but to be honest I couldn't figure out how. I actually asked a [separate question](http://stackoverflow.com/questions/24945950/how-do-i-use-a-matrix-to-resize-a-bitmap-in-surfaceview) before this pertaining to that – user3720864 Jul 25 '14 at 04:48

1 Answers1

2

To scale the bitmap to the size of the SurfaceView

//The following two are just for viewing sake, they should be defined somewhere
SurfaceView targetSurfaceView;
Bitmap mySourceBitmap;

Bitmap scaledBitmap = Bitmap.createScaledBitmap(mySourceBitmap, 
                      targetSurfaceView.getWidth(), 
                      targetSurfaceView.getHeight(), 
                      true);

Then in the method you use to draw:

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    Paint paint = new Paint();
    canvas.drawBitmap(scaledBitmap, 0, 0, paint);
}

Hope this helps

quaternion
  • 77
  • 2
  • 16
  • 1
    no don't do that, use BitmapDrawable instead and set bounds to fit the surface size – Mohammad Ersan Jul 25 '14 at 02:45
  • Thank you. I have the SurfaceView set up in a different class called MySurface.java, so how would I call that as opposed to "targetSurfaceView"? – user3720864 Jul 25 '14 at 02:47
  • You're probably going to be calling the code inside MySurface.java in a draw function somewhere. So instead of calling targetSurfaceView, you'll just be calling getWidth() and getHeight() – quaternion Jul 25 '14 at 02:49
  • @MoshErsan Do you happen to have an example of how to apply that? – user3720864 Jul 25 '14 at 02:50
  • 1
    Just create a BitmapDrawable and call setBounds. Bitmap targetBmp; BitmapDrawable bmpDrawable = new BitmapDrawable(targetBmp); bmpDrawable.setBounds(0, 0, surfaceView.getWidth(), surfaceView.getHeight()); – quaternion Jul 25 '14 at 02:55
  • @quaternion I think I'm following you, but I still can't seem to get it to run. In the first example I it seems to crash when opening, but shows no errors, and in the second example it has a strike through on BitmapDrawable and says that it's deprecated and won't let me set bounds. – user3720864 Jul 25 '14 at 03:24
  • @quaternion Sorry for the extra reply (accidentally hit enter)..Could it be that I am placing something in the incorrect spot? I'm entering the Bitmap with my initial variables, Defining the location of bitmap in the constructor (Do I need anything else in the constructor?), and placing the paint and draw bitmap under my run method. – user3720864 Jul 25 '14 at 03:28
  • You can edit, but I can't really tell you what you're doing wrong without seeing more code. Try using http://pastebin.com/ – quaternion Jul 25 '14 at 03:31
  • Sorry had to head away for the weekend, but just had a quick look at your code now. Not sure if it will work 100%, but [here's an update on your code](http://pastebin.com/YVPxfqQY) - Let me know if it's all good. – quaternion Jul 28 '14 at 00:05