3

我想创建一个背景颜色不断从红色变为蓝色的屏幕。由于某种原因,当我尝试实例化 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();

}

动画师.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));

}

主要的.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>
4

4 回答 4

3

您可以使用 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();
于 2015-11-22T09:12:25.780 回答
2

LinearLayout 的 XML 文件中没有 id 参数。

<?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"
android:id="@+id/container">
于 2012-04-30T13:30:03.133 回答
1
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.menu);

LinearLayout container = (LinearLayout) findViewById(R.id.container);

changeBackground(container, "#F44336", "#2196F3"); //#F44336 : Red , #2196F3 : Blue

}

public void changeBackground(final View view, String color1, String color2) {
    ValueAnimator anim = new ValueAnimator();
    anim.setIntValues(Color.parseColor(color1), Color.parseColor(color2));
    anim.setEvaluator(new ArgbEvaluator());
    anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator) {

            view.setBackgroundColor((Integer) valueAnimator.getAnimatedValue());
        }
    });

    anim.setDuration(2000);
    anim.setRepeatCount(ValueAnimator.INFINITE);
    anim.setRepeatMode(ValueAnimator.REVERSE);
    anim.start();
}

试试这个,希望这有帮助

于 2016-02-19T10:48:39.170 回答
0

没有变体ObjectAnimator.ofInt()将资源 ID 作为参数。请阅读本文以在 XML 中声明动画。 http://developer.android.com/guide/topics/graphics/prop-animation.html#declaring-xml

于 2013-03-23T08:27:08.563 回答