How to animate a single layer list item

I have a layer list object, it contains two images, one is the background, and the other is the image of the rotation disk, which will be the top of the background image. those. I use this list of layers as a linearlayout background, and I only want to animate the β€œdisk_bg” element in the layer list;

<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/ android"> <item android:drawable="@drawable/player_bg" /> <item android:top="166dp" > <bitmap android:id="@+id/disk_bg" android:src="@drawable/cd" android:gravity="center" /> </item> 

I use this list of layers as the background layout, do you know how I can animate the disk_bg layer in my application?

You can help me, thank you very much.

Do not you understand my question? or is there no way to do this?

+8
android animation
source share
1 answer

First, create 2 (or more) layer list resources, i.e. * layer_frame1.xml * and * layer_frame2.xml *, where you set your frames. In your case, let's change the android: top of the disk element.

Then create an animation list resource in which you set the time and frame order:

 <?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/layer_frame1" android:duration="100"/> <item android:drawable="@drawable/layer_frame2" android:duration="100"/> </animation-list> 

Save it in a file, i.e. * drawable / player_animation.xml * and set it as the background in the view

 <View android:id="@+id/animation_test" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/player_animation" /> 

Finally, in your code, just say when you want to start the animation.

  ((AnimationDrawable)findViewById(R.id.animation_test).getBackground()).start(); 

Beware don't run the animation inside the onCreate () method.

+2
source share

All Articles