Android安卓抖动动画的实现方式

效果:


一、使用Animation(补间动画)

1.1代码实现

/**
 * 晃动动画
 *
 * @param counts 0.5秒钟晃动多少下
 * @return
 */
public static Animation shakeAnimation(int counts) {
    Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0);
    //设置一个循环加速器,使用传入的次数就会出现摆动的效果。
    translateAnimation.setInterpolator(new CycleInterpolator(counts));
    translateAnimation.setDuration(500);
    return translateAnimation;
}

/**
 * 0.1秒钟摇动3 * @param counts
 * @param view
 */
public static void overshootAnimation(int counts, View view) {
    TranslateAnimation animation = new TranslateAnimation(0, -5, 0, 0);
    animation.setInterpolator(new OvershootInterpolator());
    animation.setDuration(100);
    animation.setRepeatCount(3);
    animation.setRepeatMode(Animation.REVERSE);
    view.startAnimation(animation);
}

1.2xml动画布局文件实现

shake_two.xml

<translate xmlns:android="http://schemas.android.com/apk/res/android"
           android:duration="500"
           android:fromXDelta="0"
           android:interpolator="@anim/cycle"
           android:toXDelta="10"/>

cycle.xml

<cycleInterpolator
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:cycles="3"
    >
</cycleInterpolator>

使用

//xml动画布局文件实现
Animation animationTwo = AnimationUtils.loadAnimation(this, R.anim.shake_two);
editText_shake.setAnimation(animationTwo);
animationTwo.start();

二、使用属性动画ObjectAnimator 

/**
 * 指定时间内进行左右晃动
 * @param view
 * @param delta 左右偏移
 * @param duration
 * @return
 */
public static ObjectAnimator shakeKeyframe(View view, int delta,int duration) {

    PropertyValuesHolder pvhTranslateX = PropertyValuesHolder.ofKeyframe(View.TRANSLATION_X,
            //Keyframe是一个时间/值对,用于定义在某个时刻动画的状态。
            //比如Keyframe.ofFloat(.10f, -10)定义了当动画进行了50%的时候,x轴的偏移值应该是-10            Keyframe.ofFloat(0f, 0),
            Keyframe.ofFloat(.1f, -delta),
            Keyframe.ofFloat(.2f, delta),
            Keyframe.ofFloat(.3f, -delta),
            Keyframe.ofFloat(.5f, delta),
            Keyframe.ofFloat(.7f, -delta),
            Keyframe.ofFloat(.9f, delta),
            Keyframe.ofFloat(1f, 0f)
    );

    return ObjectAnimator.ofPropertyValuesHolder(view, pvhTranslateX).
            setDuration(duration);
}

。。。

参考:

http://blog.csdn.net/howlaa/article/details/43152309

http://blog.csdn.net/zhangcanyan/article/details/54906935

http://www.apkbus.com/forum.php?mod=viewthread&tid=257854&highlight=%E5%8A%A8%E7%94%BB


  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值