How to load AnimationDrawable from xml file

后端 未结 3 504
谎友^
谎友^ 2020-12-06 16:56

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 with

相关标签:
3条回答
  • 2020-12-06 17:09

    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 :)

    0 讨论(0)
  • 2020-12-06 17:11

    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"); 
    }
    
    0 讨论(0)
  • 2020-12-06 17:27

    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" />
    
    0 讨论(0)
提交回复
热议问题