2

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:

enter image description here

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:

MainActivity

TitleView

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!

Pink Teddy
  • 23
  • 3

2 Answers2

0

To me, this problem is related to Auto Scale TextView Text to Fit within Bounds, Android Buttons not scaling, and Scale text in a view to fit?.

You may find the solutions in those questions suitable for your case, but I had a similar problem and I found that replacing my Button objects with ImageButton objects and producing images to match solved it in my case.

Follow the size ratios mentioned in Supporting Multiple Screens for your icons: 0.75x : 1 : 1.5x : 2x.

Community
  • 1
  • 1
andy256
  • 2,821
  • 2
  • 13
  • 19
  • Thank you for the reply and despite the answer being beneficial reading it does not (directly) answer the question. The solution to replace my "Button objects with ImageButton objects" could work however this problem in terms of rescaling exists not just for the buttons but all images in general. – Pink Teddy Sep 25 '13 at 17:15
  • Apparently I misunderstood the problem. Can you post your layout XML? – andy256 Sep 25 '13 at 21:58
  • Sorry it is most likely myself who is the problem. I have updated the post with more information if you would not mind taking a look. Thanks. – Pink Teddy Sep 26 '13 at 23:10
  • I have not had such a problem, so I'm learning here too. It looks as though scale factors are wrong, somehow. What is the output from the two sets of `Log.d(..."Screen Width: "` and Height lines? (Suggest you change the second pair to say "drawScaleW" etc). And what happens in `onDraw` if you put `canvas.setMatrix (null);` after `canvas.drawBitmap(titleGraphic, 0, 0, null);`? – andy256 Sep 27 '13 at 01:23
  • canvas.setMatrix (null); did nothing. I have edited the post with all relevant math obtained from logs. – Pink Teddy Sep 27 '13 at 22:32
0

I (think) I have found the answer:

I don't want Android to resize my bitmap Automatically

Basically the little screen falls under ldpi while the other screen (pictured) is either a mdpi or a hdpi. Apparently the drawable folder treats images the same as mdpi so this is why it is over-scaling for the larger screen and under-scaling the small screen.

Community
  • 1
  • 1
Pink Teddy
  • 23
  • 3