My problem is a simple one however the solution eludes me; I want the Bitmaps to rescale in respect to the screen size.
Picture of the problem:
Beneath is the code, I am not sure how much to include as I really cannot figure out why this is happening.
public TitleView(Context context) {
super(context);
myContext = context;
titleGraphicFixed = BitmapFactory.decodeResource(getResources(),
R.drawable.title_background);
playButtonUpFixed = BitmapFactory.decodeResource(getResources(),
R.drawable.button_play_up);
playButtonDownFixed = BitmapFactory.decodeResource(getResources(),
R.drawable.button_play_down);
}
@Override
public void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
screenW = w;
screenH = h;
drawScaleW = (float) screenW / 800;
drawScaleH = (float) screenH / 600;
Log.d(TAG, "Screen Width: " + drawScaleW);
Log.d(TAG, "Screen Height: " + drawScaleH);
titleGraphic = Bitmap.createScaledBitmap(titleGraphicFixed, screenW,
screenH, true);
playButtonUp = Bitmap.createScaledBitmap(playButtonUpFixed,
(int) (playButtonUpFixed.getWidth() * drawScaleW),
(int) (playButtonUpFixed.getHeight() * drawScaleH), true);
playButtonDown = Bitmap.createScaledBitmap(playButtonDownFixed,
(int) (playButtonDownFixed.getWidth() * drawScaleW),
(int) (playButtonDownFixed.getHeight() * drawScaleH), true);
Edit:
I am building on an example found in the book "Android Game Programming For Dummies" as this is the first App/Game I have coded for android. For his game title/menu pages the author does not use XML layouts.
This is a link to the code I use currently:
Second Edit:
The following are the details obtained from logs, the following text is laid out so that on the left is the smaller of the pictured devices above and on the right is the bigger.
Screen W: 320, 800 Screen H: 240, 480
Scale W: 0.4, 1.0 Scale H: 0.4, 0.8
(So far all the maths adds up/ is correct, however here is were it goes crazy
The Actual Button Size of the button image: W220, H67
Calculated button size: W: 66, 330 H: 20, 80
Clearly this is wrong as, for example of the small screen, H67 * 0.4 does not equal 20. So I went to see what the width and height of the button once they are imported are and found:
W: 165, 330 H: 50, 101
This means that for the small screen, once the Bitmap is created (But NOT rescaled, it is automatically being rescaled by 0.75 of the actual button image size). For the bigger screen it is being resized by 1.5 at the point in which it is being created. I cannot for a love of me work out why though!