1.动画文件:
1 <?xml version="1.0" encoding="utf-8"?> 2 <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" 3 android:propertyName="backgroundColor" 4 android:duration="5000" 5 android:valueFrom="#ff0033" 6 android:valueTo="#000099" 7 android:repeatCount="infinite" 8 android:repeatMode="reverse" 9 android:valueType="intType"> 10 </objectAnimator>
2.类文件:
1 package com.soft.colorchangingdemo; 2 3 import android.animation.AnimatorInflater; 4 import android.animation.ArgbEvaluator; 5 import android.animation.ObjectAnimator; 6 import android.app.Activity; 7 import android.content.Context; 8 import android.os.Bundle; 9 import android.view.View; 10 import android.widget.RelativeLayout; 11 12 public class MainActivity extends Activity { 13 14 @Override 15 protected void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.activity_main); 18 init(); 19 } 20 21 private void init() { 22 RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout); 23 AnimationView animationView = new AnimationView(this); 24 relativeLayout.addView(animationView); 25 } 26 27 public class AnimationView extends View { 28 public AnimationView(Context context) { 29 super(context); 30 ObjectAnimator objectAnimator = (ObjectAnimator) AnimatorInflater 31 .loadAnimator(MainActivity.this, R.anim.coloranimation); 32 objectAnimator.setEvaluator(new ArgbEvaluator()); 33 objectAnimator.setTarget(this); 34 objectAnimator.start(); 35 } 36 37 } 38 39 }
利用动画实现控件背景色渐变
时间: 2024-10-10 02:21:51