Android 如何从 xml 文件加载 AnimationDrawable

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2136258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:41:44  来源:igfitidea点击:

How to load AnimationDrawable from xml file

androidandroid-animation

提问by dimsuz

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:

我有一些自定义类 BitmapStorage,没有附加到任何视图或其他任何东西 - 一个实用程序。我有born_animation.xml 文件,其中包含带有动画帧的 <animation-list>:

<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.

我想从 xml 文件中加载动画作为 AnimationDrawable 使用 Resources 类(所以它会为我做所有的解析),提取位图并将它们放到我的自定义存储类中。

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 进行解析(如开发指南中所述)

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.

结果是一样的。它返回 null drawable。

Any hinsts would be greatly appreciated, thanks in advance.

任何提示将不胜感激,提前致谢。

采纳答案by dimsuz

Yay, I found the cause! :)

是的,我找到了原因!:)

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

这是我的错:我的 animation.xml 文件格式不正确:

  • I didn't use android: namespace in attributes (for some reason i decided it's not required)
  • I deleted "duration" attribute in <item> tags
  • 我没有在属性中使用 android: 命名空间(出于某种原因,我认为它不是必需的)
  • 我删除了 <item> 标签中的“duration”属性

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

在我修复了这些东西之后 res.getDrawable() 开始返回一个正确的 AnimationDrawable 实例。

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

必须更精确地查看 Resources.NotFoundException ,它是 getCause() 找出问题所在:)

回答by Alex Volovoy

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" />

回答by SpearHend

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

这可用于从“xml”目录加载资源。

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"); 
}