我有一些自定义类 BitmapStorage,没有附加到任何 View 或其他任何东西 - 一个实用程序。
我有 Born_animation.xml 文件,其中包含
<animation-list oneshot="true" >
<item drawable="@drawable/frame01" />
<item drawable="@drawable/frame02" />
</animation-list>
我想使用 Resources 类将 xml 文件中的动画作为 AnimationDrawable 加载(因此它会为我完成所有解析),提取位图并将它们放入我的自定义存储类中。
我遇到的问题:
Resources res = context.getResources();
AnimationDrawable drawable = (AnimationDrawable)res.getDrawable(R.drawable.born_animation);
assertTrue( drawable != null ); <= fails! it's null
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
结果是一样的。它返回 null 可绘制对象。
任何提示将不胜感激,在此先感谢。
最佳答案
可绘制
<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>
代码:
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);
}
查看
<ImageView
android:id="@+id/progress_bar"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/myprogress" />
关于android - 如何从 xml 文件加载 AnimationDrawable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2136258/