【问题标题】:How to load AnimationDrawable from xml file如何从 xml 文件加载 AnimationDrawable
【发布时间】:2011-01-09 07:48:44
【问题描述】:

我有一些自定义类 BitmapStorage,没有附加到任何视图或其他任何东西 - 一个实用程序。 我有 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 可绘制对象。

任何提示将不胜感激,在此先感谢。

【问题讨论】:

  • 不要将其设置为背景 - 请参见下面的示例。

标签: android android-animation


【解决方案1】:

可绘制

<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" />

【讨论】:

  • 嗯。谢谢,但这主要是开发指南中的代码 - 这不是我想要的。实际上创建 ImageView 是我不需要的 - 我尝试将其作为一种解决方法。我不会在任何地方展示它。没有 ImageView 怎么办?为什么我尝试的第一个变体(使用 res.getDrawable())不起作用?
  • 我也不能使用 View.findViewById,因为我没有 View - 正如我所说的,这是一个实用程序类。想象一下,我有一个对视图/活动一无所知的库。那它不能加载资源吗? :)
  • 活动完成后如何完成?
  • 为了清楚起见,“Drawable”代码实际上应该是它自己的 xml 文件,位于名为“myprogress.xml”的 res/drawable 文件夹中,并且还需要包含 xmlns 部分。
  • progress1,progress2,progress3 的作用是什么我想用drawable.xml创建可绘制的动画进度圈
【解决方案2】:

是的,我找到了原因! :)

这是我的错:我的 animation.xml 文件格式不正确:

  • 我没有在属性中使用 android: namespace(出于某种原因,我认为它不是必需的)
  • 我删除了 标签中的“duration”属性

在我修复这些东西之后 res.getDrawable() 开始返回一个正确的 AnimationDrawable 实例。

必须更准确地查看 Resources.NotFoundException 并通过 getCause() 找出问题所在:)

【讨论】:

    【解决方案3】:

    这可以用来从“xml”目录加载资源。

    Drawable myDrawable;
    Resources res = getResources();
    try {
       myDrawable = Drawable.createFromXml(res, res.getXml(R.xml.my_drawable));
    } catch (Exception ex) {
       Log.e("Error", "Exception loading drawable"); 
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-21
      • 2023-04-05
      • 1970-01-01
      • 2011-07-03
      • 2011-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-23
      相关资源
      最近更新 更多