我想在几毫秒后自动更改我的android应用程序的背景。因此,首先我创建了一个带有内容的可绘制资源文件"colorchange.xml“。
<animation-list android:oneshot="false" xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/color1" android:duration="1" />
<item android:drawable="@color/color2" android:duration="2" />
<item android:drawable="@color/color3" android:duration="3" />
<item android:drawable="@color/color4" android:duration="4" />
<item android:drawable="@color/color5" android:duration="5" />
我在我的主xml文件中使用它作为背景,现在只有color1被设置为背景。动画的颜色没有显示为背景。
这其中有什么问题,我怎样才能达到我的目标?
发布于 2016-04-19 07:16:22
检查你什么时候开始动画。根据文档,不应该在onCreate()方法上使用动画。
注意:不要在活动的onCreate(Bundle)方法中调用它,因为AnimationDrawable还没有完全连接到窗口。如果您想立即播放动画而不需要交互,那么您可能需要从您活动中的onWindowFocusChanged(布尔值)方法调用它,当Android使您的窗口成为焦点时,该方法将被调用。
编辑--如何使用动画的示例
// Load the View that will host the animation and
// set its background to our AnimationDrawable XML resource.
LinearLayout linearLayout = (LinearLayout) layout.findViewById(R.id.example_view);
linearLayout.setBackgroundResource(R.drawable.colorchange);
// Get the background, which has been compiled to an AnimationDrawable object.
AnimationDrawable frameAnimation = (AnimationDrawable) linearLayout.getBackground();
// Start the animation (looped playback by default).
frameAnimation.start();
发布于 2016-04-19 06:59:42
您可以像这样在colors.xml
中定义您的颜色:
<color name="yourcolor">#0FFF</color>
然后你可以像这样使用它:
<item android:drawable=@colors/yourcolor android:duration="5" />
发布于 2016-04-19 06:59:24
您应该在value/color s.xml文件中定义颜色,并像android:drawable="@color/MyGreen“一样使用它。
https://stackoverflow.com/questions/36721992
复制相似问题