android动画之属性动画1

1:安卓中的动画简述

android中的几种动画:View Animation (补间动画);Drawable Animation(帧动画); Property Animation(属性动画)

1.1:帧动画的简单实用

(1):在res/anim/...xml文件下定义xml文件

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!--   
  3.     根标签为animation-list,其中oneshot代表着是否只展示一遍,设置为false会不停的循环播放动画  
  4.     根标签下,通过item标签对动画中的每一个图片进行声明  
  5.     android:duration 表示展示所用的该图片的时间长度  
  6.  -->  
  7. <animation-list  
  8.   xmlns:android="http://schemas.android.com/apk/res/android"  
  9.   android:oneshot="true"  
  10.   >  
  11.     <item android:drawable="@drawable/icon1" android:duration="150"></item>  
  12.     <item android:drawable="@drawable/icon2" android:duration="150"></item>  
  13.     <item android:drawable="@drawable/icon3" android:duration="150"></item>  
  14. </animation-list> 
(2):将其设置为view控件的背景资源,并调用如下开启动画

  1. animationDrawable = (AnimationDrawable) animationIV.getDrawable();  
  2. animationDrawable.start();  
2:补间动画

AlphaAnimation,TranslateAnimation,ScaleAnimation,RotateAnomation,AnimationSet

在xml文件中定义使用

  1. <set xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:interpolator="@android:anim/decelerate_interpolator"  
  3.     android:shareInterpolator="true" >  
  4.   
  5.     <scale  
  6.         android:duration="2000"  
  7.         android:fromXScale="0.2"  
  8.         android:fromYScale="0.2"  
  9.         android:pivotX="50%"  
  10.         android:pivotY="50%"  
  11.         android:toXScale="1.5"  
  12.         android:toYScale="1.5" />  
  13.   
  14.     <rotate  
  15.         android:duration="1000"  
  16.         android:fromDegrees="0"  
  17.         android:repeatCount="1"  
  18.         android:repeatMode="reverse"  
  19.         android:toDegrees="360" />  
  20.   
  21.     <translate  
  22.         android:duration="2000"  
  23.         android:fromXDelta="0"  
  24.         android:fromYDelta="0"  
  25.         android:toXDelta="320"  
  26.         android:toYDelta="0" />  
  27.   
  28.     <alpha  
  29.         android:duration="2000"  
  30.         android:fromAlpha="1.0"  
  31.         android:toAlpha="0.1" />  
  32. </set> 
使用方式:
  1. Animation animation = AnimationUtils.loadAnimation(this,  
  2.                 R.anim.set_demo);  
  3.         imageView.startAnimation(animation);  

3:属性动画的简单使用:

3.1:属性动画的一些概念

Duration:设置动画持续时间,默认300ms

Time Intepolation:时间插值,定义动画的变化率。

Repeate Count And Behavior:定义动画的重复次数。

Animator Sets:动画集合,定义一组动画一起执行。

ObjectAnimator,ValueAnimator,AnimatorSet;均为动画的执行类。

AnimationInflater:用于加载属性动画的xml文件

TypeEvaluation:类估值,用于设置属性动画操作的值

3.2:ObjectAnimator

  1. ImageView view;
  2.  ObjectAnimator//  
  3.          .ofFloat(view, "rotationX"0.0F, 360.0F)//  
  4.          .setDuration(500)//  
  5.          .start();  
对于ObjectAnimator

3.2.1、提供了ofInt、ofFloat、ofObject,这几个方法都是设置动画作用的元素、作用的属性、动画开始、结束、以及中间的任意个属性值。

当对于属性值,只设置一个的时候,会认为当然对象该属性的值为开始(getPropName反射获取),然后设置的值为终点。如果设置两个,则一个为开始、一个为结束~~~

动画更新的过程中,会不断调用setPropName更新元素的属性,所有使用ObjectAnimator更新某个属性,必须得有getter(设置一个属性值的时候)和setter方法~

3.2.2、如果你操作对象的该属性方法里面,比如上例的setRotationX如果内部没有调用view的重绘,则你需要自己按照下面方式手动调用。

  1. public void rotateyAnimRun(final View view)  
  2. {  
  3.     ObjectAnimator anim = ObjectAnimator//  
  4.             .ofFloat(view, "li"1.0F,  0.0F)//  li为随便设置的属性值;
  5.             .setDuration(500);//  
  6.     anim.start();  
  7.     anim.addUpdateListener(new AnimatorUpdateListener()  //在每次更新的时候设置view的属性值
  8.     {  
  9.         @Override  
  10.         public void onAnimationUpdate(ValueAnimator animation)  
  11.         {  
  12.             float cVal = (Float) animation.getAnimatedValue();  //获取刚才定义的数值
  13.             view.setAlpha(cVal);  
  14.             view.setScaleX(cVal);  
  15.             view.setScaleY(cVal);  
  16.         }  
  17.     });  
  18. }  

3.2.3:用propertyValuesHolder设置多个属性的改变

  1. public void propertyValuesHolder(View view)  
  2.     {  
  3.         PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 1f,  
  4.                 0f, 1f);  
  5.         PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleX", 1f,  
  6.                 0, 1f);  
  7.         PropertyValuesHolder pvhZ = PropertyValuesHolder.ofFloat("scaleY", 1f,  
  8.                 0, 1f);  
  9.         ObjectAnimator.ofPropertyValuesHolder(view, pvhX, pvhY,pvhZ).setDuration(1000).start();  
  10.     }  

3.3:ValueAnimator

