您的位置:首页 > 移动开发 > Android开发

Android动画控件之Animation

2015-09-20 20:06 585 查看

概述:

android的动画效果包括:移动,渐变透明度,旋转,缩放。

实现动画的方式有两种:在java代码中动态实现,在xml中静态实现。

demo

动态实现:

/*
     动画的透明度渐变
      */
     AlphaAnimation alphaAnimation = new AlphaAnimation(1f,0);//透明度从1到0
     alphaAnimation.setDuration(1000);//完成渐变的时间
     alphaAnimation.setStartOffset(200);//响应时间
     mImageViewAnim.startAnimation(alphaAnimation);//用一个ImageView加载animation,Image务必放入src或者加载过background

    /*
    动画的移动
     */
    TranslateAnimation translateAnimation =
            //从一个坐标到另一个坐标的移动,参数依次为:起始点横、纵坐标,结束点横、纵坐标
            new TranslateAnimation(-mImageViewAnim.getMeasuredWidth(),0,0,0);
    translateAnimation.setDuration(1000);
    translateAnimation.setStartOffset(200);
    mImageViewAnim.startAnimation(translateAnimation);

    /*
    动画的旋转
     */
    RotateAnimation rotateAnimation = new RotateAnimation(0,360);//默认沿着左上角旋转
    rotateAnimation.setDuration(1000);
    rotateAnimation.setStartOffset(200);
    mImageViewAnim.startAnimation(rotateAnimation);

    /*
    动画的缩放
     */
    ScaleAnimation scaleAnimation = new ScaleAnimation(1f,2f,1f,2f);//水平尺寸变化,竖直尺寸变化
    scaleAnimation.setDuration(1000);
    scaleAnimation.setStartOffset(200);
    mImageViewAnim.startAnimation(scaleAnimation);


动画的合并加载,需要一个AnimationSet,将所有的animation加载进去:

/**
      * 声明一个AnimationSet,是一个存储animation的集合
      * false:被此set加载的每个animation用自己的interpolator;
      * true:所有animation功用一个interpolator
      */
     AnimationSet animationSet = new AnimationSet(false);
     AlphaAnimation alphaAnimation = new AlphaAnimation(1f,0);
     TranslateAnimation translateAnimation =
             new TranslateAnimation(0,0,mImageViewAnim.getMeasuredWidth(),mImageViewAnim.getMeasuredHeight());
     RotateAnimation rotateAnimation = new RotateAnimation(0,360,RotateAnimation.RELATIVE_TO_SELF
             ,0.5f,RotateAnimation.RELATIVE_TO_SELF
             ,0.5f);//RELATIVE_TO_SELF的意思是位置相对于自己
     ScaleAnimation scaleAnimation = new ScaleAnimation(1f,2f,1f,2f);

     alphaAnimation.setDuration(1000);
     translateAnimation.setDuration(1000);
     rotateAnimation.setDuration(1000);
     scaleAnimation.setDuration(1000);
     //添加各个动画
     animationSet.addAnimation(alphaAnimation);
     animationSet.addAnimation(translateAnimation);
     animationSet.addAnimation(rotateAnimation);
     animationSet.addAnimation(scaleAnimation);
     mImageViewAnim.startAnimation(animationSet);


这样运行的效果就是所有动画组合咋一起的效果。

动画的静态加载,需要在res目录下新建一个文件夹anim,在里面新建一个资源文件,我的叫rotation.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/anticipate_interpolator">
    <!-- cycle_interpolator:来回车轮效果;
    accelerate_decelerate_interpolator:先加速后减速
    bounce_interpolator:这个针对平移,下落反复弹起效果
    anticipate_interpolator:准备效果-->
        <alpha
            android:fromAlpha="1"
            android:toAlpha="0"
            android:duration="2000">
            <!-- 从全显示到不显示,完成时间2000ms-->
            <!-- 从全显示到不显示,完成时间2000ms-->
        </alpha>
       <rotate
           android:fromDegrees="0"
           android:toDegrees="360"
           android:pivotX="50%"
           android:pivotY="50%"
           android:startOffset="100"
           android:duration="2000"
           android:repeatCount="0">
           <!--参数依次是:从0度到360度相对自己中心旋转,
           100ms后开始转动,每次旋转持续2000ms,旋转(0+1)次-->
       </rotate>
        <scale
            android:fromXScale="1"
            android:toXScale="2"
            android:fromYScale="1"
            android:toYScale="2"
            android:pivotX="50%"
            android:pivotY="50%"
            android:duration="2000">
            <!-- 水平从1倍大小变为两倍,竖直从1倍大小变为两倍
            以自己的中心点为轴,完成动画需要2000ms-->
        </scale>
        <translate
            android:fromXDelta="0"
            android:toXDelta="0"
            android:fromYDelta="0"
            android:toYDelta="300"
            android:duration="2000">
            <!-- 初始水平位置:0
            终止水平位置:0
            初始竖直位置:0
            终止竖直位置:300-->
        </translate>
</set>


在java代码中调用这个资源:

AnimationUtils utils = new AnimationUtils();
        Animation animation = utils.loadAnimation(getApplicationContext(),R.anim.rotation);
        mImageViewAnim.startAnimation(animation);


运行的效果是几种xml中几种动画的综合。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: