Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am writing a android game using Canvas as the way to draw everything, the problem is that when i run it on different android phones the canvas dosn't change size i tried using canvas.scale() but that didn't make a i difference. The code i use for drawing is ...

public void draw( Canvas c, int score )
    Obstical2[] obstmp = Queue.toArray(this.o);
    Coin[] cointmp = QueueC.toArray(this.c);

    for( int i = 0; i < obstmp.length; i++ )

    for( int i = 0; i < cointmp.length; i++ )
    c.drawText(String.format("%d", score ), 20, 50, textPaint);

    if( isWon && isStarted ) c.drawText("YOU WON", 20, 400, resPaint);
    else if( isLost && isStarted ) c.drawText("YOU LOST", 20, 400, resPaint);

The function above calls the draw functions for the entity's on the screen, theses function are as follows

Draw Function For Obstical :

public void draw( Canvas c )
    Log.i("D", "COIN");

Draw Function For Coin :

public void draw( Canvas c )

The on create function is :

protected void onCreate(Bundle savedInstanceState) 
    player = new Player(this);
    //map = new Map(this, );
    WindowManager w = this.getWindowManager();
    Display d = w.getDefaultDisplay();

    float sy = d.getHeight()/800;
    float sx = d.getWidth()/480;


    Intent intent = getIntent();
    String type = intent.getStringExtra(CUBEMenuActivity.EXTRA_MESSAGE);

    Log.i("MENU", type);

    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    //mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
    mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

       // Remove the title bar from the window.

        // Make the windows into full screen mode.
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    map = new Map( this, type, sy, sx );

    canvasView = new CanvasSurfaceView(this, map, player, sx, sy );

    Log.i("C", "Canvas View Set");


i am using a canvasSurfaceView instead of an XML layout, the canvasSurfaceView constructor method is

public CanvasSurfaceView(Context context, Map map, Player player, float sx, float sy) 
    holder = getHolder();

    gestures = new GestureDetector(new GestureListener(this)); = map;
    this.player = player;
    this.background = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.background1), 480, 10000, false); = sx; = sy;

when the game is running the run() method is called repeatedly this is where the canvas class is initialized

public void run()
    while( run == true )
        if( !holder.getSurface().isValid() )

        Canvas c = holder.lockCanvas();

        if( ( map.isWon || map.isLost ) && map.isStarted ) setCompleated();
        //c.scale(sx, sy);

        if( notCompleated == false )    

            if( != 800 ) by += player.terminalVelosity / 2;
        c.drawBitmap(background, 0, by, null);
        map.draw(c, player.score);


How could i make the canvas re-size to it would look the same on any screen ?

Cheers Daniel

share|improve this question
Why do you say Canvas.scale(...) didn't work for you? How did you try it? – Shivan Dragon Nov 20 '12 at 11:36
Is it possible to use a matrix for the scale? I know a rotation and translation matrix can be used when drawing a bitmap on canvas – Thomas Nov 20 '12 at 11:46
Need to see you onCreate and layout to answer...please provide and maybe we can help! – amb Nov 20 '12 at 15:30
Scale the canvas using a Matrix related question:… – Thomas Nov 20 '12 at 15:52
QueueC.toArray(this.c) It seems like it could be confusing to have your [seems to be] Queue of coins shadow the name of your Canvas parameter... I would avoid something like that... – Tonithy Nov 18 '13 at 6:56

To do this, change the size of your paint object. Also, scale the bitmaps. Scaling the canvas doesn't do anything.


and for bitmaps

bitmap=Bitmap.createScaledBitmap(src, dstWidth, dstHeight, filter) 

where filter is whether the bilinear filtering is enabled

share|improve this answer
Could you expand your answer? There are no details or examples contained. – Pip Jul 20 '13 at 11:55
for paint you can do paint.setTextSize(textSize); and for bitmaps bitmap=Bitmap.createScaledBitmap(src, dstWidth, dstHeight, filter) where filter is whether the bilinear filtering is enabled – Onur A. Jul 20 '13 at 12:11
@OnurA. You should add this to your answer – Battle_Pasture Jul 20 '13 at 17:21
@Potato, done, thanks. – Onur A. Jul 20 '13 at 17:22

What Onur said is a lie. The canvas can be scaled, just not like that. Here is how it is done:

public void render(Canvas c){
    final float scaleFactorX = getWidth()/(WIDTH*1.f);
    final float scaleFactorY = getHeight()/(HEIGHT*1.f);

    if(c!=null && !IS_PAUSED) {

        final int savedState =;
        c.scale(scaleFactorX, scaleFactorY);//scales the images to make the screen look somewhat normal
        ////////////////////////////////////rendering start

        all the rendering must be done in between here to scale it

        /////////////////////////////////////rendering stop
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.