记一次小白之路使用AndroidViewAnimations 安卓抖动动画

首先我要实现的是类似于这种动画

 

参考过得文章:https://www.jianshu.com/p/7d262563edda

我是在这篇文章的评论中看到两位大佬的对话中,看到了AndroidViewAnimations。

Github地址:https://github.com/daimajia/AndroidViewAnimations

1:添加依赖

在项目的gradle中

dependencies {
        compile 'com.android.support:support-compat:X.X.X'   
        compile 'com.daimajia.easing:library:2.0@aar'
        compile 'com.daimajia.androidanimations:library:2.3@aar'
}

这里的版本号用你

com.android.support:appcompat 使用的版本即可。

注:1.在这可能会遇到 Could not resolve com.android.support:appcompat-v7 的问题,你去settings->Build,Execution,Deployment->Gradle,取消“offline work”的勾选,让studio去下载即可。注意后期最好在改回来,这可能会导致运行项目时gradle build running 很久.

           2.有些最新android studio可能会强制使用implementation,这里你用 compile Sync Now下载完后把compile改为implementation即可。

可能有些由于网络原因的你下载会很困难,这个请自行百度即可。

 

2:使用

YoYo.with(想要使用的动画mode)
.duration(持续时间)
.repeat(持续次数)
.pivot(YoYo.CENTER_PIVOT, YoYo.CENTER_PIVOT)
.interpolate(new AccelerateDecelerateInterpolator())
.withListener(new Animator.AnimatorListener() {
    @Override
    public void onAnimationStart(Animator animation) {

    }

    @Override
    public void onAnimationEnd(Animator animation) {
    }

    @Override
    public void onAnimationCancel(Animator animation) {
        
    }

    @Override
    public void onAnimationRepeat(Animator animation) {

    }
})
.playOn(想要使用动画的按钮view);

 

注:如果想要添加动画 在中间抖动的话只设置.pivot(YoYo.CENTER_PIVOT, YoYo.CENTER_PIVOT) 是不行的,还要设置

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    if (hasFocus) {
        rope = YoYo.with(Techniques.FadeIn).duration(时间).playOn(控件);// after start,just click mTarget view, rope is not init
    }

ps:部分手机可能出现抖动后留有屏幕残留情况,可能是屏幕渲染问题 使用invalidate()可以解决

 

For making animations more real, I created another project named Android Easing Functions which is an implementations of easing functions on Android. So, we need to dependent that project. Step 1 Gradle dependencies { compile 'com.nineoldandroids:library:2.4.0' compile 'com.daimajia.easing:library:1.0.1@aar' compile 'com.daimajia.androidanimations:library:1.1.3@aar' } Maven <dependency> <groupId>com.nineoldandroids</groupId> <artifactId>library</artifactId> <version>2.4.0</version> </dependency> <dependency> <groupId>com.daimajia.androidanimation</groupId> <artifactId>library</artifactId> <version>1.1.3</version> <type>apklib</type> </dependency> <dependency> <groupId>com.daimajia.easing</groupId> <artifactId>library</artifactId> <version>1.0.1</version> <type>apklib</type> </dependency> Eclipse Download the following jars, and copy them into your libs directory. NineOldAndroid-2.4.0 AndroidEasingFunctions-1.0.0 AndroidViewAnimations-1.1.3 Step 2 Just like play Yo-yo. YoYo.with(Techniques.Tada) .duration(700) .playOn(findViewById(R.id.edit_area)); Effects Attension Flash, Pulse, RubberBand, Shake, Swing, Wobble, Bounce, Tada, StandUp, Wave Special Hinge, RollIn, RollOut,Landing,TakingOff,DropOut Bounce BounceIn, BounceInDown, BounceInLeft, BounceInRight, BounceInUp Fade FadeIn, FadeInUp, FadeInDown, FadeInLeft, FadeInRight FadeOut, FadeOutDown, FadeOutLeft, FadeOutRight, FadeOutUp Flip FlipInX, FlipOutX, FlipOutY Rotate RotateIn, RotateInDownLeft, RotateInDownRight, RotateInUpLeft, RotateInUpRight RotateOut, RotateOutDownLeft, RotateOutDownRight, RotateOutUpLeft, RotateOutUpRight Slide SlideInLeft, SlideInRight, SlideInUp, SlideInDown SlideOutLeft, SlideOutRight, SlideOutUp, SlideOutDown Zoom ZoomIn, ZoomInDown, ZoomInLeft, ZoomInRight, ZoomInUp ZoomOut, ZoomOutDown, ZoomOutLeft, ZoomOutRight, ZoomOutUp Welcome contribute your amazing animation effect. :-D
Android中,可以使用属性动画来实现抖动动画吸引用户的注意力。以下是一个简单的示例代码: ```java // 获取需要抖动的视图 View view = findViewById(R.id.my_view); // 定义抖动动画 ObjectAnimator animator = ObjectAnimator.ofFloat(view, "rotation", -5, 5); animator.setDuration(100); animator.setRepeatCount(5); animator.setRepeatMode(ObjectAnimator.REVERSE); // 开始动画 animator.start(); ``` 这个代码会使得视图向左旋转5度,然后向右旋转5度,重复执行5次,每次执行时间为100毫秒。通过这个抖动动画,可以吸引用户的注意力。 如果想要实现中心点旋转的效果,可以在抖动动画的基础上加入平移动画,使得视图围绕中心点旋转。以下是一个修改后的示例代码: ```java // 获取需要旋转的视图 View view = findViewById(R.id.my_view); // 获取视图的中心点坐标 int cx = view.getWidth() / 2; int cy = view.getHeight() / 2; // 定义旋转动画 ObjectAnimator rotation = ObjectAnimator.ofFloat(view, "rotation", 0, 360); rotation.setDuration(2000); rotation.setInterpolator(new LinearInterpolator()); rotation.setRepeatCount(ObjectAnimator.INFINITE); // 定义平移动画 ObjectAnimator translationX = ObjectAnimator.ofFloat(view, "translationX", 0, cx); translationX.setDuration(1000); translationX.setRepeatCount(1); translationX.setRepeatMode(ObjectAnimator.REVERSE); // 合并动画 AnimatorSet animatorSet = new AnimatorSet(); animatorSet.playTogether(rotation, translationX); // 开始动画 animatorSet.start(); ``` 这个代码会使得视图围绕中心点旋转,并且在动画过程中会进行一次平移,使得视图的中心点在旋转过程中保持不变。通过这个中心点旋转的效果,可以吸引用户的注意力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值