开发者

How to display icons without alteration in an ImageButton/ImageView?

开发者 https://www.devze.com 2023-02-21 23:57 出处:网络
in my app, I have buttons with icons. The icons are provided as PNG images in the three densities in drawable-ldpi-v4/, drawable/ and drawable-hdpi-v4/. Here\'s a sample:

in my app, I have buttons with icons. The icons are provided as PNG images in the three densities in drawable-ldpi-v4/, drawable/ and drawable-hdpi-v4/. Here's a sample:

How to display icons without alteration in an ImageButton/ImageView?

Each icon is displayed in an ImageButton:

<ImageButton style="@style/Shortcut" android:id="@+id/open_button"
        android:src="@drawable/shortcut_open" android:layout_marginRight="4dp"/>

Where the Shortcut style is:

<style name="Shortcut">
    <item name="android:layout_width">65dp</item>
    <item name="android:layout_height">45dp</item>
    <item name="android:scaleType">center</item>
    <item name="android:background">@drawable/shortcut_background</item>
</style>

However, on certain devices/platform versions, certain icons get altered, blurred or something. I'm not sure that it's scaling, it's more like a rendering bug I think. I have tried to disable anti aliasing on the BitmapDrawable, but that didn't help.

As shown on the image below, on Android 2.1 LDPI, one icon gets broken/cropped, and on both Android 1.6 MDPI and 开发者_如何学编程Android 2.1 HDPI an extra line seems to be randomly added at the bottom of the icon (look closely).

How to display icons without alteration in an ImageButton/ImageView?

In the manifest, I have an empty <supports-screens /> as recommended in the docs about supporting multiple screens in legacy apps. Adding anyDensity="true" doesn't help.

Apparently, as of Froyo, things get better as you can see on the image above. But how can I solve this on Android <= 2.1?


I think you should not use explicit sizes (even when expressed as dip) for the buttons, but let the system do it for you.

Use

    <ImageButton android:id="@+id/SpeakButton"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"

Also it may be that you are falling into the "dip" vs. "dp" trap that others have reported, where documentation says that they are synonymous, but using "dip" made stuff work as intended.


Not sure if this is the case but you can check if you are not running into the bugs mentioned in this Google I/O talk. I still have to wrap my head around it but I think it's worth having a look if you are targeting platforms from 1.5 to 2.0.

0

精彩评论

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

关注公众号