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

    this.map = map;
    this.player = player;
    this.background = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.background1), 480, 10000, false);
    this.sx = sx;
    this.sy = 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( map.map.bottomOfLevel != 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

  • 1
    \$\begingroup\$ Why do you say Canvas.scale(...) didn't work for you? How did you try it? \$\endgroup\$ Commented Nov 20, 2012 at 11:36
  • \$\begingroup\$ 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 \$\endgroup\$
    – Thomas
    Commented Nov 20, 2012 at 11:46
  • \$\begingroup\$ Need to see you onCreate and layout to answer...please provide and maybe we can help! \$\endgroup\$
    – amb
    Commented Nov 20, 2012 at 15:30
  • \$\begingroup\$ Scale the canvas using a Matrix related question: stackoverflow.com/questions/8722359/… \$\endgroup\$
    – Thomas
    Commented Nov 20, 2012 at 15:52
  • \$\begingroup\$ 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... \$\endgroup\$
    – Tonithy
    Commented Nov 18, 2013 at 6:56

2 Answers 2


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

  • \$\begingroup\$ Could you expand your answer? There are no details or examples contained. \$\endgroup\$
    – Pip
    Commented Jul 20, 2013 at 11:55
  • \$\begingroup\$ 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 \$\endgroup\$
    – Onur A.
    Commented Jul 20, 2013 at 12:11
  • \$\begingroup\$ @OnurA. You should add this to your answer \$\endgroup\$ Commented Jul 20, 2013 at 17:21

in run method when you want draw canvas you can use :

//out of run method
Rect dstRect = new Rect();
//in run method
canvas.drawBitmap(background, null, dstRect, null); 

it force canvas to draw & scale your final bitmap(background) for any device resolation (may strech in som device).


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .