android的动画提供了旋转、移动、伸展和淡出等等效果
1、Alpha——淡入淡出
2、Scale——缩放
3、Rotate——旋转
4、Translate——移动
用java代码来实现动画效果如下:
//1表示不透明,0表示透明(淡入淡出)
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
//前四个参数表示由什么样缩放到什么样,后四个参数指明缩放后的位置(缩放)
ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.5f, 1,
0.5f, Animation.RELATIVE_TO_SELF, 0.1f,
Animation.RELATIVE_TO_SELF, 0.1f);
//前两个参数是由多少度转到多少度,后四个参数是转的圆心,RELATIVE_TO_SELF是以自己为参照物,RELATIVE_TO_PARENT是以父节点为参照物来定义的,但是都是由自己的(0,0)开始算的(旋转)
RotateAnimation mRotateUpAnim = new RotateAnimation(0.0f,
-180.0f, Animation.RELATIVE_TO_SELF, 0.2f,
Animation.RELATIVE_TO_SELF, 0.2f);
//前四个参数时表示x轴由哪移动到哪,后四个参数表示y轴有哪移动到哪(移动)
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 1.0f);
//可以将几个动画用animationset合并在一起
AnimationSet animationSet = new AnimationSet(true);
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
//设置动画执行的时间(单位:毫秒)
alphaAnimation.setDuration(1000);
//将AlphaAnimation对象添加到AnimationSet当中
animationSet.addAnimation(alphaAnimation);
ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.5f, 1, 0.5f, Animation.RELATIVE_TO_SELF, 0.1f, Animation.RELATIVE_TO_SELF, 0.1f);
scaleAnimation.setDuration(1000);
//如果值为true,控件则保持动画结束的状态
scaleAnimation.setFillAfter(true);
animationSet.addAnimation(scaleAnimation );
//使用ImageView的startAnimation方法开始执行动画
imageView.startAnimation(animationSet);
利用帧动画
Drawable序列,这些Drawable可以按照指定的时间间歇一个一个显示
在res/drawable-ldpi目录下创建anim_nv.xml文件
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/nv1" android:duration="500" />
<item android:drawable="@drawable/nv2" android:duration="500" />
<item android:drawable="@drawable/nv3" android:duration="500" />
<item android:drawable="@drawable/nv4" android:duration="500" />
</animation-list>
如要要把这个动画设置为一个图片的背景:
用java代码指定该图片的背景为该动画
imageView.setBackgroundResource(R.drawable.anim_nv);
AnimationDrawable animationDrawable = (AnimationDrawable)imageView.getBackground();
animationDrawable.start();
利用xml配置动画
1、在res文件夹下面建立一个名为anim的文件夹(控制动画的文件夹)
2、创建xml文件,首先加入set标签
3、在该标签中加入rotate、alpha、scale、translate
4、在代码中使用AnimationUtil当中装载xml文件,并生成Animation对象
android:pivotX="50" //使用绝对位置定位
android:pivotX="50%" //使用相对于控件本身定位
android:pivotX="50%p" //相对于控件的父控件定位
<?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">
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:startOffset="500"
android:duration="2000" />
<rotate android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000" />
</set>
//使用AnimationUtils装载动画设置文件
Animation animation=AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);
imageView.startAnimation(animation);