这两天写了一些动画,基本都是用的ObjectAnimator实现的,目前来说一些不涉及弧形变形等的使用来说这个足够了,当然其实,就我来说,就是把它当做一个比较高级的计数器来用了
/**
* 放大动画
* @param view
*/
private void blowupAnim(final View view, final OnAnimOverListener listener){
ObjectAnimator goInAnim = new ObjectAnimator();
goInAnim.setFloatValues(1f,1.5f,1f);
goInAnim.setDuration(500);
goInAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float value = (Float) animation.getAnimatedValue();
view.setScaleX(value);
view.setScaleY(value);
// view.setAlpha(1f - value);
}
});
goInAnim.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) { }
@Override
public void onAnimationEnd(Animator animation) {
if(null == listener)return;
listener.onOver();
}
@Override
public void onAnimationCancel(Animator animation) {}
@Override
public void onAnimationRepeat(Animator animation) {}
});
goInAnim.start();
}
这是其中一个典型的例子,这是没什么封装的,就是把一个固定的动画效果写了一下而已,但是还是很好用,这是针对单个属性的修改时的动画
然后是多个属性时的动画
/**
* 上飘的动画
* @param view
*/
private void flutterAnim(final View view, final OnAnimOverListener listener, final Context context){
AnimatorSet animatorSet = new AnimatorSet();//组合动画
final float flutterHeight = view.getHeight() * 3;
ObjectAnimator flutterAnim1 = new ObjectAnimator();
flutterAnim1.setFloatValues(0f,flutterHeight);//5倍的高度
flutterAnim1.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float value = (Float) animation.getAnimatedValue();
mParams.y = mY - (int)value;
mWindowManager.updateViewLayout(view, mParams);
}
});
ObjectAnimator flutterAnim2 = new ObjectAnimator();
final float flutterWidth = view.getWidth()*2/3;
flutterAnim2.setFloatValues(0f,flutterWidth,-flutterWidth,flutterWidth);//5倍的高度
flutterAnim2.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float value = (Float) valueAnimator.getAnimatedValue();
mParams.x = mX + (int)value;
mWindowManager.updateViewLayout(view, mParams);
}
});
ObjectAnimator flutterAnim3 = new ObjectAnimator();
flutterAnim3.setFloatValues(1f,0.5f);
flutterAnim3.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float value = (Float) valueAnimator.getAnimatedValue();
view.setAlpha(value);
}
});
flutterAnim1.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) { }
@Override
public void onAnimationEnd(Animator animation) {
if(null == listener)return;
listener.onOver();
}
@Override
public void onAnimationCancel(Animator animation) {}
@Override
public void onAnimationRepeat(Animator animation) {}
});
animatorSet.setDuration(1000);
animatorSet.setInterpolator(new DecelerateInterpolator());
animatorSet.play(flutterAnim1).with(flutterAnim2).with(flutterAnim3);//三个属性动画同时开始
animatorSet.start();
}
然后是下一个,我网上随便搜了一下帧动画方面的资料,发现都挺挫的,主要是根本没法知道什么时候播放结束,自己写的话又很麻烦,然后我就动了个小方法用ObjectAnimator来实现
int[] images = new int[]{R.drawable.bao_1,R.drawable.bao_2,R.drawable.bao_3,R.drawable.bao_4};
int lastValue = 0;
int currentValue = 0;
/**
* 播放图片的帧动画
* @param view
*/
private void playImgsAnim(final ImageView view, final OnAnimOverListener listener){
ObjectAnimator goInAnim = new ObjectAnimator();
goInAnim.setFloatValues(0f,((float)images.length) - 0.5f);
goInAnim.setDuration(500);
lastValue = -1;
currentValue = 0;
goInAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float value = (Float) animation.getAnimatedValue();
currentValue = (int)value;
if(currentValue != lastValue){
view.setImageResource(images[currentValue]);
lastValue = currentValue;
}
}
});
goInAnim.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) { }
@Override
public void onAnimationEnd(Animator animation) {
if(null == listener)return;
listener.onOver();
}
@Override
public void onAnimationCancel(Animator animation) {}
@Override
public void onAnimationRepeat(Animator animation) {}
});
goInAnim.start();
}
{R.drawable.bao_1,R.drawable.bao_2,R.drawable.bao_2,R.drawable.bao_3,R.drawable.bao_4}
这样就实现了,不过还是不是很灵活如果深入研究下去的话肯定可以,不过我目前只是作为工作上的记录,所以就不深入了
以上三段的动画效果为:
我直接拿ui的效果图来了,这个gif制作的不怎么好,其实红心出现时有一个膨胀的效果,这个图上没显示出来