如何从XML文件加载AnimationDrawable加载、文件、XML、AnimationDrawable

2023-09-05 01:47:49 作者:失我者永失

我有一些自定义的类BitmapStorage,不依附于任何视图或什么 - 一个实用工具之一。 我有born_animation.xml文件,其中包含<动画列表>与动画帧:

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>

我要加载从XML文件动画使用资源类的AnimationDrawable(所以它会做所有的分析对我来说),提取位图,并把它们给我的自定义存储类。

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.

我的问题:

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

跆拳道?有人能解释我吗? code编译罚款。所有资源到位。

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

我尝试另一种方式 - (如在开发指南中描述)使用的ImageView做解析

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.

任何hinsts将大大AP preciated,在此先感谢。

Any hinsts would be greatly appreciated, thanks in advance.

推荐答案

耶,我找到了原因! :)

Yay, I found the cause! :)

这是我的坏:我没有过我的animation.xml文件的正确格式为:

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

在属性命名空间(由于某种原因,我决定了它不是必需的) 我并没有采用Android 在我删除了持续时间属性与&lt;项目&GT;标签 I didn't use android: namespace in attributes (for some reason i decided it's not required) I deleted "duration" attribute in <item> tags

在我固定这些东西res.getDrawable()开始返回正确的AnimationDrawable实例。

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

只好把目光更多precisely在Resources.NotFoundException和它的getCause()来找出什么是错的:)

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