Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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++ )
    {
        obstmp[i].draw(c);
    }

    for( int i = 0; i < cointmp.length; i++ )
    {
        cointmp[i].draw(c);
    }
    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");
    coin.draw(c);
}

Draw Function For Coin :

public void draw( Canvas c )
{
     obstical.draw(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;

    super.onCreate(savedInstanceState);

    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.
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        // 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 );
    setContentView(canvasView);

    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) 
{
    super(context);
    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() )
        {
            continue;
        }

        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;
            update();
        }
        c.drawBitmap(background, 0, by, null);
        map.draw(c, player.score);
        player.draw(c);

        holder.unlockCanvasAndPost(c);
    }
}

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

Cheers Daniel

share|improve this question
1  
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: stackoverflow.com/questions/8722359/… –  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
add comment

1 Answer

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

paint.setTextSize(textSize);

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 –  Potato Jul 20 '13 at 17:21
    
@Potato, done, thanks. –  Onur A. Jul 20 '13 at 17:22
add comment

Your Answer

 
discard

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.