开发者

Android: how to draw an ImageView with the same size regardless of device?

开发者 https://www.devze.com 2023-03-05 23:15 出处:网络
I want to put a toolbar in my application.The toolbar will use ImageViews as application buttons.I would like the ImageViews to be the exact same size regardless device; by \"sam开发者_运维百科e size\

I want to put a toolbar in my application. The toolbar will use ImageViews as application buttons. I would like the ImageViews to be the exact same size regardless device; by "sam开发者_运维百科e size" I mean that, when rendered to the screen, if I measure them with a ruler, the dimensions will be the same. I would like this size to be ~10mm (the width of my index finger).

I have been completely unsuccessful in accomplishing this.

I am testing on a MDPI, large screen Acer Iconia Tab and a HDPI medium screen Samsung Galaxy Tab. If I set layout_width="50dip" and layout_height="50dip", the buttons render as ~10mm on the Samsung, and ~8mm on the Acer. If I set the buttons as layout_width="12mm" and layout_height="12mm", they render as 12mm on the Acer and ~9mm on the Galaxy (confusing that 50dip renders bigger on the Galaxy than the Acer, but 12 mm renders smaller on the Galaxy than on the Acer).

If I place a 32pix x 32pix icon in my drawable-mdpi folder (and no equivalent in the drawable-hdpi folder), and set layout_width="wrap_content" and layout_height="wrap_content"; the results are similar to if set to 50dip, the Samsung is about 10mm, the Acer about 8mm. If I add a 48pix x 48pix icon to my drawable-hdpi folder, there is no change; presumably because the Acer still uses the mdpi icon and the Samsung uses the bigger one, but scales it down by 50% because it knows its hdpi (if am confident that this surprising scaling occurs because if I move the 48pix icon to the mdpi folder, and have nothing in the hdpi folder, the icon shows very large on the Samsung).

I am confused and could really use advice. How do I make my button fingertip sized regardless of dpi and screen size?


Ok, I finally figured out what is going on:

The problem is that a image button who's width is specified as 10mm, shows as about 7mm on my Galaxy tab, and about 9mm on my Iconia Tab. There are 2 reasons for this rather significant difference:

  1. The Iconia Tab reports its xdpi as ~160, when its actual xdpi is ~150.
  2. Honeycomb uses a new drawable for its button background, with significantly smaller margin than the drawable in Froyo.

Issue 1 accounts for about .6mm of the difference, Issue 2 accounts for the remainder. Issue 2 can be solved by choosing one of the two backgrounds, packaging it with the application, and specifying it as the background for the ImageButton. Issue 1 cannot be resolved, but the difference it creates is relatively minor once issue 2 is dealt with.


You have two basic approaches:

  1. Use supplied resource indentifiers - hdpi/ldpi/etc - and just rely on the OS.

  2. Use xdpi/ydpi in DisplayMetrics class. They are supposed to return the exact physical pixels per inch and scale the image manually with a corresponding scale factor. This, of course, will slow down performance. What is more - I am not sure whether these readings will be correct.


Did you try stuff in the no-dpi resource identifier. Honestly I'm not sure what you would get out of this but it's worth a shot.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号