3.3.1:基本使用方式

  1.  public void verticalRun( View view)  
  2.     {  
  3.         ValueAnimator animator = ValueAnimator.ofFloat(0, mScreenHeight  
  4.                 - view.getHeight());  
  5.         animator.setTarget(view);  //设置目标
  6.         animator.setDuration(1000).start();  
  7. //      animator.setInterpolator(value)  
  8.         animator.addUpdateListener(new AnimatorUpdateListener()  
  9.         {  
  10.             @Override  
  11.             public void onAnimationUpdate(ValueAnimator animation)  
  12.             {  
  13.                 view.setTranslationY((Float) animation.getAnimatedValue());  
  14.             }  
  15.         });  
  16.     }  
3.3.2:setEvaluation()的使用

  1. public void evaluation(View view)  
  2.     {  
  3.   
  4.         ValueAnimator valueAnimator = new ValueAnimator();  
  5.         valueAnimator.setDuration(3000);  
  6.         valueAnimator.setObjectValues(new PointF(00));  //设置自定义对象的属性值
  7.         valueAnimator.setInterpolator(new LinearInterpolator());  
  8.         valueAnimator.setEvaluator(new TypeEvaluator<PointF>()  
  9.         {  
  10.             // fraction = t / duration  
  11.             @Override  
  12.             public PointF evaluate(float fraction, PointF startValue,  
  13.                     PointF endValue)  
  14.             {  
  15.                 Log.e(TAG, fraction * 3 + "");  
  16.                 // x方向200px/s ,则y方向0.5 * 10 * t  
  17.                 PointF point = new PointF();  
  18.                 point.x = 200 * fraction * 3;  
  19.                 point.y = 0.5f * 200 * (fraction * 3) * (fraction * 3);  
  20.                 return point;  
  21.             }  
  22.         });  
  23.   
  24.         valueAnimator.start();  
  25.         valueAnimator.addUpdateListener(new AnimatorUpdateListener()  
  26.         {  
  27.             @Override  
  28.             public void onAnimationUpdate(ValueAnimator animation)  
  29.             {  
  30.                 PointF point = (PointF) animation.getAnimatedValue();  
  31.                 mBlueBall.setX(point.x);  
  32.                 mBlueBall.setY(point.y);  
  33.   
  34.             }  
  35.         });  
  36.     }  
3.3.3:添加动画监听

  1. public void animation(View view)  
  2.     {  
  3.         ObjectAnimator anim = ObjectAnimator.ofFloat(mBlueBall, "alpha"0.5f);  
  4.           
  5.         anim.addListener(new AnimatorListener()  
  6.         {  
  7.   
  8.             @Override  
  9.             public void onAnimationStart(Animator animation)  
  10.             {  
  11.                 Log.e(TAG, "onAnimationStart");  
  12.             }  
  13.   
  14.             @Override  
  15.             public void onAnimationRepeat(Animator animation)  
  16.             {  
  17.                 // TODO Auto-generated method stub  
  18.                 Log.e(TAG, "onAnimationRepeat");  
  19.             }  
  20.   
  21.             @Override  
  22.             public void onAnimationEnd(Animator animation)  
  23.             {  
  24.                 Log.e(TAG, "onAnimationEnd");  
  25.                 ViewGroup parent = (ViewGroup) mBlueBall.getParent();  
  26.                 if (parent != null)  
  27.                     parent.removeView(mBlueBall);  
  28.             }  
  29.   
  30.             @Override  
  31.             public void onAnimationCancel(Animator animation)  
  32.             {  
  33.                 // TODO Auto-generated method stub  
  34.                 Log.e(TAG, "onAnimationCancel");  
  35.             }  
  36.         });  
  37.         anim.start();  
  38.     } 
3.4:AnimatorSet的使用

  1.  public void togetherRun(View view)  
  2.     {  
  3.         ObjectAnimator anim1 = ObjectAnimator.ofFloat(mBlueBall, "scaleX",  
  4.                 1.0f, 2f);  
  5.         ObjectAnimator anim2 = ObjectAnimator.ofFloat(mBlueBall, "scaleY",  
  6.                 1.0f, 2f);  
  7.         AnimatorSet animSet = new AnimatorSet();  
  8.         animSet.setDuration(2000);  
  9.         animSet.setInterpolator(new LinearInterpolator());  
  10.         //两个动画同时执行  
  11.         animSet.playTogether(anim1, anim2);  
  12.         animSet.start();  
  13.     }  
  14.   
  15.     public void playWithAfter(View view)  
  16.     {  
  17.         float cx = mBlueBall.getX();  
  18.   
  19.         ObjectAnimator anim1 = ObjectAnimator.ofFloat(mBlueBall, "scaleX",  
  20.                 1.0f, 2f);  
  21.         ObjectAnimator anim2 = ObjectAnimator.ofFloat(mBlueBall, "scaleY",  
  22.                 1.0f, 2f);  
  23.         ObjectAnimator anim3 = ObjectAnimator.ofFloat(mBlueBall,  
  24.                 "x",  cx ,  0f);  
  25.         ObjectAnimator anim4 = ObjectAnimator.ofFloat(mBlueBall,  
  26.                 "x", cx);  
  27.           
  28.         /** 
  29.          * anim1,anim2,anim3同时执行 
  30.          * anim4接着执行 
  31.          */  
  32.         AnimatorSet animSet = new AnimatorSet();  
  33.         animSet.play(anim1).with(anim2);  
  34.         animSet.play(anim2).with(anim3);  
  35.         animSet.play(anim4).after(anim3);  
  36.         animSet.setDuration(1000);  
  37.         animSet.start();  
  38.     } 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值