0

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

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

Cheers Daniel

4

2 回答 2

0

覆盖 onMeasure() 函数并计算屏幕宽度和屏幕高度,然后计算比例。

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {     
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        screen_width = MeasureSpec.getSize(widthMeasureSpec);
        screen_height= MeasureSpec.getSize(heightMeasureSpec);
}
于 2012-11-20T05:31:48.263 回答
0

试试这个,这给了你改变的屏幕尺寸

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    // TODO Auto-generated method stub
      super.onSizeChanged(w, h, oldw, oldh);
      screen_width = w;
      screen_height= h;
}
于 2012-11-20T05:41:39.477 回答