我在Android应用中使用以下代码:
InsetDrawable id = new InsetDrawable(context.getDrawable(drawableId), 150, 200, 150, 400);
imageView.setImageDrawable(id);
它在较新的Android API级别上可以正常工作,但是在API 21(我要支持的最低级别)上会发生这种情况,并且应用程序崩溃:
08-02 09:39:26.657 30728-30728/com.example.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.app, PID: 30728
java.lang.IllegalArgumentException: width and height must be > 0
at android.graphics.Bitmap.createBitmap(Bitmap.java:933)
at android.graphics.Bitmap.createBitmap(Bitmap.java:912)
at android.graphics.Bitmap.createBitmap(Bitmap.java:879)
at android.graphics.drawable.VectorDrawable$VectorDrawableState.createCachedBitmapIfNeeded(VectorDrawable.java:711)
at android.graphics.drawable.VectorDrawable.draw(VectorDrawable.java:278)
at android.graphics.drawable.InsetDrawable.draw(InsetDrawable.java:220)
at android.widget.ImageView.onDraw(ImageView.java:1187)
at android.view.View.draw(View.java:16219)
at android.view.View.updateDisplayListIfDirty(View.java:15139)
at android.view.View.getDisplayList(View.java:15162)
at android.view.View.draw(View.java:15940)
at android.view.ViewGroup.drawChild(ViewGroup.java:3703)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3497)
at android.view.View.updateDisplayListIfDirty(View.java:15134)
at android.view.View.getDisplayList(View.java:15162)
at android.view.View.draw(View.java:15940)
at android.view.ViewGroup.drawChild(ViewGroup.java:3703)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3497)
at android.view.View.updateDisplayListIfDirty(View.java:15134)
at android.view.View.getDisplayList(View.java:15162)
at android.view.View.draw(View.java:15940)
at android.view.ViewGroup.drawChild(ViewGroup.java:3703)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3497)
at android.view.View.draw(View.java:16222)
at android.widget.FrameLayout.draw(FrameLayout.java:592)
at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2932)
at android.view.View.updateDisplayListIfDirty(View.java:15139)
at android.view.View.getDisplayList(View.java:15162)
at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:275)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:281)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:320)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2751)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2584)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2176)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1191)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6642)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
at android.view.Choreographer.doCallbacks(Choreographer.java:590)
at android.view.Choreographer.doFrame(Choreographer.java:560)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5951)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
似乎是Android支持库中的错误或此特定API级别中的错误,如此处所述:IllegalArgumentException: width and height must be > 0 in activity
是否有InsetDrawable
的替代方法或API 21的某些解决方法?