Android 之属性动画 Animator

PetLutz 3年前
   <p>Android 3.0之前已有动画框架Animation(详见: Android之视图动画Animation ),但存在一些局限性,当某个元素发生视图动画后,其响应事件位置还在动画前的地方。于是3.0之后,Google提出了属性动画。</p>    <h2><strong>ObjectAnimator</strong></h2>    <pre>  <code class="language-java">ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(imageView, "translationX", 300);  objectAnimator1.setInterpolator(new AccelerateInterpolator());  objectAnimator1.setDuration(2000);  objectAnimator.setRepeatCount(ValueAnimator.INFINITE);//Animation.INFINITE 表示重复多次  objectAnimator.setRepeatMode(ValueAnimator.RESTART);//RESTART表示从头开始,REVERSE表示从末尾倒播  objectAnimator1.start();</code></pre>    <p>第一个参数:操纵的view</p>    <p>第二个参数:操纵的动画属性值</p>    <p>第三个参数:可变数组参数</p>    <h2><strong>动画属性值</strong></h2>    <p>translationX和translationY:增量控制view从它布局容器左上角坐标偏移</p>    <pre>  <code class="language-java">ObjectAnimator.ofFloat(imageView, "translationX", 300f);</code></pre>    <p>rotation、rotationX、rotationY:控制view绕支点进行2D或3D旋转</p>    <pre>  <code class="language-java">ObjectAnimator.ofFloat(imageView, "rotation", 360);</code></pre>    <p>scaleX、scaleY:控制view绕支点进行2D缩放</p>    <pre>  <code class="language-java">ObjectAnimator.ofFloat(imageView, "scaleX", 1f, 0.5f,1f);</code></pre>    <p>alpha:控制view透明度,默认是1(不透明),0完全透明(不可见)</p>    <pre>  <code class="language-java">ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0.5f);</code></pre>    <p>x和y:描述view在容器最终位置</p>    <h2><strong>可变数组参数</strong></h2>    <p>可以有一个到N个,如果是一个值的话默认这个值是动画过渡值的结束值。如果有N个值,动画就在这N个值之间过渡。</p>    <h2><strong>动画监听</strong></h2>    <pre>  <code class="language-java">ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(imageView, "alpha", 0.5f, 1f);  objectAnimator1.addListener(new Animator.AnimatorListener() {      @Override      public void onAnimationStart(Animator animation) {        }        @Override      public void onAnimationEnd(Animator animation) {        }        @Override      public void onAnimationCancel(Animator animation) {        }        @Override      public void onAnimationRepeat(Animator animation) {        }  });</code></pre>    <p>一般我们只关心onAnimationEnd,所以Android提供了AnimatorListenerAdapter:</p>    <pre>  <code class="language-java">ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(imageView, "alpha", 0.5f, 1f);   objectAnimator1.addListener(new AnimatorListenerAdapter() {       @Override       public void onAnimationEnd(Animator animation) {           super.onAnimationEnd(animation);       }   });</code></pre>    <h2><strong>ValueAnimator</strong></h2>    <p>ValueAnimator 本身不提供任何动画效果,像个数值 发生器,用来产生具有一点规律数字。</p>    <pre>  <code class="language-java">ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 100);  valueAnimator.setTarget(imageView);  valueAnimator.setDuration(2000).start();  valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {      @Override      public void onAnimationUpdate(ValueAnimator animation) {          Int value = (Integer) animation.getAnimatedValue();          //TODO use the value          Toast.makeText(getApplicationContext(), "value=" + value, Toast.LENGTH_LONG).show();      }  });</code></pre>    <h2><strong>PropertyValuesHolder</strong></h2>    <p>针对同一个对象多个属性,同时作用多种动画</p>    <pre>  <code class="language-java">PropertyValuesHolder propertyValuesHolder1 = PropertyValuesHolder.ofFloat("translationX", 300f);  PropertyValuesHolder propertyValuesHolder2 = PropertyValuesHolder.ofFloat("alpha", 1f, 0.5f);  PropertyValuesHolder propertyValuesHolder3 = PropertyValuesHolder.ofFloat("scaleX", 1f, 0, 1f);  PropertyValuesHolder propertyValuesHolder4 = PropertyValuesHolder.ofFloat("scaleY", 1f, 0, 1f);  ObjectAnimator.ofPropertyValuesHolder(imageView, propertyValuesHolder1, propertyValuesHolder2, propertyValuesHolder3, propertyValuesHolder4)          .setDuration(5000).start();</code></pre>    <h2><strong>AnimatorSet</strong></h2>    <p>与PropertyValuesHolder类似,但AnimatorSet多了playTogether(同时执行)、playSequentially(顺序执行)、play(objectAnimator1).with(objectAnimator2)、before、after这些方法协同工作。</p>    <pre>  <code class="language-java">ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0.5f);  ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(imageView, "translationY", 300);  ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(imageView, "scaleX", 1f, 0, 1f);  AnimatorSet animatorSet = new AnimatorSet();  animatorSet.setDuration(5000);  animatorSet.playTogether(objectAnimator1, objectAnimator2,objectAnimator3);  animatorSet.start();</code></pre>    <h2><strong>xml使用属性动画</strong></h2>    <p>res下建立animator文件夹,然后建立res/animator/set_animator.xml</p>    <pre>  <code class="language-java"><?xml version="1.0" encoding="utf-8"?>  <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"  android:duration="2000"  android:propertyName="alpha"  android:valueFrom="0.1"  android:valueTo="1.0"  android:valueType="floatType" /></code></pre>    <p>调用:</p>    <pre>  <code class="language-java">Animator animator = AnimatorInflater.loadAnimator(getApplicationContext(), R.animator.set_animator);  animator.setTarget(imageView);  animator.start();</code></pre>    <p>动画组合</p>    <p>set标签,有一个orderring属性设置为together,还有另一个值:sequentially(表示一个接一个执行)。</p>    <pre>  <code class="language-java"><?xml version="1.0" encoding="utf-8"?>  <set xmlns:android="http://schemas.android.com/apk/res/android"      android:ordering="together">      <objectAnimator          android:duration="1000"          android:propertyName="scaleX"          android:valueFrom="1"          android:valueTo="0.5" />      <objectAnimator          android:duration="1000"          android:propertyName="scaleY"          android:valueFrom="1"          android:valueTo="0.5" />  </set></code></pre>    <h2><strong>View的animate方法</strong></h2>    <p>Android 3.0后,谷歌给View增加animate方法直接驱动属性动画。</p>    <pre>  <code class="language-java">imageView.animate()   .alpha(0.5f)   .y(300)   .setDuration(2000)   //api min is 16   .withStartAction(new Runnable() {       @Override       public void run() {         }   })   //api min is 16   .withEndAction(new Runnable() {       @Override       public void run() {         }   })   .start();</code></pre>    <h2><strong>布局动画</strong></h2>    <p>设置子View过渡动画</p>    <pre>  <code class="language-java"><?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:id="@+id/parentLayout"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:orientation="vertical"      android:padding="@dimen/activity_vertical_margin">      <ImageView          android:id="@+id/imageMove"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:background="@mipmap/ic_launcher" />  </LinearLayout></code></pre>    <pre>  <code class="language-java">LinearLayout parentLayout = (LinearLayout) findViewById(R.id.parentLayout);   ScaleAnimation scaleAnimation=new ScaleAnimation(0,1,0,1);   scaleAnimation.setDuration(2000);   LayoutAnimationController layoutAnimationController=new LayoutAnimationController(scaleAnimation,0.5f);   layoutAnimationController.setOrder(LayoutAnimationController.ORDER_NORMAL);   parentLayout.setLayoutAnimation(layoutAnimationController);</code></pre>    <h2> </h2>    <p style="text-align:center"> </p>    <p> </p>    <p> </p>    <p>来自:http://mp.weixin.qq.com/s?__biz=MzI4MTQyNDg3Mg==&mid=2247483687&idx=1&sn=964ec9f4c681cd6bdade8b65d7399222#rd</p>    <p> </p>