java 如何以编程方式在 Android 上加载动画师 xml 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33745029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 22:05:07  来源:igfitidea点击:

How to load animator xml file on Android programatically?

javaandroidxmlanimation

提问by shoheikawano

According to the Android developer site, we can load AnimatorSetclass programatically from xml file located on the path like this: res/animator/filename.xml. So I have created a sample project and tried to see if it actually works, and it doesn't; nothing happens. It would be very nice if I can understand what is missing and/or what I have done wrong. Thanks in advance! Below is my animator xml file and Java code to load the xml:

Android开发网站,我们可以加载AnimatorSet从位于这样的路径上的XML文件编程类:res/animator/filename.xml。所以我创建了一个示例项目并尝试查看它是否真的有效,但它没有;没发生什么事。如果我能理解遗漏了什么和/或我做错了什么,那就太好了。提前致谢!下面是我的动画师 xml 文件和用于加载 xml 的 Java 代码:

res/animator/sample.xml:

res/animator/sample.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:ordering="sequentially"
    >
  <set>
    <objectAnimator
        android:propertyName="x"
        android:duration="500"
        android:valueTo="400"
        android:valueType="intType"
        />
    <objectAnimator
        android:propertyName="y"
        android:duration="500"
        android:valueTo="300"
        android:valueType="intType"
        />
  </set>
  <objectAnimator
      android:propertyName="alpha"
      android:duration="500"
      android:valueTo="1f"
      />
</set>

And here is my Java codes to load the xml file above:

这是我加载上面 xml 文件的 Java 代码:

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
  @Override public void onClick(View view) {
    // Load and start Animaton
    AnimatorSet animSet =
        (AnimatorSet) AnimatorInflater.loadAnimator(view.getContext(), R.animator.sample);
    animSet.setTarget(view);
    animSet.start();
  }
});

采纳答案by DeltaCap019

This is an error in the example given in the documentation.

这是文档中给出的示例中的错误。

Try changing the android:valueType="intType"to android:valueType="floatType".

尝试将 更改android:valueType="intType"android:valueType="floatType"

It works in case of @RaymondChenonas he is not explicitly changing the android:valueTypeto intso system is taking the default one float

它适用于@RaymondCheno 的情况,因为他没有明确更改为 android:valueTypeint因此系统采用默认值float

The problem here is you are giving android:valueType="intType"in your animatorwhich is supposed to be android:valueType="floatType"for the property android:propertyName="x"you are animating.

这里的问题是你给android:valueType="intType"了你的动画师,这应该是你正在制作动画android:valueType="floatType"的财产android:propertyName="x"

At runtime system look for setterfor the property, you want to animate. Like in your case it will look for setX(), but as you are defining argument type of type intit causes a mismatch as there is no such method, I don't know why it doesn't lead to a crash.

在运行时系统寻找属性的setter,你想设置动画。就像你的情况一样,它会寻找setX(),但是当你定义类型的参数类型时,int它会导致不匹配,因为没有这样的方法,我不知道为什么它不会导致崩溃。

Look at the properties of Viewclass there is a method setX(float)

查看View类的属性有一个方法setX(float)

For further understanding, you can refer StackOverflow Question

为了进一步理解,您可以参考StackOverflow Question

回答by Raymond Chenon

You set contains another Set res/animator/sample.xml. Simplify it

您的 set 包含另一个 Set res/animator/sample.xml。简化它

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:ordering="sequentially"
    >

  <objectAnimator
      android:propertyName="alpha"
      android:duration="500"
      android:valueTo="1f"
      />
</set>

You inflate the AnimatorSet like this

你像这样膨胀 AnimatorSet

AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(getActivity(), R.animator.sample);
set.setTarget(fab); // set the view you want to animate
set.start();

So far, I haven't found a way to inflate objectAnimatorfrom xml to Java. I have to wrap it inside a Set

到目前为止,我还没有找到将objectAnimator从 xml膨胀到 Java 的方法。我必须把它包裹在一个Set 中