我在尝试基于自定义 bade png 文件创建自己的 Android 可绘制对象时遇到了大问题。该问题与我几天前发布的上一个问题类似(尚未解决:使用定制的 png 文件在 Android 的图像视图中渲染错误)。
我有以下定制的可绘制对象:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<corners android:radius="5dp" />
<solid android:color="#c07313" />
</shape>
</item>
<item
android:drawable="@drawable/coal_icon"
android:width="29dp"
android:height="29dp"
android:right="1dp"
android:left="1dp"
android:bottom="1dp"
android:top
="1dp"
android:gravity="left|center_vertical" />
</layer-list>
使用定制的 png 文件“coal_icon”
我用 Inscape 制作了一个矢量图形文件,并将其保存到我的 Android 应用程序的可绘制文件夹中。但是,当应使用自定义可绘制对象作为背景的 ImageView 应在我的应用程序中显示时,我收到一个非特定的 OpernGLRender 错误,并且应用程序崩溃(这里只是其中的一部分,因为消息很长):
023-08-25 11:31:57.801 12546-12571 OpenGLRenderer com.example.game E GL error: GL_INVALID_OPERATION
2023-08-25 11:31:57.801 12546-12571 OpenGLRenderer com.example.game A GL errors! frameworks/base/libs/hwui/pipeline/skia/SkiaOpenGLPipeline.cpp:127
2023-08-25 11:31:57.855 12546-12571 om.example.gam com.example.game A runtime.cc:655] Runtime aborting...
runtime.cc:655] Dumping all threads without mutator lock held
runtime.cc:655] All threads:
runtime.cc:655] DALVIK THREADS (14):
runtime.cc:655] "RenderThread" prio=10 tid=18 Runnable
runtime.cc:655] | group="" sCount=0 dsCount=0 flags=0 obj=0x13340718 self=0xe4a91810
runtime.cc:655] | sysTid=12571 nice=-10 cgrp=top-app sched=0/0 handle=0xc33251e0
runtime.cc:655] | state=R schedstat=( 8290970061 745199188 7990 ) utm=33 stm=795 core=1 HZ=100
runtime.cc:655] | stack=0xc322a000-0xc322c000 stackSize=1008KB
runtime.cc:655] | held mutexes= "abort lock" "mutator lock"(shared held)
runtime.cc:655] native: #00 pc 00542d9e /apex/com.android.art/lib/libart.so (art::DumpNativeStack(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, int, BacktraceMap*, char const*, art::ArtMethod*, void*, bool)+110)
runtime.cc:655] native: #01 pc 006a0897 /apex/com.android.art/lib/libart.so (art::Thread::DumpStack(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, bool, BacktraceMap*, bool) const+1015)
runtime.cc:655] native: #02 pc 0069a171 /apex/com.android.art/lib/libart.so (art::Thread::Dump(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, bool, BacktraceMap*, bool) const+65)
runtime.cc:655] native: #03 pc 006c61b4 /apex/com.android.art/lib/libart.so (art::DumpCheckpoint::Run(art::Thread*)+1172)
runtime.cc:655] native: #04 pc 006bf266 /apex/com.android.art/lib/libart.so (art::ThreadList::RunCheckpoint(art::Closure*, art::Closure*)+630)
runtime.cc:655] native: #05 pc 006be1ce /apex/com.android.art/lib/libart.so (art::ThreadList::Dump(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, bool)+2446)
runtime.cc:655] native: #06 pc 0065227d /apex/com.android.art/lib/libart.so (art::AbortState::DumpAllThreads(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, art::Thread*) const+445)
runtime.cc:655] native: #07 pc 0063984f /apex/com.android.art/lib/libart.so (art::Runtime::Abort(char const*)+1967)
runtime.cc:655] native: #08 pc 00025a23 /apex/com.android.art/lib/libartbase.so (std::__1::__function::__func<void (*)(char const*), std::__1::allocator<void (*)(char const*)>, void (char const*)>::operator()(char const*&&)+35)
runtime.cc:655] native: #09 pc 0001588f /system/lib/libbase.so (android::base::SetAborter(std::__1::function<void (char const*)>&&)::$_3::__invoke(char const*)+79)
runtime.cc:655] native: #10 pc 00006dbd /system/lib/liblog.so (__android_log_assert+285)
runtime.cc:655] native: #11 pc 0024dd11 /system/lib/libhwui.so (android::uirenderer::skiapipeline::SkiaOpenGLPipeline::swapBuffers(android::uirenderer::renderthread::Frame const&, bool, SkRect const&, android::uirenderer::FrameInfo*, bool*)+145)
runtime.cc:655] native: #12 pc 00257e78 /system/lib/libhwui.so (android::uirenderer::renderthread::CanvasContext::draw()+1112)
runtime.cc:655] native: #13 pc 0025ab5f /system/lib/libhwui.so (_ZNSt3__110__function6__funcIZN7android10uirenderer12renderthread13DrawFrameTask11postAndWaitEvE3$_0NS_9allocatorIS6_EEFvvEEclEv$c303f2d2360db58ed70a2d0ac7ed911b+767)
runtime.cc:655] native: #14 pc 0024542d /system/lib/libhwui.so (android::uirenderer::WorkQueue::process()+285)
runtime.cc:655] native: #15 pc 0026cabf /system/lib/libhwui.so (android::uirenderer::renderthread::RenderThread::threadLoop()+127)
runtime.cc:655] native: #16 pc 00015116 /system/lib/libutils.so (android::Thread::_threadLoop(void*)+374)
runtime.cc:655] native: #17 pc 000147d9 /system/lib/libutils.so (thread_data_t::trampoline(thread_data_t const*)+457)
runtime.cc:655] native: #18 pc 000e6974 /apex/com.android.runtime/lib/bionic/libc.so (__pthread_start(void*)+100)
runtime.cc:655] native: #19 pc 00078567 /apex/com.android.runtime/lib/bionic/libc.so (__start_thread+71)
runtime.cc:655] (no managed stack frames)
runtime.cc:655]
runtime.cc:655] "main" prio=10 tid=1 Native
2023-08-25 11:31:57.855 12546-12571 om.example.gam com.example.game A runtime.cc:655] | group="" sCount=1 dsCount=0 flags=1 obj=0x71401c28 self=0xe4a80e10
runtime.cc:655] | sysTid=12546 nice=-10 cgrp=top-app sched=0/0 handle=0xf2ed1478
runtime.cc:655] | state=S schedstat=( 2688356931 656401896 5291 ) utm=144 stm=124 core=0 HZ=100
runtime.cc:655] | stack=0xff484000-0xff486000 stackSize=8192KB
runtime.cc:655] | held mutexes=
runtime.cc:655] native: #00 pc 00000b97 [vdso] (__kernel_vsyscall+7)
runtime.cc:655] native: #01 pc 0005ad68 /apex/com.android.runtime/lib/bionic/libc.so (syscall+40)
runtime.cc:655] native: #02 pc 0007725e /apex/com.android.runtime/lib/bionic/libc.so (__futex_wait_ex(void volatile*, bool, int, bool, timespec const*)+142)
runtime.cc:655] native: #03 pc 000e5b2e /apex/com.android.runtime/lib/bionic/libc.so (pthread_cond_wait+62)
runtime.cc:655] native: #04 pc 0025a146 /system/lib/libhwui.so (android::uirenderer::renderthread::DrawFrameTask::postAndWait()+182)
runtime.cc:655] native: #05 pc 0023922d /system/lib/libhwui.so (android::android_view_ThreadedRenderer_syncAndDrawFrame(_JNIEnv*, _jobject*, long long, _jlongArray*, int)+109)
runtime.cc:655] at android.graphics.HardwareRenderer.nSyncAndDrawFrame(Native method)
runtime.cc:655] at android.graphics.HardwareRenderer.syncAndDrawFrame(HardwareRenderer.java:433)
runtime.cc:655] at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:658)
runtime.cc:655] at android.view.ViewRootImpl.draw(ViewRootImpl.java:4101)
runtime.cc:655] at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:3828)
runtime.cc:655] at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3099)
runtime.cc:655] at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1952)
runtime.cc:655] at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8171)
runtime.cc:655] at android.view.Choreographer$CallbackRecord.run(Choreographer.java:972)
runtime.cc:655] at android.view.Choreographer.doCallbacks(Choreographer.java:796)
runtime.cc:655] at android.view.Choreographer.doFrame(Choreographer.java:731)
runtime.cc:655] at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:957)
runtime.cc:655] at android.os.Handler.handleCallback(Handler.java:938)
runtime.cc:655] at android.os.Handler.dispatchMessage(Handler.java:99)
runtime.cc:655] at android.os.Looper.loop(Looper.java:223)
runtime.cc:655] at android.app.ActivityThread.main(ActivityThread.java:7656)
runtime.cc:655] at java.lang.reflect.Method.invoke(Native method)
runtime.cc:655] at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
runtime.cc:655] at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
runtime.cc:655]
runtime.cc:655] "Signal Catcher" prio=10 tid=6 WaitingInMainSignalCatcherLoop
runtime.cc:655] | group="" sCount=1 dsCount=0 flags=1 obj=0x13340268 self=0xe4a80010
runtime.cc:655] | sysTid=12556 nice=-20 cgrp=top-app sched=0/0 handle=0xd9cd21e0
runtime.cc:655] | state=S schedstat=( 688585 0 1 ) utm=0 stm=0 core=0 HZ=100
runtime.cc:655] | stack=0xd9bd7000-0xd9bd9000 stackSize=1008KB
runtime.cc:655] | held mutexes=
runtime.cc:655] native: #00 pc 00000b99 [vdso] (__kernel_vsyscall+9)
runtime.cc:655] native: #01 pc 000ce821 /apex/com.android.runtime/lib/bionic/libc.so (__rt_sigtimedwait+33)
runtime.cc:655] native: #02 pc 00086c55 /apex/com.android.runtime/lib/bionic/libc.so (sigwait+69)
runtime.cc:655] native: #03 pc 00665f1d /apex/com.android.art/lib/libart.so (art::SignalCatcher::WaitForSignal(art::Thread*, art::SignalSet&)+461)
runtime.cc:655] native: #04 pc 006648ff /apex/com.android.art/lib/libart.so (art::SignalCatcher::Run(void*)+479)
runtime.cc:655] native: #05 pc 000e6974 /apex/com.android.runtime/lib/bionic/libc.so (__pthread_start(void*)+100)
runtime.cc:655] native: #06 pc 00078567 /apex/com.android.runtime/lib/bionic/libc.so (__start_thread+71)
runtime.cc:655] (no managed stack frames)
runtime.cc:655]
2023-08-25 11:31:57.855 12546-12571 om.example.gam com.example.game A runtime.cc:655] "perfetto_hprof_listener" prio=10 tid=7 Native (still starting up)
runtime.cc:655] | group="" sCount=1 dsCount=0 flags=1 obj=0x0 self=0xe4a83810
runtime.cc:655] | sysTid=12557 nice=-20 cgrp=top-app sched=0/0 handle=0xd9bd11e0
runtime.cc:655] | state=S schedstat=( 686318 100287 8 ) utm=0 stm=0 core=1 HZ=100
runtime.cc:655] | stack=0xd9ad6000-0xd9ad8000 stackSize=1008KB
runtime.cc:655] | held mutexes=
runtime.cc:655] native: #00 pc 00000b97 [vdso] (__kernel_vsyscall+7)
runtime.cc:655] native: #01 pc 000ccf9c /apex/com.android.runtime/lib/bionic/libc.so (read+28)
runtime.cc:655] native: #02 pc 0001aca2 /apex/com.android.art/lib/libperfetto_hprof.so (void* std::__1::__thread_proxy<std::__1::tuple<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_delete<std::__1::__thread_struct> >, ArtPlugin_Initialize::$_29> >(void*)+306)
runtime.cc:655] native: #03 pc 000e6974 /apex/com.android.runtime/lib/bionic/libc.so (__pthread_start(void*)+100)
runtime.cc:655] native: #04 pc 00078567 /apex/com.android.runtime/lib/bionic/libc.so (__start_thread+71)
runtime.cc:655] (no managed stack frames)
runtime.cc:655]
runtime.cc:655] "ADB-JDWP Connection Control Thread" prio=10 tid=8 WaitingInMainDebuggerLoop
runtime.cc:655] | group="" sCount=1 dsCount=0 flags=1 obj=0x133402e0 self=0xe4a87e10
runtime.cc:655] | sysTid=12558 nice=-20 cgrp=top-app sched=0/0 handle=0xd9ad01e0
runtime.cc:655] | state=S schedstat=( 1594143 693720 6 ) utm=0 stm=0 core=3 HZ=100
runtime.cc:655] | stack=0xd99d5000-0xd99d7000 stackSize=1008KB
runtime.cc:655] | held mutexes=
runtime.cc:655] native: #00 pc 00000b99 [vdso] (__kernel_vsyscall+9)
runtime.cc:655] native: #01 pc 000cf496 /apex/com.android.runtime/lib/bionic/libc.so (__ppoll+38)
runtime.cc:655] native: #02 pc 00083979 /apex/com.android.runtime/lib/bionic/libc.so (poll+105)
runtime.cc:655] native: #03 pc 0000a493 /apex/com.android.art/lib/libadbconnection.so (adbconnection::AdbConnectionState::RunPollLoop(art::Thread*)+1171)
runtime.cc:655] native: #04 pc 000086d2 /apex/com.android.art/lib/libadbconnection.so (adbconnection::CallbackFunction(void*)+1666)
runtime.cc:655] native: #05 pc 000e6974 /apex/com.android.runtime/lib/bionic/libc.so (__pthread_start(void*)+100)
runtime.cc:655] native: #06 pc 00078567 /apex/com.android.runtime/lib/bionic/libc.so (__start_thread+71)
runtime.cc:655] (no managed stack frames)
runtime.cc:655]
runtime.cc:655] "Jit thread pool worker thread 0" prio=5 tid=9 Native
runtime.cc:655] | group="" sCount=1 dsCount=0 flags=1 obj=0x13340358 self=0xe4a8d210
runtime.cc:655] | sysTid=12559 nice=0 cgrp=top-app sched=0/0 handle=0xc4d74d60
runtime.cc:655] | state=S schedstat=( 933177212 340822158 1157 ) utm=32 stm=60 core=0 HZ=100
runtime.cc:655] | stack=0xc4c76000-0xc4c78000 stackSize=1023KB
runtime.cc:655] | held mutexes=
runtime.cc:655] native: #00 pc 00000b97 [vdso] (__kernel_vsyscall+7)
runtime.cc:655] native: #01 pc 0005ad68 /apex/com.android.runtime/lib/bionic/libc.so (syscall+40)
runtime.cc:655] native: #02 pc 001d82ec /apex/com.android.art/lib/libart.so (art::ConditionVariable::WaitHoldingLocks(art::Thread*)+108)
runtime.cc:655] native: #03 pc 001d8273 /apex/com.android.art/lib/libart.so (art::ConditionVariable::Wait(art::Thread*)+35)
runtime.cc:655] native: #04 pc 006c838f /apex/com.android.art/lib/libart.so (art::ThreadPool::GetTask(art::Thread*)+143)
runtime.cc:655] native: #05 pc 006c73e5 /apex/com.android.art/lib/libart.so (art::ThreadPoolWorker::Run()+133)
runtime.cc:655] native: #06 pc 006c6e9d /apex/com.android.art/lib/libart.so (art::ThreadPoolWorker::Callback(void*)+269)
runtime.cc:655] native: #07 pc 000e6974 /apex/com.android.runtime/lib/bionic/libc.so (__pthread_start(void*)+100)
runtime.cc:655] native: #08 pc 00078567 /apex/com.android.runtime/lib/bionic/libc.so (__start_thread+71)
runtime.cc:655] (no managed stack frames)
runtime.cc:655]
后端的 Java 代码本身是正确的,因为当我在定制的可绘制对象中仅使用 Android Studio 内部矢量资源时,根本没有收到此错误(测试了 1000 次)
android:drawable="@drawable/vector_asset_from_android_studio"
有人可以解释为什么我在使用带有自定义 png 文件的自定义可绘制对象时出现此错误吗?
将此文件放在可绘制的一侧 - Coal_mine.xml
<vector android:height="80dp" android:viewportHeight="512"
android:viewportWidth="512" android:width="80dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#000000" android:pathData="M365.91,60.84C347.22,62.03 327.73,73.3 307.72,87.5c-20.81,14.77 -41.85,32.27 -64.63,41.88a8,8 0,0 1,-9.13 -2.13c-15.01,-17.31 -38.15,-30.55 -73.22,-27.28 -19.12,1.78 -32.25,10.63 -43.69,23.66 -5.26,6 -10,12.98 -14.53,20.38h322.62c-15.21,-29.85 -29.05,-64.18 -59.25,-83.16zM40,160v44h416v-44L40,160zM58.38,220l49.34,186.56L118,406.56c3.09,-6.59 7.31,-12.68 12.56,-17.94 11.92,-11.92 28.63,-18.91 45.84,-18.91 17.21,0 33.9,6.99 45.81,18.91 5.25,5.25 9.47,11.35 12.56,17.94h42.97c3.1,-6.47 7.28,-12.44 12.41,-17.56 11.93,-11.93 28.38,-18.75 45.25,-18.75 16.87,0 33.32,6.82 45.25,18.75 5.12,5.12 9.3,11.09 12.41,17.56h6.03L438.16,220L58.38,220zM426.22,355l-6.06,29L456,384v-29h-29.78zM176.41,386.56c-11.69,0 -23.73,4.91 -32.25,13.44 -8.52,8.52 -13.75,21 -13.75,33.34 0,12.34 5.23,24.82 13.75,33.34 8.52,8.52 20.56,13.44 32.25,13.44 11.69,0 23.7,-4.91 32.22,-13.44 8.52,-8.52 13.78,-21 13.78,-33.34 0,-12.34 -5.26,-24.82 -13.78,-33.34 -8.52,-8.52 -20.53,-13.44 -32.22,-13.44zM335.41,387.84c-12.04,0 -24.3,5.08 -32.81,13.59C294.08,409.95 289,422.21 289,434.25c0,12.04 5.08,24.3 13.59,32.81 8.51,8.51 20.77,13.59 32.81,13.59 12.04,0 24.3,-5.08 32.81,-13.59 8.51,-8.51 13.59,-20.77 13.59,-32.81 0,-12.04 -5.08,-24.3 -13.59,-32.81 -8.51,-8.51 -20.78,-13.59 -32.81,-13.59z"/></vector>
转到浏览器 - 搜索免费的 SVG 图标,然后下载,然后打开 android studio - 选择可绘制对象并右键单击 - 新建 - 矢量资源 - 打开弹出窗口,然后选择 SVG 单选按钮并提供文件路径并完成。然后到处使用。 随时更改尺寸。