2

我正在从服务器接收九个补丁图像以显示在我的 android 应用程序的 ui 中。这是一个硬性要求,所以我无法将九个补丁编译到 APK 中。Android 似乎会将任何 PNG 识别并显示为九个补丁,这意味着图像的拉伸遵循外部像素指示的轮廓。问题是Android仍然显示文件的1px边界。因此,我尝试将 View 上的填充设置为 -1,以便不显示该行。这似乎不起作用,因为黑线仍在显示。

    <ImageView
        android:id="@+id/stub_image"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="centerCrop"
        android:padding="-1px"
        android:cropToPadding="true"
        android:src="@drawable/stub_image"
        android:contentDescription="@string/image_title" />

如何将此布局设置为不显示 PNG 周围的 1px 边界?我可以在不覆盖 View 类并手动处理像素的情况下做到这一点吗?

解决方案

在@alex 链接到的帖子上的第一个选项之后,我在一个空的解决方案中编译了 Ninepatch 文件。然后将这些从我的服务器提供给应用程序。由于 png 可能是由服务器发送的,还有九个补丁,我用来获取 drawable 的代码如下:

InputStream stream = .. //whatever
Bitmap bitmap = BitmapFactory.decodeStream(stream);
byte[] chunk = bitmap.getNinePatchChunk();
if (NinePatch.isNinePatchChunk(chunk)) {
    return new NinePatchDrawable(getResources(), bitmap, chunk, new Rect(), null);
} else {
    return new BitmapDrawable(getResources(), bitmap);
}
4

1 回答 1

1

使用 aNinePatchDrawable解析,Bitmap使其正确显示。

于 2013-04-30T01:22:27.747 回答