public
class
MyAnimationView
extends
View {
private
static
final
int
RED =
0xffFF8080
;
private
static
final
int
BLUE =
0xff8080FF
;
private
static
final
int
CYAN =
0xff80ffff
;
private
static
final
int
GREEN =
0xff80ff80
;
public
final
ArrayList<ShapeHolder> balls =
new
ArrayList<ShapeHolder>();
AnimatorSet animation =
null
;
public
MyAnimationView(Context context) {
super
(context);
ValueAnimator colorAnim = ObjectAnimator.ofInt(
this
,
"backgroundColor"
, RED, BLUE);
colorAnim.setDuration(
3000
);
colorAnim.setEvaluator(
new
ArgbEvaluator());
colorAnim.setRepeatCount(ValueAnimator.INFINITE);
colorAnim.setRepeatMode(ValueAnimator.REVERSE);
colorAnim.start();
}
public
MyAnimationView(Context context, AttributeSet attrs) {
super
(context, attrs);
ValueAnimator colorAnim = ObjectAnimator.ofInt(
this
,
"backgroundColor"
, RED, BLUE);
colorAnim.setDuration(
3000
);
colorAnim.setEvaluator(
new
ArgbEvaluator());
colorAnim.setRepeatCount(ValueAnimator.INFINITE);
colorAnim.setRepeatMode(ValueAnimator.REVERSE);
colorAnim.start();
}
public
MyAnimationView(Context context, AttributeSet attrs,
int
defStyleAttr) {
super
(context, attrs, defStyleAttr);
ValueAnimator squashAnim1 = ObjectAnimator.ofFloat(newBall,
"y"
, newBall.getY(),
newBall.getY() - 25f);
squashAnim1.setDuration(duration/
4
);
squashAnim1.setRepeatCount(
1
);
squashAnim1.setRepeatMode(ValueAnimator.REVERSE);
squashAnim1.setInterpolator(
new
DecelerateInterpolator());
ValueAnimator squashAnim2 = ObjectAnimator.ofFloat(newBall,
"height"
, newBall.getHeight(),
newBall.getHeight() +
50
);
squashAnim2.setDuration(duration/
4
);
squashAnim2.setRepeatCount(
1
);
squashAnim2.setRepeatMode(ValueAnimator.REVERSE);
squashAnim2.setInterpolator(
new
DecelerateInterpolator());
ValueAnimator stretchAnim1 = ObjectAnimator.ofFloat(newBall,
"x"
, endX,
endX + 25f);
stretchAnim1.setDuration(duration/
4
);
stretchAnim1.setRepeatCount(
1
);
stretchAnim1.setInterpolator(
new
DecelerateInterpolator());
stretchAnim1.setRepeatMode(ValueAnimator.REVERSE);
ValueAnimator stretchAnim2 = ObjectAnimator.ofFloat(newBall,
"width"
,
newBall.getWidth(), newBall.getWidth() -
25
);
stretchAnim2.setDuration(duration/
4
);
stretchAnim2.setRepeatCount(
1
);
stretchAnim2.setInterpolator(
new
DecelerateInterpolator());
stretchAnim2.setRepeatMode(ValueAnimator.REVERSE);
ValueAnimator bounceBackAnim = ObjectAnimator.ofFloat(newBall,
"x"
, endX,
startX);
bounceBackAnim.setDuration(duration);
bounceBackAnim.setInterpolator(
new
DecelerateInterpolator());
AnimatorSet bouncer =
new
AnimatorSet();
bouncer.play(bounceAnim).before(squashAnim1);
bouncer.play(squashAnim1).with(squashAnim2);
bouncer.play(squashAnim1).with(stretchAnim1);
bouncer.play(squashAnim1).with(stretchAnim2);
ValueAnimator fadeAnim = ObjectAnimator.ofFloat(newBall,
"alpha"
, 1f, 0f);
fadeAnim.setDuration(
250
);
fadeAnim.addListener(
new
AnimatorListenerAdapter() {
@Override
public
void
onAnimationEnd(Animator animation) {
balls.remove(((ObjectAnimator)animation).getTarget());
}
});
AnimatorSet animatorSet =
new
AnimatorSet();
animatorSet.play(bouncer).before(fadeAnim);
animatorSet.start();
return
true
;
}