gpt4 book ai didi

android - 如何从 XML 文件运行动画?

转载 作者:行者123 更新时间:2023-11-29 15:35:21 28 4
gpt4 key购买 nike

我在android studio的animator目录下创建了一个动画文件。我正在尝试更改按钮的颜色。

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator android:duration="500"
xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="color"
android:valueTo="#333333"
android:valueFrom="@color/start_button"
/>

然后我尝试从我的 MainActivity.java 运行动画,但是当我单击按钮运行动画时,应用程序崩溃了。

public void btnClick(View view){
AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.rotation);
set.setTarget(view); // set the view you want to animate
set.start();
}

最佳答案

android:propertyName

String. Required. The object's property to animate, referenced by its name. For example you can specify "alpha" or "backgroundColor" for a View object. The objectAnimator element does not expose a target attribute, however, so you cannot set the object to animate in the XML declaration. You have to inflate your animation XML resource by calling loadAnimator() and call setTarget() to set the target object that contains this property.

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

<objectAnimator android:duration="500"
xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="backgroundColor"
android:valueTo="#333333"
android:valueFrom="@color/start_button"
/>
</set>

膨胀并运行 AnimatorSet

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

更新

如果您设置了 propertyName="color",那么目标 View 应该有 setColor 方法。

W/PropertyValuesHolder: Method setColor() with type int not found on target class class android.support.v7.widget.Toolbar

通常 propertyName="someName" 然后目标 View 应该有方法 setSomeName

Ex. 如果您需要更改自定义 View angle 那么 propertyName="angle"自定义目标 View 必须实现 setAngle

关于android - 如何从 XML 文件运行动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51702803/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com