Android仿google now效果的呼吸按钮

Android仿google now效果的呼吸按钮,第1张

概述Android仿google now效果呼吸按钮 呼吸按钮是我最早接触到为view添加动画效果的需求,刚刚参加安卓开发工作,要求设计一个好看的语音按钮效果,就有了这个成果,但是后来又改方案了,所以我也就没有对该按钮进行封装为一个自定义按钮,本文主要是展示一种合理组合利用animation来实现一些好看的动画效果,只是一种思路. 先上图: 实现该效果,重要的是我们要如何实现这种动态的呼吸效果,因为是一种非线性运动,直接实现起来有些麻烦,特别是对于像我刚刚入行的菜鸟来说.但是幸好,android的SDK提供了一种叫interpolator属性,通过

呼吸按钮是我最早接触到为vIEw添加动画效果的需求,刚刚参加安卓开发工作,要求设计一个好看的语音按钮效果,就有了这个成果,但是后来又改方案了,所以我也就没有对该按钮进行封装为一个自定义按钮,本文主要是展示一种合理组合利用animation来实现一些好看的动画效果,只是一种思路。

先上图:

实现该效果,重要的是我们要如何实现这种动态的呼吸效果,因为是一种非线性运动,直接实现起来有些麻烦,特别是对于像我刚刚入行的菜鸟来说。但是幸好,androID的SDK提供了一种叫interpolator属性,通过设置该属性为accelerate_decelerate_interpolato可以实现加速效果,使动画看起来更丰满,更具活力。

首先,我们需要三个anim文件。

进入效果anim:

<?xml version="1.0" enCoding="utf-8"?>  <set xmlns:androID="http://schemas.androID.com/apk/res/androID"  androID:interpolator="@androID:anim/decelerate_interpolator"  androID:shareInterpolator="true">  <scale    androID:fromXScale="0.0"    androID:toXScale="0.9"    androID:fromYScale="0.0"    androID:toYScale="0.9"    androID:pivotX="50%"    androID:pivotY="50%"    androID:duration="1000"/></set>

呼吸效果anim:

<?xml version="1.0" enCoding="utf-8"?>  <set xmlns:androID="http://schemas.androID.com/apk/res/androID"  androID:interpolator="@androID:anim/accelerate_decelerate_interpolator"  androID:shareInterpolator="true">  <scale    androID:fromXScale="0.9"    androID:toXScale="1.0"    androID:fromYScale="0.9"    androID:toYScale="1.0"    androID:duration="1500"    androID:pivotX="50%"    androID:pivotY="50%"    androID:repeatCount="infinite"    androID:repeatMode="reverse"/></set>

退出效果anim:

 <?xml version="1.0" enCoding="utf-8"?>  <set xmlns:androID="http://schemas.androID.com/apk/res/androID"  androID:interpolator="@androID:anim/accelerate_interpolator"  androID:shareInterpolator="true">  <scale    androID:fromXScale="0.95"    androID:toXScale="0.0"    androID:fromYScale="0.95"    androID:toYScale="0.0"    androID:pivotX="50%"    androID:pivotY="50%"    androID:duration="1000"/></set>

然后是Java代码,代码很简单,在MainActivity中,对按钮设置点击事件,唤起开始动画->执行呼吸动画->唤起结束对话。同时对开始和接收的动画进行监听,执行完毕后完成显示和隐藏背景的设置。部分代码:

private voID initVIEw() {    voice.setonClickListener(new VIEw.OnClickListener() {      @OverrIDe      public voID onClick(VIEw v) {        if (!isVisible) {          back.startAnimation(animationIn);          isVisible = true;        } else {          back.startAnimation(animationExit);          isVisible = false;        }      }    });}

Animtion动画相关部分代码

 //动画监听animationIn.setAnimationListener(new Animation.AnimationListener() {      @OverrIDe      public voID onAnimationStart(Animation animation) {      }      @OverrIDe      public voID onAnimationRepeat(Animation animation) {      }      @OverrIDe      public voID onAnimationEnd(Animation animation) {        back.startAnimation(animationVoice);  //开始呼吸动画        back.setVisibility(VIEw.VISIBLE);      }    });    animationExit.setAnimationListener(new Animation.AnimationListener() {      @OverrIDe      public voID onAnimationStart(Animation animation) {      }      @OverrIDe      public voID onAnimationEnd(Animation animation) {        back.clearanimation(); //清除动画        back.setVisibility(VIEw.INVISIBLE);      }      @OverrIDe      public voID onAnimationRepeat(Animation animation) {      }    });

好了,一个呼吸按钮就成了,有兴趣的可以把呼吸按钮封装一下,做成一个自定义按钮来使用。最后附上github链接:BreathButton

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

总结

以上是内存溢出为你收集整理的Android仿google now效果的呼吸按钮全部内容,希望文章能够帮你解决Android仿google now效果的呼吸按钮所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1145787.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存