Android动画学习笔记之补间动画

时间:2021-05-20

本文实例为大家分享了Android补间动画展示的具体代码,供大家参考,具体内容如下

首先看看补间动画的共同属性:

Duration:动画持续的时间(单位:毫秒)
fillAfter:设置为true,动画转化在动画被结束后被应用
fillBefore:设置为true,动画转化在动画开始前被应用
interpolator:动画插入器(加速、减速插入器)
repeatCount:动画重复的次数
repeatMode:顺序动画(restart)/倒序动画(reverse)
startOffset:动画之间时间间隔

对于动画的创建一般有两种方式:

第一种是在res/新建一个anim文件夹,然后在其下面分别建立四种动画
第二种方式是通过java代码的方式创建

在补间动画中我们通常有以下四种动画:

位移动画

创建方式一:

在anim文件下新建一个translate资源文件

<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="0" //设置从x的哪个点起,也可以设置为百分比,以控件的宽为基准 android:toXDelta="100" //设置移动到目标x点 android:fromYDelta="0" //设置从y的哪个点起,也可以设置为百分比,以控件的高为基准 android:toYDelta="0" //设置移动到目标y点 android:repeatCount="2" //动画重复两次,实际上你会发现是重复3次 //(restart模式下执行相同方向3次) //这里的意思是,在首次执行完之后再重复2次 //所以总的执行次数为 n + 1次 //如果是reverse模式下,则反序移动也算一次,所以在反序模式下 //往同一方向只有两次,加上反序的一次刚好就是3次 android:repeatMode="restart" //沿当前方向顺序移动一次,没有反序移动, //如果设置为reverse则有一次顺序/反序移动的操作, android:duration="2000" //完成该动作需要2秒 > </translate> //通过以下代码注册该动画 private void creatTranslateByInflate(){ Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate); mCircle.startAnimation(animation); }

创建方式二:(代码创建)

private void creatTranslateByCode() { TranslateAnimation animation = new TranslateAnimation(0, 100, 0, 0); animation.setDuration(2000); animation.setRepeatMode(Animation.REVERSE); animation.setRepeatCount(2); mCircle.startAnimation(animation); }

旋转动画

创建方式一:

<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0"//起始的角度数, android:toDegrees="180"//终止的角度,顺时针(起始度 < 终止的角度) 逆时针 //(起始度 > 终止的角度) //这里就是顺时针的旋转180 android:pivotX="50%" //旋转中轴的x点,50%表示以控件宽为基准,在控件的中间x点 android:pivotY="50%" //旋转中轴的y点,50%表示以控件高为基准,在控件的中间y点 android:duration="2000" android:repeatMode="reverse" android:repeatCount="2" > </rotate> //通过以下代码注册该动画 private void createRotateByInflate(){ Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate); mCircle.startAnimation(animation); }

创建方式二:(代码创建)

private void createRotateByCode() { float pivotX = mCircle.getWidth() / 2.0f; float pivotY = mCircle.getHeight() / 2.0f; RotateAnimation animation = new RotateAnimation(0, 180, pivotX, pivotY); animation.setDuration(2000); animation.setRepeatMode(Animation.REVERSE); animation.setRepeatCount(2); mCircle.startAnimation(animation); }

缩放动画

创建方式一:

<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:fromXScale="0.1"// android:toXScale="1.0" // android:fromYScale="0.1" android:toYScale="1.0" android:pivotY="50%" android:pivotX="50%" android:duration="2000" android:repeatMode="reverse" android:repeatCount="2"> </scale> //通过以下代码注册该动画 private void createScaleByInflate(){ Animation animation = AnimationUtils.loadAnimation(this, R.anim.scale); mCircle.startAnimation(animation); }

创建方式二:(代码创建)

private void createScaleByCode() { //创建动画Animation.RELATIVE_TO_PARENT 以父容器为参照物 //Animation.RELATIVE_TO_SELF 以自己为参照物, 如果以父容器为参照物会导致控件移动 float pivotX = mCircle.getWidth() / 2.0f; float pivotY = mCircle.getHeight() / 2.0f; ScaleAnimation animation = new ScaleAnimation(0.1f, 1.0f, 0.1f, 1.0f, pivotX,pivotY); animation.setDuration(2000); animation.setRepeatMode(Animation.REVERSE); animation.setRepeatCount(2); mCircle.startAnimation(animation); }

渐变动画

创建方式一:

<?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:fromAlpha="0.1" android:toAlpha="1.0" android:repeatMode="reverse" android:repeatCount="2" android:duration="2000"> </alpha> //通过以下代码注册该动画 private void createAlphaByInflate(){ Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha); mCircle.startAnimation(animation); }

创建方式二:(代码创建)

private void createAlphaByCode() { AlphaAnimation animation = new AlphaAnimation(0.1f, 1.0f); animation.setDuration(2000); animation.setRepeatMode(Animation.REVERSE); animation.setRepeatCount(2); mCircle.startAnimation(animation); }

以上的四种可以单独使用也可以结合起来使用,如果要结合起来使用的话,直接在anim文件夹下创建set集合,然后将需要结合的动画

放在一起即可

如下示例:

<?xml version="1.0" encoding="UTF-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <rotate android:toDegrees="3600" android:pivotY="50%" android:pivotX="50%" android:fromDegrees="0" android:duration="2000"/> <translate android:duration="3000" android:toYDelta="100%" android:toXDelta="150%" android:fromYDelta="-150%" android:fromXDelta="-100%"/> <alpha android:duration="5000" android:toAlpha="1.0" android:repeatMode="reverse" android:repeatCount="3" android:fromAlpha="0.5"/> </set>

基本的创建方式,以及基本属性都在这里了,至于如何实现一个具有美感的效果图,那就看个人的设计感了。

最后看看运行结果图

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

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章