package com.example.selector;
import android.animation.TypeEvaluator;
public class ColorEvaluator implements TypeEvaluator {
private int mCurrentRed = -1;
private int mCurrentGreen = -1;
private int mCurrentBlue = -1;
@Override
public Object evaluate(float fraction, Object startValue, Object endValue) {
System.out.println("fraction..........."+fraction);
if(fraction==0){//用于repeat,值复位
mCurrentRed = -1;
mCurrentGreen = -1;
mCurrentBlue = -1;
}
String startColor = (String) startValue;
String endColor = (String) endValue;
int startRed = Integer.parseInt(startColor.substring(1, 3), 16);
int startGreen = Integer.parseInt(startColor.substring(3, 5), 16);
int startBlue = Integer.parseInt(startColor.substring(5, 7), 16);
int endRed = Integer.parseInt(endColor.substring(1, 3), 16);
int endGreen = Integer.parseInt(endColor.substring(3, 5), 16);
int endBlue = Integer.parseInt(endColor.substring(5, 7), 16);
// 初始化颜色的值
if (mCurrentRed == -1 ) {
mCurrentRed = startRed;
}
if (mCurrentGreen == -1 ) {
mCurrentGreen = startGreen;
}
if (mCurrentBlue == -1 ) {
mCurrentBlue = startBlue;
}
// 计算初始颜色和结束颜色之间的差值
int redDiff = Math.abs(startRed - endRed);
int greenDiff = Math.abs(startGreen - endGreen);
int blueDiff = Math.abs(startBlue - endBlue);
int colorDiff = redDiff + greenDiff + blueDiff;
if (mCurrentRed != endRed) {
mCurrentRed = getCurrentColor(startRed, endRed, colorDiff, 0,
fraction);
} else if (mCurrentGreen != endGreen) {
mCurrentGreen = getCurrentColor(startGreen, endGreen, colorDiff,
redDiff, fraction);
} else if (mCurrentBlue != endBlue) {
mCurrentBlue = getCurrentColor(startBlue, endBlue, colorDiff,
redDiff + greenDiff, fraction);
}
// 将计算出的当前颜色的值组装返回
String currentColor = "#" + getHexString(mCurrentRed)
+ getHexString(mCurrentGreen) + getHexString(mCurrentBlue);
/*
if(currentColor.equalsIgnoreCase(endColor)){
mCurrentRed = -1;
mCurrentGreen = -1;
mCurrentBlue = -1;
}*/
return currentColor;
}
/**
* 根据fraction值来计算当前的颜色。
*/
private int getCurrentColor(int startColor, int endColor, int colorDiff,
int offset, float fraction) {
int currentColor;
if (startColor > endColor) {
currentColor = (int) (startColor - (fraction * colorDiff - offset));
if (currentColor < endColor) {
currentColor = endColor;
}
} else {
currentColor = (int) (startColor + (fraction * colorDiff - offset));
if (currentColor > endColor) {
currentColor = endColor;
}
}
return currentColor;
}
/**
* 将10进制颜色值转换成16进制。
*/
private String getHexString(int value) {
String hexString = Integer.toHexString(value);
if (hexString.length() == 1) {
hexString = "0" + hexString;
}
return hexString;
}
}
将Button进行封装
package com.example.selector;
import android.animation.ArgbEvaluator;
import android.graphics.Color;
import android.widget.Button;
/**颜色过渡动画apiArgbEvalutor
* @author Administrator
*
*/
public class ColorButton {
private Button btn;
public ColorButton(Button btn){
this.btn = btn;
}
public String color;
public String getColor(){
return color;
}
public void setColor(String color){
btn.setBackgroundColor(Color.parseColor(color));
}
}
MaindActivity
package com.example.selector;
import java.util.Random;
import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
private ProgressBar pb;
private Button btn;
@SuppressLint("NewApi") @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
ObjectAnimator animator = ObjectAnimator.ofObject(new ColorButton(btn), "color", new ColorEvaluator(), "#0000FF", "#FF0000");
animator.setDuration(6000);//设置过渡动画时长
animator.setRepeatCount(3);
animator.start();//开始动画
}
}