开发者

Scaling ImageButtons for multiple resolutions

开发者 https://www.devze.com 2023-01-19 02:50 出处:网络
I have a button which I want to occupy 75% of the screen: On a 480x800 开发者_开发百科resolution screen this would be 360 pixels wide.

I have a button which I want to occupy 75% of the screen:

Scaling ImageButtons for multiple resolutions

On a 480x800 开发者_开发百科resolution screen this would be 360 pixels wide.

On a 280x320 resolution screen this would be 210 pixels wide.

How do I go about specifying this in my layout XML file?

I understand there is a DIP unit of measurement, but does that also work to scale screen images for resolutions? What DIP measurement would I use for this, and do the images need to be saved at 160dpi.


You should really try to avoid making this complicate by thinking about all the different screen sizes, and densities and such.

You say you want your button to occupy 75% of the screen.

Okay, put it in a LinearLayout somewhat like this:

<LinearLayout android:layout_width="match_parent"
        android:layout_height="match_parent
        android:orientation="horizontal">

    <My75PercentButton
            android:layout_width="0px"
            android:layout_height="match_parent"
            android:layout_weight="75">

    <View
            android:layout_width="0px"
            android:layout_height="match_parent"
            android:layout_weight="25">

</LinearLayout>

This makes the two views have 0 width, but spreads 75% of the available space for them after that into the first, and 25% into the second.

If you want to make your button stretch to fill that space (instead of just leaving empty padding around it), make it a 9-patch.

You will also want to support different screen densities, so provide different image sizes for your button in drawable-ldpi, drawable-mdpi, and drawable-hdpi.

But I will really caution: trying to push yourself into a world where you are designing your graphics for various specific screen sizes is going to end up in a huge world of hurt. You really don't want to do that. There are already a lot of screen sizes Android devices are running; this is only going to increase greatly in the future. If you take advantage of layout managers and other such facilities to adjust for the actual screen size, your app will run well on all of these screens with little to no work on your part. If you try to design your graphic for specific screen sizes, you are going to be doing this over and over and over again until you can't stand it.


You can create different layouts for different screen densities by defining the layouts in their appropriate subdirectories called layout-small for small density screens, layout-normal for all densities and layout-large for high density screens.

The dip-measurement works for this but I'm not quite sure on what value to set I guess you gotta try it and find out which one fits your needs best.

You can find an in depth article here.

EDIT: I've actually mixed them up ... sorry for the confusion. Corrected my answer above.

@HardQuestion: Here are the supported media formats for Android.

0

精彩评论

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