属性动画

来源:互联网 发布:手把手教开淘宝店 编辑:程序博客网 时间:2024/06/03 04:51

新引入的属性动画机制已经不再是针对于View来设计的了,也不限定于只能实现移动、缩放、旋转和淡入淡出这几种动画操作,同时也不再只是一种视觉上的动画效果了。它实际上是一种不断地对值进行操作的机制,并将值赋值到指定对象的指定属性上,可以是任意对象的任意属性。所以我们仍然可以将一个View进行移动或者缩放,但同时也可以对自定义View中的Point对象进行动画操作了。我们只需要告诉系统动画的运行时长,需要执行哪种类型的动画,以及动画的初始值和结束值,剩下的工作就可以全部交给系统去完成了。

属性动画主要分为两个大块,一个是ValueAnimtor,另一个是ObjectAnimator

ValueAnimator

ValueAnimator是整个属性动画机制当中最核心的一个类,前面我们已经提到了,属性动画的运行机制是通过不断地对值进行操作来实现的,而初始值和结束值之间的动画过渡就是由ValueAnimator这个类来负责计算的。它的内部使用一种时间循环的机制来计算值与值之间的动画过渡,我们只需要将初始值和结束值提供给ValueAnimator,并且告诉它动画所需运行的时长,那么ValueAnimator就会自动帮我们完成从初始值平滑地过渡到结束值这样的效果。除此之外,ValueAnimator还负责管理动画的播放次数、播放模式、以及对动画设置监听器等,确实是一个非常重要的类。


ObjectAnimator

相比于ValueAnimator,ObjectAnimator可能才是我们最常接触到的类,因为ValueAnimator只不过是对值进行了一个平滑的动画过渡,但我们实际使用到这种功能的场景好像并不多。而ObjectAnimator则就不同了,它是可以直接对任意对象的任意属性进行动画操作的,比如说View的alpha属性。


不过虽说ObjectAnimator会更加常用一些,但是它其实是继承自ValueAnimator的,底层的动画实现机制也是基于ValueAnimator来完成的,因此ValueAnimator仍然是整个属性动画当中最核心的一个类。那么既然是继承关系,说明ValueAnimator中可以使用的方法在ObjectAnimator中也是可以正常使用的,它们的用法也非常类似


简单的介绍就到这了,接下来写代码,效果图:


简图:



布局文件:activity_main.xml:

[html] view plain copy
  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:id="@+id/id_container" >  
  6.     <LinearLayout  
  7.         android:id="@+id/btns"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:orientation="horizontal">  
  11.         <Button  
  12.             android:id="@+id/jianbian"  
  13.             android:layout_width="wrap_content"  
  14.             android:layout_height="wrap_content"  
  15.             android:layout_weight="1"  
  16.             android:text="渐变"/>  
  17.         <Button  
  18.             android:id="@+id/pingyi"  
  19.             android:layout_width="wrap_content"  
  20.             android:layout_height="wrap_content"  
  21.             android:layout_weight="1"  
  22.             android:text="平移"/>  
  23.         <Button  
  24.             android:id="@+id/xuanzhuan"  
  25.             android:layout_width="wrap_content"  
  26.             android:layout_height="wrap_content"  
  27.             android:layout_weight="1"  
  28.             android:text="旋转"/>  
  29.         <Button  
  30.             android:id="@+id/suofang"  
  31.             android:layout_width="wrap_content"  
  32.             android:layout_height="wrap_content"  
  33.             android:layout_weight="1"  
  34.             android:text="缩放"/>  
  35.         <Button  
  36.             android:id="@+id/jihe"  
  37.             android:layout_width="wrap_content"  
  38.             android:layout_height="wrap_content"  
  39.             android:layout_weight="1"  
  40.             android:text="集合"/>  
  41.   
  42.     </LinearLayout>  
  43.   
  44.     <ImageView  
  45.         android:layout_below="@id/btns"  
  46.         android:id="@+id/id_ball"  
  47.         android:layout_width="400dp"  
  48.         android:layout_height="400dp"  
  49.         android:layout_centerInParent="true"  
  50.         android:src="@mipmap/a"  
  51.         android:scaleType="centerCrop"  
  52.         android:onClick="rotateyAnimRun"  
  53.         />  
  54.   
  55. </RelativeLayout>  


MainActivity:

