gpt4 book ai didi

java - ValueAnimator 改变背景颜色

转载 作者:太空宇宙 更新时间:2023-11-03 13:01:38 25 4
gpt4 key购买 nike

我想创建一个背景色不断从红色变为蓝色的屏幕。出于某种原因,当我尝试实例化 ValueAnimator 时它总是崩溃。我不知道我的代码有什么问题

谢谢

动画类

public BackgroundAnimation(Context context){
super(context);
ValueAnimator colorAnim = ObjectAnimator.ofInt(R.anim.animator, "backgroundColor", Color.RED, Color.BLUE);
colorAnim.setDuration(3000);
colorAnim.setEvaluator(new ArgbEvaluator());
colorAnim.setRepeatCount(ValueAnimator.INFINITE);
colorAnim.setRepeatMode(ValueAnimator.REVERSE);
colorAnim.start();

}

animator.xml

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

主类

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.menu);

LinearLayout container = (LinearLayout) findViewById(R.id.container);
container.addView(new BackgroundAnimation(this));

}

ma​​in.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<TextView
android:id="@+id/TextView01"
android:gravity="center"
android:textSize="20sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>

<TextView
android:id="@+id/TextView02"
android:gravity="center"
android:textSize="20sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>

<ImageView
android:id="@+id/ImageView01"
android:layout_width="fill_parent"
android:layout_height="300sp"/>

</LinearLayout>

最佳答案

您可以使用 ObjectAnimator 来改变背景颜色:

对于 API >= 21 :

ObjectAnimator colorAnimator = ObjectAnimator.ofArgb(travelersListView.getBackground().mutate(), "tint", mCurrentBackground, mFadeColor);
colorAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
colorAnimator.start();

要获得 API 16 的向后支持,请使用:

ObjectAnimator colorAnimator = ObjectAnimator.ofObject(travelersListView.getBackground().mutate(), "tint", new ArgbEvaluator(), mCurrentBackground, mFadeColor);
colorAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
colorAnimator.start();

关于java - ValueAnimator 改变背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10384484/

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