[img]
[/img]
四个动画的xml文件:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <alpha android:fromAlpha="1.0" android:toAlpha="0" android:duration="5000" /> </set> <!-- alpha标识透明度, android:fromAlpha表示动画开始时的透明度,1.0表示一点都不透明, android:toAlpha="0" 表示动画结束后的透明度,0表示完全透明, android:duration="5000"表示动画持续的时间,即从不透明到完全透明的时间为5秒钟 -->
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <translate android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="100" android:toYDelta="100" android:duration="5000" /> </set> <!-- translate表示动画中的移动 android:fromXDelta="0" android:fromYDelta="0" 表示动画开始时的位置为控件的自身位置 android:toXDelta="100" android:toYDelta="100" 表示动画结束后,x坐标 y坐标都增加100,即向右下方移动 android:duration="5000" 表示动画持续的时间-->
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <scale android:fromXScale="1.0" android:fromYScale="1.0" android:toXScale="5.0" android:toYScale="5.0" android:pivotX="50%" android:pivotY="50%" android:duration="5000" /> </set> <!-- scale动画中的缩放 android:fromXScale="1.0"表示动画开始时宽度是控件自身宽度的1.0被。 android:fromYScale="1.0"表示动画开始时高度度是控件自身高度的1.0被。 android:toXScale="5.0" 表示动画结束后宽度扩大到原来的5被。 android:toYScale="5.0" 表示动画结束后高度扩大到原来的5被。 android:pivotX="50%" android:pivotY="50%"表示以中心点为参考点进行缩放 android:duration="5000" 表示动画的持续时间为5000毫秒=5秒。 -->
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <rotate android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%" android:duration="5000" /> </set> <!-- rotate控制动画中的旋转 android:fromDegrees="0" 表示初始时的角度为0度。 android:toDegrees="360" 表示将要旋转360度。 android:pivotX="50%" android:pivotY="50%" 表示旋转时以空间本身的中心点为中心进行旋转。 android:duration="5000" 表示动画持续时间为5000毫秒 -->
MainActivity:
package com.zzl.animation; import android.app.Activity; import android.os.Bundle; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.view.animation.RotateAnimation; import android.widget.ImageView; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 1,透明动画效果 Animation animation1 = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha); // 2,移动动画效果 Animation animation2 = AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate); // 3,缩放动画效果 Animation animation3 = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale); // 4,旋转动画效果 Animation animation4 = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate); // 如果设为 true,表示设置动画完成后保持动画后的状态 animation1.setFillAfter(true); // animation2.setFillAfter(true); /** * 用代码添加一个旋转动画 */ Animation animation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); animation.setDuration(10000); ImageView iv = (ImageView) findViewById(R.id.iv); iv.setAnimation(animation); } }
Android 平台提供了两类动画。 一类是Tween动画,就是对场景里的对象不断的进行图像变化来产生动画效果(旋转、平移、放缩和渐变)。
第二类就是 Frame动画,即顺序的播放事先做好的图像,与gif图片原理类似。
下面就讲一下Tweene Animations。
AlphaAnimation
通过代码实现 AlphaAnimation,如下:
//初始化 Animation alphaAnimation = new AlphaAnimation(0.1f, 1.0f); //设置动画时间 alphaAnimation.setDuration(3000); this.startAnimation(alphaAnimation);
其中AlphaAnimation类第一个参数fromAlpha表示动画起始时的透明度, 第二个参数toAlpha表示动画结束时的透明度。
setDuration用来设置动画持续时间。
RotateAnimation
代码:
Animation rotateAnimation = new RotateAnimation(0f, 360f); rotateAnimation.setDuration(1000); this.startAnimation(rotateAnimation);
其中RotateAnimation类第一个参数fromDegrees表示动画起始时的角度, 第二个参数toDegrees表示动画结束时的角度。
另外还可以设置伸缩模式pivotXType、pivotYType, 伸缩动画相对于x,y 坐标的开始位置pivotXValue、pivotYValue等。
ScaleAnimation
代码:
//初始化 Animation scaleAnimation = new ScaleAnimation(0.1f, 1.0f,0.1f,1.0f); //设置动画时间 scaleAnimation.setDuration(500); this.startAnimation(scaleAnimation);
ScaleAnimation类中
第一个参数fromX ,第二个参数toX:分别是动画起始、结束时X坐标上的伸缩尺寸。
第三个参数fromY ,第四个参数toY:分别是动画起始、结束时Y坐标上的伸缩尺寸。
另外还可以设置伸缩模式pivotXType、pivotYType, 伸缩动画相对于x,y 坐标的开始位置pivotXValue、pivotYValue等。
TranslateAnimation
//初始化 Animation translateAnimation = new TranslateAnimation(0.1f, 100.0f,0.1f,100.0f); //设置动画时间 translateAnimation.setDuration(1000); this.startAnimation(translateAnimation);
TranslateAnimation类
第一个参数fromXDelta ,第二个参数toXDelta:分别是动画起始、结束时X坐标。
第三个参数fromYDelta ,第四个参数toYDelta:分别是动画起始、结束时Y坐标。