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
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\$