美文网首页Android_花式组件,自定义组件
Android 摇晃动画代码实现技巧

Android 摇晃动画代码实现技巧

作者: 素颜的你 | 来源:发表于2018-04-28 16:15 被阅读239次

有两种效果,下边进行详细介绍:
效果一:以自身x轴中轴为中心,左右以20°晃动

c7a583d2-857d-4c2c-bf95-9da5bd53f294.gif
/**
     * 晃动动画
     * <p>
     * 那么CycleInterpolator是干嘛用的??
     * Api demo里有它的用法,是个摇头效果!
     *
     * @param counts 1秒钟晃动多少下
     * @return Animation
     */
    public static Animation shakeAnimation(int counts) {
        Animation rotateAnimation = new RotateAnimation(0, 20, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnimation.setInterpolator(new CycleInterpolator(counts));
        rotateAnimation.setRepeatCount(-1);
        rotateAnimation.setDuration(3000);
        return rotateAnimation;
    }

效果二:以自身x轴中轴为中心,左右平移

a3e004e1-769e-440c-ba84-a544d05434c5.gif
/**
     * 晃动动画
     * <p>
     * 那么CycleInterpolator是干嘛用的??
     * Api demo里有它的用法,是个摇头效果!
     *
     * @param counts 1秒钟晃动多少下
     * @return Animation
     */
    public static Animation shakeAnimation(int counts) {
        Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0);
        translateAnimation.setInterpolator(new CycleInterpolator(counts));
        translateAnimation.setRepeatCount(100000);
        translateAnimation.setDuration(1000);
        return translateAnimation;
    }

调用方式:

  imageView.setAnimation(shakeAnimation(6));

参考文章:https://blog.csdn.net/pmqiujun/article/details/48627943

相关文章

网友评论

    本文标题:Android 摇晃动画代码实现技巧

    本文链接:https://www.haomeiwen.com/subject/embglftx.html