Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load AnimationDrawable from xml file

I have some custom class BitmapStorage, not attached to any View or whatever - an utility one. And I have born_animation.xml file which contains <animation-list> with animation frames:

<animation-list oneshot="true" >
    <item drawable="@drawable/frame01" />
    <item drawable="@drawable/frame02" />
</animation-list>

I want to load animation from xml file as an AnimationDrawable using Resources class (so it would do all the parsing for me), extract Bitmaps and put them to my custom storage class.

The problem i have:

Resources res = context.getResources(); 
AnimationDrawable drawable = (AnimationDrawable)res.getDrawable(R.drawable.born_animation); 
assertTrue( drawable != null ); <= fails! it's null 

WTF? Can someone explain me that? Code compiles fine. All resources are in place.

I tried another way - use ImageView to do the parsing (like described in dev guide)

ImageView view = new ImageView(context); 
view.setBackgroundResource(R.drawable.born_animation); 
AnimationDrawable drawable = (AnimationDrawable)view.getBackground(); 
assertTrue( drawable != null ); <= fails! it's null 

Results are the same. it returns the null drawable.

Any hinsts would be greatly appreciated, thanks in advance.

like image 406
dimsuz Avatar asked Jan 25 '10 22:01

dimsuz


3 Answers

Drawable

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"   
                android:id="@+id/myprogress" 
                android:oneshot="false">
    <item android:drawable="@drawable/progress1" android:duration="150" />
    <item android:drawable="@drawable/progress2" android:duration="150" />
    <item android:drawable="@drawable/progress3" android:duration="150" />
</animation-list> 

Code:

ImageView progress = (ImageView)findViewById(R.id.progress_bar);
if (progress != null) {
    progress.setVisibility(View.VISIBLE);
    AnimationDrawable frameAnimation = (AnimationDrawable)progress.getDrawable();
    frameAnimation.setCallback(progress);
    frameAnimation.setVisible(true, true);
}

View

<ImageView
  android:id="@+id/progress_bar"
  android:layout_alignParentRight="true"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:src="@drawable/myprogress" />
like image 103
Alex Volovoy Avatar answered Oct 19 '22 17:10

Alex Volovoy


Yay, I found the cause! :)

It was my bad: I didn't had the proper format of my animation.xml file:

  • I didn't use android: namespace in attributes (for some reason i decided it's not required)
  • I deleted "duration" attribute in <item> tags

After I fixed these things res.getDrawable() started to return a correct AnimationDrawable instance.

Had to look more precisely at Resources.NotFoundException and it's getCause() to find out what's wrong :)

like image 7
dimsuz Avatar answered Oct 19 '22 16:10

dimsuz


This could be used to load resources from the "xml" directory.

Drawable myDrawable;
Resources res = getResources();
try {
   myDrawable = Drawable.createFromXml(res, res.getXml(R.xml.my_drawable));
} catch (Exception ex) {
   Log.e("Error", "Exception loading drawable"); 
}
like image 3
SpearHend Avatar answered Oct 19 '22 17:10

SpearHend