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>
# 1 楼答案
可以使用ObjectAnimator更改背景色:
对于API>;=21:
要获得API 16的向后支持,请使用以下命令:
# 2 楼答案
ObjectAnimator.ofInt()
的任何变量都不会将资源ID作为参数。请阅读此文档,了解如何用XML声明动画http://developer.android.com/guide/topics/graphics/prop-animation.html#declaring-xml# 3 楼答案
试试这个,希望有帮助
# 4 楼答案
XML文件中没有LinearLayout的id参数