显示png时隐藏1px边距



我正在接收来自服务器的九个补丁图像,以便在我的android应用程序的ui中显示。这是一个很难的要求,因此我无法将九个补丁编译成APK。Android似乎会识别并显示任何PNG为9个补丁,这意味着图像的拉伸遵循外部像素所指示的轮廓。问题是Android仍然显示文件的1px边界。因此,我尝试将视图上的填充设置为-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" />

我如何设置这个布局不显示1px的边界周围的PNG?我可以做到这一点,而不重写视图类和手动摆弄像素?

<

解决方案/strong>

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

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);
}

使用NinePatchDrawable来解析Bitmap,使其正确显示

最新更新