Android动画控件之Animation

概述:

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中几种动画的综合。

我们猿类工作压力大,很需要有自己的乐趣,于是乎,我开通了音乐人账号,以后的作品将会上传到我的音乐人小站上。如果这篇博客帮助到您,希望您能多关注,支持,鼓励我将创作进行下去,同时也祝你能在工作和生活乐趣两发面都能出彩!

如果这篇博客帮助到您,您可以完成以下操作:
在网易云搜索“星河河”->歌手->点击进入(您将进入我的网易云音乐人账号星河河)->关注我->多听听我的歌。
豆瓣音乐人地址:https://site.douban.com/chuxinghe/ 星河河

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值