1

我的资源中有一个可绘制的九个补丁。我必须在运行时更改它的一些颜色,方法是将 NinePatchDrawable 保存为位图并更改像素,然后再次将此位图保存为 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)

我究竟做错了什么?

4

1 回答 1

2

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

所以而不是:

byte[] chunk = bitmap.getNinePatchChunk();

应该:

byte[] chunk = src.getNinePatchChunk();
于 2013-07-24T08:20:00.970 回答