有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java ValueAnimator更改背景颜色

我想创建一个背景颜色不断从红色变为蓝色的屏幕。由于某些原因,当我尝试实例化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:安卓="http://schemas.安卓.com/apk/res/安卓">   
   <objectAnimator
         安卓: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));

}

main。xml

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

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

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

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

</LinearLayout>

共 (4) 个答案

  1. # 1 楼答案

    可以使用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();
    
  2. # 3 楼答案

    @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();
    }
    

    试试这个,希望有帮助

  3. # 4 楼答案

    XML文件中没有LinearLayout的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">