Android 应用程序在动画渐变背景上崩溃
Android App crashes on animated gradient background
我在我的应用程序中添加了动画渐变背景,但几秒钟后应用程序崩溃了。异常表示 线性渐变需要角度属性 。但是我添加到项目中的每个渐变中都有一个角度属性。有人对我搞砸了什么提出建议吗?
MainActivity.kt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//background animation
val constraintLayout = findViewById<ConstraintLayout>(R.id.layout)
val animationDrawable = constraintLayout.background as AnimationDrawable
animationDrawable.setEnterFadeDuration(2000)
animationDrawable.setExitFadeDuration(4000)
animationDrawable.start()
...
}
activity_main.xml
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout"
android:background="@drawable/gradient_list"
tools:context=".MainActivity">
...
gradient_list.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/gradient_1"
android:duration="5000"/>
<item android:drawable="@drawable/gradient_2"
android:duration="5000"/>
<item android:drawable="@drawable/gradient_3"
android:duration="5000"/>
<item android:drawable="@drawable/gradient_4"
android:duration="5000"/>
</animation-list>
gradient_1.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="225"
android:endColor="#044fab"
android:startColor="#f18127"
android:type="linear"/>
</shape>
gradient_2.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="45"
android:endColor="#43b4ef"
android:startColor="#32ff3f"
android:type="linear"/>
</shape>
gradient_3.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="70"
android:endColor="#f674da"
android:startColor="#38d7bf"
android:type="linear"/>
</shape>
gradient_4.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="48"
android:endColor="#e4033e"
android:startColor="#e6de15"
android:type="linear"/>
</shape>
这是崩溃后的日志:
2021-06-05 23:23:33.409 5892-5892/com.jakobu.memory E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.jakobu.memory, PID: 5892
java.lang.IllegalArgumentException: Linear gradient requires 'angle' attribute to be a multiple of 45
at android.graphics.drawable.GradientDrawable$GradientState.updateGradientStateOrientation(GradientDrawable.java:2208)
at android.graphics.drawable.GradientDrawable$GradientState.getOrientation(GradientDrawable.java:2196)
at android.graphics.drawable.GradientDrawable.ensureValidRect(GradientDrawable.java:1273)
at android.graphics.drawable.GradientDrawable.draw(GradientDrawable.java:717)
at android.graphics.drawable.DrawableContainer.draw(DrawableContainer.java:87)
at android.view.View.getDrawableRenderNode(View.java:21702)
at android.view.View.drawBackground(View.java:21636)
at android.view.View.draw(View.java:21413)
at android.view.View.updateDisplayListIfDirty(View.java:20298)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4372)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4345)
at android.view.View.updateDisplayListIfDirty(View.java:20258)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4372)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4345)
at android.view.View.updateDisplayListIfDirty(View.java:20258)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4372)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4345)
at android.view.View.updateDisplayListIfDirty(View.java:20258)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4372)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4345)
at android.view.View.updateDisplayListIfDirty(View.java:20258)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4372)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4345)
at android.view.View.updateDisplayListIfDirty(View.java:20258)
at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:575)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:581)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:654)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:3610)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:3418)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2755)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1721)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7598)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:966)
at android.view.Choreographer.doCallbacks(Choreographer.java:790)
at android.view.Choreographer.doFrame(Choreographer.java:725)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:951)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
2021-06-05 23:23:33.421 5892-5892/com.jakobu.memory I/Process: Sending signal. PID: 5892 SIG: 9
问题出在这两个文件中。渐变角度应为 45 或其倍数之一,如 90、135、180、225 ... 等。
所以将其更改为 90
gradient_3.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="90"
android:endColor="#f674da"
android:startColor="#38d7bf"
android:type="linear"/>
</shape>
并将其更改为 45(最接近 48)
gradient_4.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="45"
android:endColor="#e4033e"
android:startColor="#e6de15"
android:type="linear"/>
</shape>
我在我的应用程序中添加了动画渐变背景,但几秒钟后应用程序崩溃了。异常表示 线性渐变需要角度属性 。但是我添加到项目中的每个渐变中都有一个角度属性。有人对我搞砸了什么提出建议吗?
MainActivity.kt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//background animation
val constraintLayout = findViewById<ConstraintLayout>(R.id.layout)
val animationDrawable = constraintLayout.background as AnimationDrawable
animationDrawable.setEnterFadeDuration(2000)
animationDrawable.setExitFadeDuration(4000)
animationDrawable.start()
...
}
activity_main.xml
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout"
android:background="@drawable/gradient_list"
tools:context=".MainActivity">
...
gradient_list.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/gradient_1"
android:duration="5000"/>
<item android:drawable="@drawable/gradient_2"
android:duration="5000"/>
<item android:drawable="@drawable/gradient_3"
android:duration="5000"/>
<item android:drawable="@drawable/gradient_4"
android:duration="5000"/>
</animation-list>
gradient_1.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="225"
android:endColor="#044fab"
android:startColor="#f18127"
android:type="linear"/>
</shape>
gradient_2.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="45"
android:endColor="#43b4ef"
android:startColor="#32ff3f"
android:type="linear"/>
</shape>
gradient_3.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="70"
android:endColor="#f674da"
android:startColor="#38d7bf"
android:type="linear"/>
</shape>
gradient_4.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="48"
android:endColor="#e4033e"
android:startColor="#e6de15"
android:type="linear"/>
</shape>
这是崩溃后的日志:
2021-06-05 23:23:33.409 5892-5892/com.jakobu.memory E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.jakobu.memory, PID: 5892
java.lang.IllegalArgumentException: Linear gradient requires 'angle' attribute to be a multiple of 45
at android.graphics.drawable.GradientDrawable$GradientState.updateGradientStateOrientation(GradientDrawable.java:2208)
at android.graphics.drawable.GradientDrawable$GradientState.getOrientation(GradientDrawable.java:2196)
at android.graphics.drawable.GradientDrawable.ensureValidRect(GradientDrawable.java:1273)
at android.graphics.drawable.GradientDrawable.draw(GradientDrawable.java:717)
at android.graphics.drawable.DrawableContainer.draw(DrawableContainer.java:87)
at android.view.View.getDrawableRenderNode(View.java:21702)
at android.view.View.drawBackground(View.java:21636)
at android.view.View.draw(View.java:21413)
at android.view.View.updateDisplayListIfDirty(View.java:20298)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4372)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4345)
at android.view.View.updateDisplayListIfDirty(View.java:20258)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4372)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4345)
at android.view.View.updateDisplayListIfDirty(View.java:20258)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4372)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4345)
at android.view.View.updateDisplayListIfDirty(View.java:20258)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4372)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4345)
at android.view.View.updateDisplayListIfDirty(View.java:20258)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4372)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4345)
at android.view.View.updateDisplayListIfDirty(View.java:20258)
at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:575)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:581)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:654)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:3610)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:3418)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2755)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1721)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7598)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:966)
at android.view.Choreographer.doCallbacks(Choreographer.java:790)
at android.view.Choreographer.doFrame(Choreographer.java:725)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:951)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
2021-06-05 23:23:33.421 5892-5892/com.jakobu.memory I/Process: Sending signal. PID: 5892 SIG: 9
问题出在这两个文件中。渐变角度应为 45 或其倍数之一,如 90、135、180、225 ... 等。 所以将其更改为 90 gradient_3.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="90"
android:endColor="#f674da"
android:startColor="#38d7bf"
android:type="linear"/>
</shape>
并将其更改为 45(最接近 48) gradient_4.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="45"
android:endColor="#e4033e"
android:startColor="#e6de15"
android:type="linear"/>
</shape>