相关疑难解决方法(0)

如何从xml文件加载AnimationDrawable

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

<animation-list oneshot="true" >
    <item drawable="@drawable/frame01" />
    <item drawable="@drawable/frame02" />
</animation-list>
Run Code Online (Sandbox Code Playgroud)

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

我有的问题是:

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

WTF?有人可以解释一下吗?代码编译好.所有资源都已到位.

我尝试了另一种方法 - 使用ImageView进行解析(如开发指南中所述)

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

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

任何希望都会受到高度赞赏,提前谢谢.

android android-animation

15
推荐指数
2
解决办法
5万
查看次数

标签 统计

android ×1

android-animation ×1