如何从XML文件加载AnimationDrawable [英] How to load AnimationDrawable from xml file

查看:316
本文介绍了如何从XML文件加载AnimationDrawable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些自定义的类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 :)

这篇关于如何从XML文件加载AnimationDrawable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