安卓中补间动画的使用

来源:互联网 发布:淘宝如何上传照片 编辑:程序博客网 时间:2024/05/19 06:49

补间动画(Tween Animation)

  • 补间动画是通过在两个关键帧之间补充渐变的动画效果来实现的。补间动画的优点是可以节省空间。目前Android应用框架支持的补间动画效果有以下5种。具体实现在android.view.animation类库中。
  • AlphaAnimation:透明度(alpha)渐变效果,对应alpha标签。
  • TranslateAnimation:位移渐变,需要指定移动点的开始和结束坐标,对应translate标签。
  • ScaleAnimation:缩放渐变,可以指定缩放的参考点,对应scale标签。
  • RotateAnimation:旋转渐变,可以指定旋转的参考点,对应rotate标签。
  • AnimationSet:组合渐变,支持组合多种渐变效果,对应set标签。

补间动画的代码实现

  • ①AlphaAnimation(透明效果)
// 1.0f表示完全不透明,0.0f表示完全透明。AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);// 设置执行的时间alphaAnimation.setDuration(2000);// 设置重复的次数为2次,本身会执行1次,所以此处总共会执行三次alphaAnimation.setRepeatCount(2);//设置重复模式alphaAnimation.setRepeatMode(Animation.REVERSE);//表示动画执行后保持原来的样子alphaAnimation.setFillAfter(true);//开启动画,iv为ImageViewiv.startAnimation(alphaAnimation);
  • ②RotateAnimation(旋转效果)
/*这个构造方法是以左上角为坐标旋转,第一个参数表示旋转的起始角度,第二个参数表示旋转后的角度。*/RotateAnimation rotateAnimation = new RotateAnimation(0, 360);rotateAnimation.setDuration(3000);rotateAnimation.setRepeatCount(1);rotateAnimation.setRepeatMode(Animation.RESTART);iv.setAnimation(rotateAnimation);/*如要相对自身中心旋转,可用如下构造方法,前两个参数与上面相同,第三个参数表示相对于自己,第四个参数为x的坐标,此处意为相对于自己宽度的一半,后两个参数同理,为y坐标。*/RotateAnimation rotateAnimation=newRotateAnimation(0,360,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f)
  • ③ScaleAnimation(缩放效果)
/*第一个参数为x轴的起始大小,第二个参数为缩放的倍数,0.5f表示缩放一半,同理,第三第四个参数为y轴的,后几个参数为相对于谁和哪个点缩放,这里表示相对于自身中心点缩放。*/ScaleAnimation sa = new ScaleAnimation(0.0f,1.0f,0.0f,1.0f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);sa.setDuration(3000);iv.setAnimation(sa);
  • ④TranslateAnimation(位移效果)
/*第一二个参数表示x轴的起始坐标,同理三四个为y轴的,五六个参数为移动后的x坐标,同理七八个参数为移动后y轴的坐标,这里指图片向下移动父窗体的高的一半距离。*/TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT,0, Animation.RELATIVE_TO_PARENT, 0,Animation.RELATIVE_TO_PARENT, 0.2f);ta.setDuration(3000);iv.setAnimation(ta);
  • ⑤AnimationSet(组合效果)
        /*动画集合类AnimationSet */        AnimationSet set = new AnimationSet(true);        /*将其他动画效果加到动画集合类中*/        set.addAnimation(am);        set.addAnimation(sm);        set.addAnimation(rm);        /*开启全部动画*/        rl_root.startAnimation(set);

补间动画的xml实现方式

  • 在res目录下新建文件夹anim,在anim文件夹下新建xml文件,要实现什么动画,就选择什么动画,下面给出与代码实现相同效果的xml实现方法。
  • xml方式定义动画的使用方式。
/*加载uml文件,生成一个动画对象*/Animation am = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha);iv.setAnimation(am);
  • ①AlphaAnimation(透明效果)
<?xml version="1.0" encoding="utf-8"?><alpha    android:fromAlpha="0"    android:toAlpha="1"    android:duration="2000"    android:fillAfter="true"    android:repeatCount="2"    android:repeatMode="reverse"    xmlns:android="http://schemas.android.com/apk/res/android"></alpha>
  • ②RotateAnimation(旋转效果)
<?xml version="1.0" encoding="utf-8"?><rotate    android:fromDegrees="0"    android:toDegrees="360"    android:duration="3000"    android:repeatCount="1"    android:repeatMode="reverse"    android:fillAfter="true"    //这里的50%是相对自身的,如果要相对于父控件,则写成50%p    android:pivotX="50%"    android:pivotY="50%"    xmlns:android="http://schemas.android.com/apk/res/android"></rotate>
  • ③ScaleAnimation(缩放效果)
<?xml version="1.0" encoding="utf-8"?><scale    android:fromXScale="0"    android:toXScale="1"    android:fromYScale="0"    android:toYScale="1"    android:duration="3000"    android:pivotX="50%"    android:pivotY="50%"    xmlns:android="http://schemas.android.com/apk/res/android"></scale>
  • ④TranslateAnimation(位移效果)
<?xml version="1.0" encoding="utf-8"?><translate//这里表示控件向下移动相对于父控件20%的距离    android:fromXDelta="0%p"    android:toXDelta="0%p"    android:fromYDelta="0%p"    android:toYDelta="20%p"    android:duration="3000"    xmlns:android="http://schemas.android.com/apk/res/android"></translate>
  • ⑤AnimationSet(组合效果)
    组合效果很简单,对应set标签,把其他动画效果的uml标签放进去即可
<?xml version="1.0" encoding="utf-8"?><set>    <rotate    android:fromDegrees="0"    android:toDegrees="360"    android:duration="1000"    android:fillAfter="true"    android:pivotX="50%"    android:pivotY="50%"    xmlns:android="http://schemas.android.com/apk/res/android"></rotate>    <alpha    android:fromAlpha="0"    android:toAlpha="1"    android:duration="2000"    android:fillAfter="true"    xmlns:android="http://schemas.android.com/apk/res/android"></alpha>    <scale    android:fromXScale="0"    android:toXScale="1"    android:fromYScale="0"    android:toYScale="1"     android:duration="1000"    android:fillAfter="true"    android:pivotX="50%"    android:pivotY="50%"    xmlns:android="http://schemas.android.com/apk/res/android"></scale></set>

补间动画的监听事件

set.setAnimationListener(new AnimationListener() {            //动画开始            @Override            public void onAnimationStart(Animation animation) {            }            //动画重复            @Override            public void onAnimationRepeat(Animation animation) {            }            //动画结束            @Override            public void onAnimationEnd(Animation animation) {            }        });    }

注意

  • 使用动画和效果时,虽然看着控件发生里位移或旋转,但实际上控件并没有移动,它还是在原来位置。
0 0
原创粉丝点击