dim*_*suz 15 android android-animation
我有一些自定义类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.
任何希望都会受到高度赞赏,提前谢谢.
Ale*_*voy 28
可绘制
<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>
Run Code Online (Sandbox Code Playgroud)
码:
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);
}
Run Code Online (Sandbox Code Playgroud)
视图
<ImageView
android:id="@+id/progress_bar"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/myprogress" />
Run Code Online (Sandbox Code Playgroud)
是的,我找到了原因!:)
这是我的坏事:我没有我的animation.xml文件的正确格式:
在我修复了这些东西后,res.getDrawable()开始返回一个正确的AnimationDrawable实例.
不得不更精确地看看Resources.NotFoundException和它的getCause()来找出错误:)
| 归档时间: |
|
| 查看次数: |
53197 次 |
| 最近记录: |