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>



相关文章
|
1月前
|
XML 缓存 Android开发
Android开发,使用kotlin学习多媒体功能(详细)
Android开发,使用kotlin学习多媒体功能(详细)
96 0
|
3月前
|
安全 Linux Android开发
Android安全启动学习(一):AVB校验是什么?
Android安全启动学习(一):AVB校验是什么?
98 0
|
3月前
|
存储 安全 Linux
Android安全启动学习(四):device-mapper-verity (dm-verity)和哈希树
Android安全启动学习(四):device-mapper-verity (dm-verity)和哈希树
121 0
|
3月前
|
开发框架 物联网 数据库
89个android学习样例源码
89个android学习样例源码
76 0
|
3月前
|
Java Android开发 C++
2023安卓逆向 -- JNI学习(从开发到反编译)
2023安卓逆向 -- JNI学习(从开发到反编译)
21 0
|
3月前
|
Android开发
Android源码学习(五):AVB2.0-libavb库介绍2
Android源码学习(五):AVB2.0-libavb库介绍2
101 0
|
3月前
|
安全 算法 Android开发
Android安全启动学习(五):Android Verified Boot 2.0
Android安全启动学习(五):Android Verified Boot 2.0
212 0
|
3月前
|
存储 安全 Android开发
Android安全启动学习(三):AVB校验的内容、怎么校验、AVB的作用
Android安全启动学习(三):AVB校验的内容、怎么校验、AVB的作用
142 0
|
3月前
|
存储 缓存 安全
Android安全启动学习(二):android镜像有什么?
Android安全启动学习(二):android镜像有什么?
82 0
|
4月前
|
Android开发
安卓手机快速过检测完成某某学习
安卓手机快速过检测完成某某学习
22 0