利用Property Animation(属性卡通片)使组件的背景色渐变
利用Property Animation(属性动画)使组件的背景色渐变
MainActivity如下:
package cc.testpropertyanimation; import android.os.Bundle; import android.view.View; import android.widget.RelativeLayout; import android.animation.AnimatorInflater; import android.animation.ArgbEvaluator; import android.animation.ObjectAnimator; import android.app.Activity; import android.content.Context; /** * Demo描述: * 利用Property Animation(属性动画)使组件的背景色渐变 * * 参考资料 * Android疯狂讲义(第二版) 作者李刚 * Thank you very much * */ public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init(){ RelativeLayout relativeLayout=(RelativeLayout) findViewById(R.id.relativeLayout); AnimationView animationView=new AnimationView(this); relativeLayout.addView(animationView); } public class AnimationView extends View{ public AnimationView(Context context) { super(context); ObjectAnimator objectAnimator= (ObjectAnimator) AnimatorInflater.loadAnimator(MainActivity.this, R.animator.coloranimation); objectAnimator.setEvaluator(new ArgbEvaluator()); objectAnimator.setTarget(this); objectAnimator.start(); } } }
coloranimation.xml如下:
<?xml version="1.0" encoding="utf-8"?> <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" android:propertyName="backgroundColor" android:duration="5000" android:valueFrom="#ff0033" android:valueTo="#000099" android:repeatCount="infinite" android:repeatMode="reverse" android:valueType="intType"> </objectAnimator>
main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/relativeLayout" android:layout_width="match_parent" android:layout_height="match_parent" > </RelativeLayout>