http://hencoder.com/ui-1-7/
1.argb颜色变化
public class Sample01ArgbEvaluatorLayout extends RelativeLayout {
Sample01ArgbEvaluatorView view;
Button animateBt;
public Sample01ArgbEvaluatorLayout(Context context) {
super(context);
}
public Sample01ArgbEvaluatorLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public Sample01ArgbEvaluatorLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
view = (Sample01ArgbEvaluatorView) findViewById(R.id.objectAnimatorView);
animateBt = (Button) findViewById(R.id.animateBt);
animateBt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ObjectAnimator animator = ObjectAnimator.ofInt(view, "color", 0xffff0000, 0xff00ff00);
animator.setEvaluator(new ArgbEvaluator());
animator.setInterpolator(new LinearInterpolator());
animator.setDuration(2000);
animator.start();
}
});
}
}
2.hsvEvaluator
public class Sample02HsvEvaluatorLayout extends RelativeLayout {
Sample02HsvEvaluatorView view;
Button animateBt;
public Sample02HsvEvaluatorLayout(Context context) {
super(context);
}
public Sample02HsvEvaluatorLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public Sample02HsvEvaluatorLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
view = (Sample02HsvEvaluatorView) findViewById(R.id.objectAnimatorView);
animateBt = (Button) findViewById(R.id.animateBt);
animateBt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ObjectAnimator animator = ObjectAnimator.ofInt(view, "color", 0xffff0000, 0xff00ff00);
animator.setEvaluator(new HsvEvaluator());
animator.setInterpolator(new LinearInterpolator());
animator.setDuration(2000);
animator.start();
}
});
}
private class HsvEvaluator implements TypeEvaluator {
float[] startHsv = new float[3];
float[] endHsv = new float[3];
float[] outHsv = new float[3];
@Override
public Integer evaluate(float fraction, Integer startValue, Integer endValue) {
// 把 ARGB 转换成 HSV
Color.colorToHSV(startValue, startHsv);
Color.colorToHSV(endValue, endHsv);
// 计算当前动画完成度(fraction)所对应的颜色值
if (endHsv[0] - startHsv[0] > 180) {
endHsv[0] -= 360;
} else if (endHsv[0] - startHsv[0] < -180) {
endHsv[0] += 360;
}
outHsv[0] = startHsv[0] + (endHsv[0] - startHsv[0]) * fraction;
if (outHsv[0] > 360) {
outHsv[0] -= 360;
} else if (outHsv[0] < 0) {
outHsv[0] += 360;
}
outHsv[1] = startHsv[1] + (endHsv[1] - startHsv[1]) * fraction;
outHsv[2] = startHsv[2] + (endHsv[2] - startHsv[2]) * fraction;
// 计算当前动画完成度(fraction)所对应的透明度
int alpha = startValue >> 24 + (int) ((endValue >> 24 - startValue >> 24) * fraction);
// 把 HSV 转换回 ARGB 返回
return Color.HSVToColor(alpha, outHsv);
}
}
}
3.移动
public class Sample03OfObjectLayout extends RelativeLayout {
Sample03OfObjectView view;
Button animateBt;
public Sample03OfObjectLayout(Context context) {
super(context);
}
public Sample03OfObjectLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public Sample03OfObjectLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
view = (Sample03OfObjectView) findViewById(R.id.objectAnimatorView);
animateBt = (Button) findViewById(R.id.animateBt);
animateBt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ObjectAnimator animator = ObjectAnimator.ofObject(view, "position",
new PointFEvaluator(), new PointF(0, 0), new PointF(1, 1));
animator.setInterpolator(new LinearInterpolator());
animator.setDuration(1000);
animator.start();
}
});
}
private class PointFEvaluator implements TypeEvaluator {
PointF newPoint = new PointF();
@Override
public PointF evaluate(float fraction, PointF startValue, PointF endValue) {
float x = startValue.x + (fraction * (endValue.x - startValue.x));
float y = startValue.y + (fraction * (endValue.y - startValue.y));
newPoint.set(x, y);
return newPoint;
}
}
}
4.
public class Sample04PropertyValuesHolderLayout extends RelativeLayout {
View view;
Button animateBt;
public Sample04PropertyValuesHolderLayout(Context context) {
super(context);
}
public Sample04PropertyValuesHolderLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public Sample04PropertyValuesHolderLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
view = findViewById(R.id.objectAnimatorView);
animateBt = (Button) findViewById(R.id.animateBt);
animateBt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
PropertyValuesHolder holder1 = PropertyValuesHolder.ofFloat("scaleX", 0, 1);
PropertyValuesHolder holder2 = PropertyValuesHolder.ofFloat("scaleY", 0, 1);
PropertyValuesHolder holder3 = PropertyValuesHolder.ofFloat("alpha", 0, 1);
ObjectAnimator.ofPropertyValuesHolder(view, holder1, holder2, holder3).start();
}
});
}
}
5.动画顺序执行
public class Sample05AnimatorSetLayout extends RelativeLayout {
View view;
Button animateBt;
public Sample05AnimatorSetLayout(Context context) {
super(context);
}
public Sample05AnimatorSetLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public Sample05AnimatorSetLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
view = findViewById(R.id.objectAnimatorView);
animateBt = (Button) findViewById(R.id.animateBt);
animateBt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
view.setTranslationX(-200f);
ObjectAnimator animator1 = ObjectAnimator.ofFloat(view, "alpha", 0, 1);
ObjectAnimator animator2 = ObjectAnimator.ofFloat(view, "translationX", -200, 200);
ObjectAnimator animator3 = ObjectAnimator.ofFloat(view, "rotation", 0, 1080);
animator3.setDuration(1000);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(animator1).before(animator2); // 先执行 1 再执行 2
animatorSet.playTogether(animator2, animator3); // 2 和 3 同时开始
animatorSet.start();
}
});
}
}
6炫酷动画
public class Sample06KeyframeLayout extends RelativeLayout {
Sample06KeyframeView view;
Button animateBt;
public Sample06KeyframeLayout(Context context) {
super(context);
}
public Sample06KeyframeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public Sample06KeyframeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
view = (Sample06KeyframeView) findViewById(R.id.objectAnimatorView);
animateBt = (Button) findViewById(R.id.animateBt);
animateBt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Keyframe keyframe1 = Keyframe.ofFloat(0, 0); // 开始:progress 为 0
Keyframe keyframe2 = Keyframe.ofFloat(0.5f, 100); // 进行到一半是,progres 为 100
Keyframe keyframe3 = Keyframe.ofFloat(1, 80); // 结束时倒回到 80
PropertyValuesHolder holder = PropertyValuesHolder.ofKeyframe("progress", keyframe1, keyframe2, keyframe3);
ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(view, holder);
animator.setDuration(2000);
animator.setInterpolator(new FastOutSlowInInterpolator());
animator.start();
}
});
}
}