[html] view plain copy
  1. package com.eightgroup.rikao1014;  
  2.   
  3. import android.animation.AnimatorSet;  
  4. import android.animation.ObjectAnimator;  
  5. import android.animation.ValueAnimator;  
  6. import android.os.Bundle;  
  7. import android.support.v7.app.AppCompatActivity;  
  8. import android.view.View;  
  9. import android.widget.Button;  
  10. import android.widget.ImageView;  
  11. import android.widget.RelativeLayout;  
  12.   
  13. import butterknife.BindView;  
  14. import butterknife.ButterKnife;  
  15. import butterknife.OnClick;  
  16.   
  17. public class MainActivity extends AppCompatActivity {  
  18.   
  19.     @BindView(R.id.jianbian)  
  20.     Button jianbian;  
  21.     @BindView(R.id.pingyi)  
  22.     Button pingyi;  
  23.     @BindView(R.id.xuanzhuan)  
  24.     Button xuanzhuan;  
  25.     @BindView(R.id.suofang)  
  26.     Button suofang;  
  27.     @BindView(R.id.jihe)  
  28.     Button jihe;  
  29.     @BindView(R.id.id_container)  
  30.     RelativeLayout idContainer;  
  31.     @BindView(R.id.id_ball)  
  32.     ImageView idBall;  
  33.   
  34.     @Override  
  35.     protected void onCreate(Bundle savedInstanceState) {  
  36.         super.onCreate(savedInstanceState);  
  37.         setContentView(R.layout.activity_main);  
  38.         ButterKnife.bind(this);  
  39.   
  40.     }  
  41.   
  42.   
  43.     @OnClick({R.id.jianbian, R.id.pingyi, R.id.xuanzhuan, R.id.suofang, R.id.jihe})  
  44.     public void onViewClicked(final View view) {  
  45.         switch (view.getId()) {  
  46.             case R.id.jianbian:  
  47.                 ObjectAnimator animator = ObjectAnimator.ofFloat(idBall, "alpha", 1f, 0f, 1f);  
  48.                 animator.setDuration(1000);  
  49.                 animator.start();  
  50.                 break;  
  51.             case R.id.pingyi:  
  52.                 float curTranslationX = idBall.getTranslationX();  
  53.                 animator = ObjectAnimator.ofFloat(idBall, "translationX", curTranslationX, -500f, curTranslationX);  
  54.                 animator.setDuration(5000);  
  55.                 animator.start();  
  56.                 break;  
  57.             case R.id.xuanzhuan:  
  58.                 ObjectAnimator//  
  59.                         .ofFloat(idBall, "rotationX", 0.0F, 360.0F)//  
  60.                         .setDuration(500)//  
  61.                         .start();  
  62.                 break;  
  63.             case R.id.suofang:  
  64.                 ObjectAnimator anim = ObjectAnimator//  
  65.                         .ofFloat(idBall, "zhy", 1.0F, 0.0F)//  
  66.                         .setDuration(500);//  
  67.                 anim.start();  
  68.                 anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {  
  69.                     @Override  
  70.                     public void onAnimationUpdate(ValueAnimator animation) {  
  71.                         float cVal = (Float) animation.getAnimatedValue();  
  72.                         idBall.setAlpha(cVal);  
  73.                         idBall.setScaleX(cVal);  
  74.                         idBall.setScaleY(cVal);  
  75.                     }  
  76.                 });  
  77.                 break;  
  78.             case R.id.jihe:  
  79.                 ObjectAnimator moveIn = ObjectAnimator.ofFloat(idBall, "translationX", -500f, 0f);  
  80.                 ObjectAnimator rotate = ObjectAnimator.ofFloat(idBall, "rotation", 0f, 360f);  
  81.                 ObjectAnimator fadeInOut = ObjectAnimator.ofFloat(idBall, "alpha", 1f, 0f, 1f);  
  82.                 AnimatorSet animSet = new AnimatorSet();  
  83.                 animSet.play(rotate).with(fadeInOut).after(moveIn);  
  84.                 animSet.setDuration(5000);  
  85.                 animSet.start();  
  86.                 break;  
  87.         }  
  88.     }  
  89. }  

到这就基本上完成了,


这其中点击事件用到了ButterKnife,这个工具下一篇简单的说一说它的使用步骤,还是很简单的。
原创粉丝点击