android学习之Animation(三)---AnimationSet

简介: 概述:利用AnimationSet使所有的动画效果同时显示一、AnimationSet的使用     1、它是Animation的 一个子类     2、一个AnimationSet包含了一系列的Animation     3、针对AnimationSet设置一系列的属性,可以...
概述:利用AnimationSet使所有的动画效果同时显示
一、 AnimationSet的使用
    1、它是Animation的 一个子类
    2、一个AnimationSet包含了一系列的Animation
    3、针对AnimationSet设置一系列的属性,可以被应用在AnimationSet中的所有Animation
  (1)如果在xml文件中实现动画,那么所有的动画效果都设在同一个xml文件中
       <?xml version="1.0" encoding="utf-8"?>
                    xmlns:android="http://schemas.android.com/apk/res/android"
           android:interpolator="@android:anim/accelerate_interpolator">
 
                           android:fromAlpha="0.1"
               android:toAlpha="1.0"
               android:duration="3000"
               android:startOffset="500"
           /> 
                          android:fromDegrees="0"
               android:toDegrees="+350"
               android:pivotX="50%"
               android:pivotY="50%"
               android:duration="3000"
            />
                     android:fromXScale="1.0"
            android:toXScale="0.1"
            android:fromYScale="1.0"
            android:toYScale="0.1"
            android:pivotX="50%"
            android:pivotY="50%"
            android:duration="3000"  
         />
    
 (2)如果在代码中同时实现所有动画效果
        //定义一个AnimationSet
        AnimationSet animationSet = new AnimationSet(true);
        //定义几个Animation
        AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
        ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1, 0.1f, 
                            Animation.RELATIVE_TO_SELF, 0.5f, 
                            Animation.RELATIVE_TO_SELF, 0.5f);
        //将Animation添加到AnimationSet中
        animationSet.addAnimation(alphaAnimation);
        animationSet.addAnimation(scaleAnimation);
        //为aAnimationSet添加效果,会应用在所有的Animation
        animationSet.setDuration(5000);
        animationSet.setStartOffset(3000);
        //启动AnimationSet
        imageView.startAnimation(animationSet);

  二、什么是interpolator
     1、在xml文件中的情况
               xmlns:android="http://schemas.android.com/apk/res/android"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:shareInterpolator="true"
        > 
      (1) 定义了动画变化的速率
            android:interpolator="@android:anim/accelerate_interpolator"
            accelerate_interpolator        动画开始变化速率慢,然后加速
            decelerate_interpolator       动画开始变化速率快,然后减速
            accelerate_decelerate_interpolator      动画开始和结束的变化速率慢,中间加速
            cycle_interpolator         动画播放特定次数,变化速率沿着正弦曲线
            linear_interpolator        动画匀速进行
      (2)  android:shareInterpolator="true"
            shareInterpolator为true代表应用于所有的动画
            如果是false,那么需要为每一个动画添加自己的变化速率
     2、在代码中使用interpolator
      (1)AnimationSet animationSet = new AnimationSet(true);
           true代表所有的animation共享interpolator,如果是false就需要为每一个animation添加interpolator
      (2)animationSet.setInterpolator(new AccelerateInterpolator()); 
            创造一个interpolator


三、源代码
    MainActivity.java    主程序

点击(此处)折叠或打开

  1. public class MainActivity extends Activity
  2. {
  3.     private Button testButton = null;
  4.     private ImageView imageView = null;
  5.     
  6.     @Override
  7.     protected void onCreate(Bundle savedInstanceState)
  8.     {
  9.         super.onCreate(savedInstanceState);
  10.         setContentView(R.layout.activity_main);
  11.         //找到控件
  12.         testButton = (Button)findViewById(R.id.testBtn);;
  13.         imageView = (ImageView)findViewById(R.id.imageViewId);
  14.         //为控件添加事件
  15.         testButton.setOnClickListener(new btnListener());
  16.     }
  17.     
  18.     class btnListener implements OnClickListener
  19.     {

  20.         @Override
  21.         public void onClick(View v)
  22.         {
  23.             // TODO Auto-generated method stub
  24.                 //装载xml文件中的Animation
  25.             /*
  26.                 Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.animation);
  27.                 //启动Animation,所有的效果同时发生
  28.                 imageView.startAnimation(animation);
  29.             */
  30.             //定义一个AnimationSet
  31.             AnimationSet animationSet = new AnimationSet(true);
  32.             animationSet.setInterpolator(new AccelerateInterpolator());
  33.             //定义几个Animation
  34.             AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
  35.             ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1, 0.1f,
  36.                     Animation.RELATIVE_TO_SELF, 0.5f,
  37.                     Animation.RELATIVE_TO_SELF, 0.5f);
  38.             //将Animation添加到AnimationSet中
  39.             animationSet.addAnimation(alphaAnimation);
  40.             animationSet.addAnimation(scaleAnimation);
  41.             //为aAnimationSet添加效果,会应用在所有的Animation
  42.             animationSet.setDuration(5000);
  43.             animationSet.setStartOffset(3000);
  44.             //启动AnimationSet
  45.             imageView.startAnimation(animationSet);
  46.         }
  47.         
  48.     }
  49.     @Override
  50.     public boolean onCreateOptionsMenu(Menu menu)
  51.     {
  52.         // Inflate the menu; this adds items to the action bar if it is present.
  53.         getMenuInflater().inflate(R.menu.main, menu);
  54.         return true;
  55.     }

  56. }
    animation.xml    animation动画文件

