在运行时创建 NinePatch

发布于 2024-10-09 10:45:27 字数 222 浏览 3 评论 0原文

我有一个在运行时创建 NinePatchDrawable 对象的业务需求,即从服务器接收外部 .png 图像,并且必须将其作为九个补丁应用到按钮的背景(例如)中。

我尝试创建 NinePatchDrawable 对象,但构造函数要求我提供描述补丁的“byte[] chunk”。问题是,我不知道如何从没有 9patch 信息的位图构建这个块。

关于这个话题有什么想法吗?我是否从错误的角度看待问题?

I have a business need to create the NinePatchDrawable objects at runtime, this is, an exterior .png image is received from a server and it has to be applied in a button's background (for example) as a nine patch.

I have tried to create the NinePatchDrawable object, but the constructor asks me for a "byte[] chunck" that describes the patch. The thing is, I have no idea on how to build this chunk from a bitmap that does not have the 9patch information in it.

Any ideas on this topic? Am I seeing the problem from a wrong perspective?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撕心裂肺的伤痛 2024-10-16 10:45:27

请参阅我的答案 在运行时创建 NinePatch/NinePatchDrawable

我开发了一个工具来从(未编译的)NinePatch 位图创建 NinePatchDrawable。
请参阅 https://gist.github.com/knight9999/86bec38071a9e0a781ee

方法
NinePatchDrawable createNinePatchDrawable(Resources res, Bitmap bitmap)
对你有帮助。

例如,

    ImageView imageView = (ImageView) findViewById(R.id.imageview);
    Bitmap bitmap = loadBitmapAsset("my_nine_patch_image.9.png", this);
    NinePatchDrawable drawable = NinePatchBitmapFactory.createNinePatchDrawable(getResources(), bitmap);
    imageView.setBackground( drawable );

public static final Bitmap loadBitmapAsset(String fileName,Context context) {
    final AssetManager assetManager = context.getAssets();
    BufferedInputStream bis = null;
    try {
        bis = new BufferedInputStream(assetManager.open(fileName));
        return BitmapFactory.decodeStream(bis);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            bis.close();
        } catch (Exception e) {

        }
    }
    return null;
}

本例中,my_nine_patch_image.9.png 位于 asset 目录下。

See my answer for Create a NinePatch/NinePatchDrawable in runtime

I develop a tool to create NinePatchDrawable from (uncompiled) NinePatch bitmap.
See https://gist.github.com/knight9999/86bec38071a9e0a781ee .

The method
NinePatchDrawable createNinePatchDrawable(Resources res, Bitmap bitmap)
helps you.

For example,

    ImageView imageView = (ImageView) findViewById(R.id.imageview);
    Bitmap bitmap = loadBitmapAsset("my_nine_patch_image.9.png", this);
    NinePatchDrawable drawable = NinePatchBitmapFactory.createNinePatchDrawable(getResources(), bitmap);
    imageView.setBackground( drawable );

where

public static final Bitmap loadBitmapAsset(String fileName,Context context) {
    final AssetManager assetManager = context.getAssets();
    BufferedInputStream bis = null;
    try {
        bis = new BufferedInputStream(assetManager.open(fileName));
        return BitmapFactory.decodeStream(bis);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            bis.close();
        } catch (Exception e) {

        }
    }
    return null;
}

In this case, the my_nine_patch_image.9.png is under the assets directory.

鲜血染红嫁衣 2024-10-16 10:45:27

我已经在问题5519768回答了这个问题。请记住“源”和“编译”ninepatch 图像之间的差异。

I've answered this over at question 5519768. Keep in mind the differences between "source" and "compiled" ninepatch images.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文