NinePatchDrawable - >位图 - > NinePatchDrawable

时间:2013-07-24 07:39:35

标签: android nine-patch

我的资源中有九个补丁。我必须在运行时更改一些颜色,方法是将NinePatchDrawable保存为Bitmap并更改像素,然后再将此Bitmap保存为NinePatchDrawable。

我的代码:

Bitmap src = BitmapFactory.decodeResource(res, resId);
Bitmap bitmap = src.copy(Bitmap.Config.ARGB_8888, true);
//... pixels operation

byte[] chunk = bitmap.getNinePatchChunk();
return new NinePatchDrawable(res, bitmap, chunk, new Rect(), null);

该应用程序崩溃'new NinePatchDrawable',其中包括:

"Signal Catcher" daemon prio=5 tid=4 RUNNABLE
   | group="system" sCount=0 dsCount=0 obj=0x40517900 self=0xe89f0
   | sysTid=21817 nice=0 sched=0/0 cgrp=[no-cpu-subsys] handle=473072
   | schedstat=( 1007082 4364014 23 )
   at dalvik.system.NativeStart.run(Native Method)
 [<c0294308>] save_stack_trace_tsk+0x0/0x90
 [<c03b5568>] proc_pid_stack+0xf8/0x144
 [<c03b68cc>] proc_single_show+0x48/0x84
 [<c038aef8>] seq_read+0x26c/0x4e4
 [<c0371ab8>] vfs_read+0xa8/0x150
 [<c0371c0c>] sys_read+0x3c/0x68
 [<c0290740>] ret_fast_syscall+0x0/0x30
    [<ffffffff>] 0xffffffff
    ------------------------------
"main" prio=5 tid=1 RUNNABLE
   | group="main" sCount=1 dsCount=0 obj=0x400281f8     self=0xd190
   | sysTid=21814 nice=0 sched=0/0 cgrp=[no-cpu-subsys]     handle=-1345002240
   | schedstat=( 1833007809 896209717 2404 )
   at     android.graphics.NinePatch.validateNinePatchChunk(Native Method)
   at android.graphics.NinePatch.<init>(NinePatch.java:50)
   at android.graphics.drawable.NinePatchDrawable.<init>(NinePatchDrawable.java:73)

我做错了什么?

1 个答案:

答案 0 :(得分:1)

当我使用源位图中的九个补丁块而不是新的位图时,它起作用了:

所以而不是:

byte[] chunk = bitmap.getNinePatchChunk();

应该是:

byte[] chunk = src.getNinePatchChunk();