点击(此处)折叠或打开

  1. ?xml version="1.0" encoding="utf-8"?>
  2. set
  3.     xmlns:android="http://schemas.android.com/apk/res/android"
  4.     android:interpolator="@android:anim/accelerate_interpolator"
  5.     android:shareInterpolator="true"
  6.     >
  7.  
  8.     alpha
  9.         android:fromAlpha="0.1"
  10.         android:toAlpha="1.0"
  11.         android:duration="3000"
  12.         android:startOffset="500"
  13.         />
  14.     rotate
  15.            android:fromDegrees="0"
  16.            android:toDegrees="+350"
  17.            android:pivotX="50%"
  18.            android:pivotY="50%"
  19.            android:duration="3000"
  20.         />
  21.     scale
  22.         android:fromXScale="1.0"
  23.         android:toXScale="0.1"
  24.         android:fromYScale="1.0"
  25.         android:toYScale="0.1"
  26.         android:pivotX="50%"
  27.         android:pivotY="50%"
  28.         android:duration="3000"
  29.     />
  30. /set>
四、布局文件    
    activity_main.xml    activity布局文件

点击(此处)折叠或打开

  1. RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:paddingBottom="@dimen/activity_vertical_margin"
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"
  7.     android:paddingRight="@dimen/activity_horizontal_margin"
  8.     android:paddingTop="@dimen/activity_vertical_margin"
  9.     tools:context=".MainActivity" >

  10.     TextView
  11.         android:layout_width="wrap_content"
  12.         android:layout_height="wrap_content"
  13.         android:text="@string/hello_world" />
  14.     Button
  15.         android:id="@+id/testBtn"
  16.         android:layout_width="fill_parent"
  17.         android:layout_height="wrap_content"
  18.         android:layout_alignParentBottom="true"
  19.         android:text="测试动画"
  20.         />
  21.         />
  22.     ImageView
  23.         android:id="@+id/imageViewId"
  24.         android:layout_height="wrap_content"
  25.         android:layout_width="wrap_content"
  26.         android:layout_centerInParent="true"
  27.         android:layout_marginTop="100dip"
  28.         android:src="@drawable/ic_launcher"
  29.         />

  30. /RelativeLayout>



相关文章
|
XML 缓存 Android开发
Android开发,使用kotlin学习多媒体功能(详细)
Android开发,使用kotlin学习多媒体功能(详细)
427 0
|
存储 安全 Linux
Android安全启动学习(四):device-mapper-verity (dm-verity)和哈希树
Android安全启动学习(四):device-mapper-verity (dm-verity)和哈希树
1453 0
|
监控 Unix 应用服务中间件
Android-音视频学习系列-(八)基于-Nginx-搭建(rtmp、http)直播服务器
Android-音视频学习系列-(八)基于-Nginx-搭建(rtmp、http)直播服务器
|
Java Maven 开发工具
第一个安卓项目 | 中国象棋demo学习
本文是作者关于其第一个安卓项目——中国象棋demo的学习记录,展示了demo的运行结果、爬坑记录以及参考资料,包括解决Android Studio和maven相关问题的方法。
463 7
第一个安卓项目 | 中国象棋demo学习
|
Android开发
Android学习 —— 测试init.rc中的条件触发的处理顺序
Android学习 —— 测试init.rc中的条件触发的处理顺序
|
搜索推荐 Android开发
学习AOSP安卓系统源代码,需要什么样的电脑?不同配置的电脑,其编译时间有多大差距?
本文分享了不同价位电脑配置对于编译AOSP安卓系统源代码的影响,提供了从6000元到更高价位的电脑配置实例,并比较了它们的编译时间,以供学习AOSP源代码时电脑配置选择的参考。
2522 0
学习AOSP安卓系统源代码,需要什么样的电脑?不同配置的电脑,其编译时间有多大差距?
|
Web App开发 编解码 视频直播
视频直播技术干货(十二):从入门到放弃,快速学习Android端直播技术
本文详细介绍了Android端直播技术的全貌,涵盖了从实时音视频采集、编码、传输到解码与播放的各个环节。文章还探讨了直播中音视频同步、编解码器选择、传输协议以及直播延迟优化等关键问题。希望本文能为你提供有关Andriod端直播技术的深入理解和实践指导。
983 0
|
存储 定位技术 开发工具
Android 开发前的设计,Android之内存泄漏调试学习与总结
Android 开发前的设计,Android之内存泄漏调试学习与总结
|
Java Android开发 Dart
50家大厂面试万字精华总结android编程基础学习
50家大厂面试万字精华总结android编程基础学习
|
网络协议 Shell Android开发
Android 深入学习ADB调试原理(1)
Android 深入学习ADB调试原理(1)
1817 1

热门文章

最新文章