简体   繁体   中英

How to size images to render the same proportion in Android

I have a design concept that I need to create. It was designed in a given resolution: 480x800. It contains a box with the width of 390px that has to be horizontally aligned in the middle. So there are 45 pixels left in both sides.

When I create the layout in Android, I have to give the box width in dp. So I use a dp->px calculator and check what numbers it shows. It tells me, I have to set the width 252dp to make the box be 390px wide in a hdpi, 480x800 phone. When I set it, and starts the emulator, it shows the proportions all right.

 <EditText
  android:id="@+id/txtBox"
  android:layout_width="262dp"
  android:layout_height="37dp"
/>

I change the emulator to a 720x1280, hdpi. What width will the box have? Will it be 252 dp (390px)? If so, the box will take up only half of the screen and my proportion will be off.

How should I set the image width and size to create the right proportions? What will make it dynamic?

Images in android are difficult to deal with. Due to the resolution factor images in one device seems a different in proportions than other devices. The solution is to use appropriate images for appropriate screen density. See iconography on developer site. By providing alternate resources in folder's like drawable-hdpi you ensure that they are rendered properly. Also as a good programming practice use wrap_content or match_parent as our layout_width or height.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM