安卓开发20:动画之Animation 详细使用-主要通过java代码实现动画效果

来源:互联网 发布:畅销小说排行榜前网络 编辑:程序博客网 时间:2024/06/05 18:52

AlphaAnimation 透明效果实现:

activity_main.xml中仅仅是一个简单的图片,下面的例子都会使用这个xml:

  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.   
  11.     <!-- 用于动画的图片 -->  
  12.   
  13.     <ImageView  
  14.         android:id="@+id/imageView1"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:layout_alignParentLeft="true"  
  18.         android:layout_alignParentTop="true"  
  19.         android:layout_marginLeft="70dp"  
  20.         android:layout_marginTop="138dp"  
  21.         android:src="@drawable/jhs_button1_h" />  
  22.   
  23. </RelativeLayout>  

透明效果的java代码:
  1. package com.example.test.com;  
  2.   
  3.   
  4. import android.app.Activity;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.view.Window;  
  9. import android.view.animation.AlphaAnimation;  
  10. import android.view.animation.Animation;  
  11. import android.view.animation.DecelerateInterpolator;  
  12. import android.widget.ImageView;  
  13.   
  14. public class MainActivity extends Activity {  
  15.   
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  20.         setContentView(R.layout.activity_main);  
  21.   
  22.         ImageView imageView = (ImageView) findViewById(R.id.imageView1);  
  23.         //图片点击的时候,启动动画效果    
  24.         imageView.setOnClickListener(new OnClickListener() {  
  25.   
  26.             @Override  
  27.             public void onClick(View v) {  
  28.                 Animation anim = getAlphaAnimation();  
  29.                 v.startAnimation(anim);  
  30.             }  
  31.         });  
  32.   
  33.     }  
  34.   
  35.     /** 
  36.      * 透明效果 
  37.      * @return 
  38.      */  
  39.     public Animation getAlphaAnimation() {  
  40.         //实例化 AlphaAnimation 主要是改变透明度  
  41.         //透明度 从 1-不透明 0-完全透明   
  42.         Animation animation = new AlphaAnimation(1.0f, 0.5f);  
  43.         //设置动画插值器 被用来修饰动画效果,定义动画的变化率   
  44.         animation.setInterpolator(new DecelerateInterpolator());  
  45.         //设置动画执行时间  
  46.         animation.setDuration(1000);  
  47.         return animation;  
  48.     }  
  49.   
  50. }  

ScaleAnimation 缩放效果实现:

  1. package com.example.test.com;  
  2.   
  3.   
  4. import android.app.Activity;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.view.Window;  
  9. import android.view.animation.AlphaAnimation;  
  10. import android.view.animation.Animation;  
  11. import android.view.animation.DecelerateInterpolator;  
  12. import android.view.animation.ScaleAnimation;  
  13. import android.widget.ImageView;  
  14.   
  15. public class MainActivity extends Activity {  
  16.   
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  21.         setContentView(R.layout.activity_main);  
  22.   
  23.         ImageView imageView = (ImageView) findViewById(R.id.imageView1);  
  24.         //图片点击的时候,启动动画效果    
  25.         imageView.setOnClickListener(new OnClickListener() {  
  26.   
  27.             @Override  
  28.             public void onClick(View v) {  
  29.                 Animation scalegetAnim = getScaleAnimation();  
  30.                 v.startAnimation(scalegetAnim);  
  31.             }  
  32.         });  
  33.   
  34.     }  
  35.       
  36.     /** 
  37.      * 缩放动画 
  38.      * @return 
  39.      */  
  40.     public Animation getScaleAnimation() {  
  41.         //实例化 ScaleAnimation 主要是缩放效果  
  42.         //参数:fromX-动画开始前,x坐标   toX-动画结束后x坐标  
  43.         //fromY-动画开始前,Y坐标  toY-动画结束后Y坐标  
  44.         //pivotXType - 为动画相对于物件的X坐标的参照物   pivotXValue - 值  
  45.         //pivotYType - 为动画相对于物件的Y坐标的参照物   pivotYValue - 值  
  46.         Animation animation = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f,   
  47.                 Animation.RELATIVE_TO_SELF, 0.5f,  
  48.                 Animation.RELATIVE_TO_SELF, 0.5f);  
  49.         //设置动画插值器 被用来修饰动画效果,定义动画的变化率   
  50.         animation.setInterpolator(new DecelerateInterpolator());  
  51.         //设置动画执行时间  
  52.         animation.setDuration(1000);  
  53.         return animation;  
  54.     }  
  55.   
  56. }  

RotateAnimation 旋转效果实现:

  1. package com.example.test.com;  
  2.   
  3.   
  4. import android.app.Activity;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.view.Window;  
  9. import android.view.animation.AlphaAnimation;  
  10. import android.view.animation.Animation;  
  11. import android.view.animation.DecelerateInterpolator;  
  12. import android.view.animation.RotateAnimation;  
  13. import android.view.animation.ScaleAnimation;  
  14. import android.widget.ImageView;  
  15.   
  16. public class MainActivity extends Activity {  
  17.   
  18.     @Override  
  19.     protected void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  22.         setContentView(R.layout.activity_main);  
  23.   
  24.         ImageView imageView = (ImageView) findViewById(R.id.imageView1);  
  25.         //图片点击的时候,启动动画效果    
  26.         imageView.setOnClickListener(new OnClickListener() {  
  27.   
  28.             @Override  
  29.             public void onClick(View v) {  
  30.                 Animation rotateAnim = getRotateAnimation();  
  31.                 v.startAnimation(rotateAnim);  
  32.             }  
  33.         });  
  34.   
  35.     }  
  36.       
  37.     /** 
  38.      * 旋转 
  39.      * @return 
  40.      */  
  41.     public Animation getRotateAnimation() {  
  42.         //实例化RotateAnimation  
  43.         //以自身中心为圆心,旋转360度 正值为顺时针旋转,负值为逆时针旋转  
  44.         RotateAnimation animation = new RotateAnimation(0360,   
  45.                                         Animation.RELATIVE_TO_SELF, 0.5f,  
  46.                                         Animation.RELATIVE_TO_SELF, 0.5f);    
  47.         //设置动画插值器 被用来修饰动画效果,定义动画的变化率   
  48.         animation.setInterpolator(new DecelerateInterpolator());  
  49.         //设置动画执行时间  
  50.         animation.setDuration(1000);  
  51.         return animation;  
  52.     }  
  53.   
  54. }  

TranslateAnimation 移动效果实现:

  1. package com.example.test.com;  
  2.   
  3.   
  4. import android.app.Activity;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.view.Window;  
  9. import android.view.animation.AlphaAnimation;  
  10. import android.view.animation.Animation;  
  11. import android.view.animation.DecelerateInterpolator;  
  12. import android.view.animation.RotateAnimation;  
  13. import android.view.animation.ScaleAnimation;  
  14. import android.view.animation.TranslateAnimation;  
  15. import android.widget.ImageView;  
  16.   
  17. public class MainActivity extends Activity {  
  18.   
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  23.         setContentView(R.layout.activity_main);  
  24.   
  25.         ImageView imageView = (ImageView) findViewById(R.id.imageView1);  
  26.         //图片点击的时候,启动动画效果    
  27.         imageView.setOnClickListener(new OnClickListener() {  
  28.   
  29.             @Override  
  30.             public void onClick(View v) {  
  31.                 Animation translateAnim = getTranslateAnimation();  
  32.                 v.startAnimation(translateAnim);  
  33.             }  
  34.         });  
  35.   
  36.     }  
  37.       
  38.     /** 
  39.      * 移动 
  40.      * @return 
  41.      */  
  42.     public Animation getTranslateAnimation() {  
  43.         //实例化TranslateAnimation  
  44.         //以自身为坐标系和长度单位,从(0,0)移动到(1,1)  
  45.         TranslateAnimation animation = new   
  46.                 TranslateAnimation(  
  47.                         Animation.RELATIVE_TO_SELF, 0.0f,   
  48.                         Animation.RELATIVE_TO_SELF, 1.0f,   
  49.                         Animation.RELATIVE_TO_SELF, 0.0f,   
  50.                         Animation.RELATIVE_TO_SELF, 1.0f);    
  51.         //设置动画插值器 被用来修饰动画效果,定义动画的变化率   
  52.         animation.setInterpolator(new DecelerateInterpolator());  
  53.         //设置动画执行时间  
  54.         animation.setDuration(1000);  
  55.         return animation;  
  56.     }  
  57.   
  58. }  

AnimationSet 动画集合实现和使用:

  1. package com.example.test.com;  
  2.   
  3.   
  4. import android.app.Activity;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.view.Window;  
  9. import android.view.animation.AlphaAnimation;  
  10. import android.view.animation.Animation;  
  11. import android.view.animation.AnimationSet;  
  12. import android.view.animation.DecelerateInterpolator;  
  13. import android.view.animation.RotateAnimation;  
  14. import android.view.animation.ScaleAnimation;  
  15. import android.view.animation.TranslateAnimation;  
  16. import android.widget.ImageView;  
  17.   
  18. public class MainActivity extends Activity {  
  19.   
  20.     @Override  
  21.     protected void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  24.         setContentView(R.layout.activity_main);  
  25.   
  26.         ImageView imageView = (ImageView) findViewById(R.id.imageView1);  
  27.         //图片点击的时候,启动动画效果    
  28.         imageView.setOnClickListener(new OnClickListener() {  
  29.   
  30.             @Override  
  31.             public void onClick(View v) {  
  32.                 //AnimationSet类是一个Animation集合,里面可以许多Animation,  
  33.                 //且在AnimationSet中设置的属性适用于里面的所有Animation。  
  34.                 //参数true 则共享@Interpolator  
  35.                 AnimationSet set = new AnimationSet(true);  
  36.   
  37.                 //透明  
  38.                 Animation alphaAnim = getAlphaAnimation();  
  39.                 set.addAnimation(alphaAnim);  
  40.   
  41.                 //缩放  
  42.                 Animation scalegetAnim = getScaleAnimation();  
  43.                 set.addAnimation(scalegetAnim);  
  44.   
  45.                 //旋转  
  46.                 Animation rotateAnim = getRotateAnimation();  
  47.                 set.addAnimation(rotateAnim);  
  48.   
  49.                 //移动 上面三个动画是同时进行的,我现在需要让移动这个动画在上面的动画之后执行  
  50.                 //需要使用setStartOffset 设置动画开始的时间  
  51.                 Animation translateAnim = getTranslateAnimation();  
  52.                 translateAnim.setStartOffset(500);  
  53.                 set.addAnimation(translateAnim);  
  54.   
  55.                 v.startAnimation(set);  
  56.             }  
  57.         });  
  58.   
  59.     }  
  60.   
  61.     /** 
  62.      * 透明效果 
  63.      * @return 
  64.      */  
  65.     public Animation getAlphaAnimation() {  
  66.         //实例化 AlphaAnimation 主要是改变透明度  
  67.         //透明度 从 1-不透明 0-完全透明   
  68.         Animation animation = new AlphaAnimation(1.0f, 0.8f);  
  69.         //设置动画插值器 被用来修饰动画效果,定义动画的变化率   
  70.         animation.setInterpolator(new DecelerateInterpolator());  
  71.         //设置动画执行时间  
  72.         animation.setDuration(1000);  
  73.         return animation;  
  74.     }  
  75.   
  76.     /** 
  77.      * 缩放动画 
  78.      * @return 
  79.      */  
  80.     public Animation getScaleAnimation() {  
  81.         //实例化 ScaleAnimation 主要是缩放效果  
  82.         //参数:fromX-动画开始前,x坐标   toX-动画结束后x坐标  
  83.         //fromY-动画开始前,Y坐标  toY-动画结束后Y坐标  
  84.         //pivotXType - 为动画相对于物件的X坐标的参照物   pivotXValue - 值  
  85.         //pivotYType - 为动画相对于物件的Y坐标的参照物   pivotYValue - 值  
  86.         Animation animation = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f,  
  87.                 Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,  
  88.                 0.5f);  
  89.         //设置动画插值器 被用来修饰动画效果,定义动画的变化率   
  90.         animation.setInterpolator(new DecelerateInterpolator());  
  91.         //设置动画执行时间  
  92.         animation.setDuration(1000);  
  93.         return animation;  
  94.     }  
  95.   
  96.     /** 
  97.      * 旋转 
  98.      * @return 
  99.      */  
  100.     public Animation getRotateAnimation() {  
  101.         //实例化RotateAnimation  
  102.         //以自身中心为圆心,旋转360度  正值为顺时针旋转,负值为逆时针旋转  
  103.         RotateAnimation animation = new RotateAnimation(0360,  
  104.                 Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,  
  105.                 0.5f);  
  106.         //设置动画插值器 被用来修饰动画效果,定义动画的变化率   
  107.         animation.setInterpolator(new DecelerateInterpolator());  
  108.         //设置动画执行时间  
  109.         animation.setDuration(1000);  
  110.         return animation;  
  111.     }  
  112.   
  113.     /** 
  114.      * 移动 
  115.      * @return 
  116.      */  
  117.     public Animation getTranslateAnimation() {  
  118.         //实例化TranslateAnimation  
  119.         //以自身为坐标系和长度单位,从(0,0)移动到(1,1)  
  120.         TranslateAnimation animation = new TranslateAnimation(  
  121.                 Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,  
  122.                 1.0f, Animation.RELATIVE_TO_SELF, 0.0f,  
  123.                 Animation.RELATIVE_TO_SELF, 1.0f);  
  124.         //设置动画插值器 被用来修饰动画效果,定义动画的变化率   
  125.         animation.setInterpolator(new DecelerateInterpolator());  
  126.         //设置动画执行时间  
  127.         animation.setDuration(1000);  
  128.         return animation;  
  129.     }  
  130.   
  131. }  

Interpolator 描述动画的速率:

安卓默认的Interpolator:

AccelerateInterpolator:动画开始时比较慢,然后逐渐加速。

DecelerateInterpolator:动画开始时比较快,然后逐渐减速。

AccelerateDecelerateInterpolator:动画开始时和结束时比较慢,中间过程加速。

LinearInterpolator:动画匀速进行。

CycleInterpolator:动画循环播放指定次数,速率沿着正弦曲线改变。

DecelerateInterpolator代码:主要实现getInterpolation ,也可以自定义

  1. /* 
  2.  * Copyright (C) 2007 The Android Open Source Project 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  */  
  16.   
  17. package android.view.animation;  
  18.   
  19. import android.content.Context;  
  20. import android.content.res.TypedArray;  
  21. import android.util.AttributeSet;  
  22.   
  23. /** 
  24.  * An interpolator where the rate of change starts out quickly and  
  25.  * and then decelerates. 
  26.  * 
  27.  */  
  28. public class DecelerateInterpolator implements Interpolator {  
  29.     public DecelerateInterpolator() {  
  30.     }  
  31.   
  32.     /** 
  33.      * Constructor 
  34.      *  
  35.      * @param factor Degree to which the animation should be eased. Setting factor to 1.0f produces 
  36.      *        an upside-down y=x^2 parabola. Increasing factor above 1.0f makes exaggerates the 
  37.      *        ease-out effect (i.e., it starts even faster and ends evens slower) 
  38.      */  
  39.     public DecelerateInterpolator(float factor) {  
  40.         mFactor = factor;  
  41.     }  
  42.       
  43.     public DecelerateInterpolator(Context context, AttributeSet attrs) {  
  44.         TypedArray a =  
  45.             context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.DecelerateInterpolator);  
  46.           
  47.         mFactor = a.getFloat(com.android.internal.R.styleable.DecelerateInterpolator_factor, 1.0f);  
  48.           
  49.         a.recycle();  
  50.     }  
  51.       
  52.     public float getInterpolation(float input) {  
  53.         float result;  
  54.         if (mFactor == 1.0f) {  
  55.             result = (float)(1.0f - (1.0f - input) * (1.0f - input));  
  56.         } else {  
  57.             result = (float)(1.0f - Math.pow((1.0f - input), 2 * mFactor));  
  58.         }  
  59.         return result;  
  60.     }  
  61.       
  62.     private float mFactor = 1.0f;  
  63. }  

使用:

  1. //设置动画插值器 被用来修饰动画效果,定义动画的变化率   
  2. animation.setInterpolator(new DecelerateInterpolator());  

AnimationListener 监听器:

可以监听动画前,动画结束,动画repeat的时候的动作,对上面代码中的移动效果进行动画监听:

  1. /** 
  2.     * 移动 
  3.     * @return 
  4.     */  
  5.    public Animation getTranslateAnimation() {  
  6.        //实例化TranslateAnimation  
  7.        //以自身为坐标系和长度单位,从(0,0)移动到(1,1)  
  8.        TranslateAnimation animation = new TranslateAnimation(  
  9.                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,  
  10.                1.0f, Animation.RELATIVE_TO_SELF, 0.0f,  
  11.                Animation.RELATIVE_TO_SELF, 1.0f);  
  12.        //设置动画插值器 被用来修饰动画效果,定义动画的变化率   
  13.        animation.setInterpolator(new DecelerateInterpolator());  
  14.        //设置重复动画  
  15.        animation.setRepeatCount(2);   
  16.        //设置动画执行时间  
  17.        animation.setDuration(1000);  
  18.        //设置监听器  
  19.        animation.setAnimationListener(new AnimationListener() {  
  20.              
  21.            @Override  
  22.            public void onAnimationStart(Animation animation) {  
  23.                //动画开始前  
  24.                Toast.makeText(getBaseContext(), "Strart!", Toast.LENGTH_SHORT).show();    
  25.            }  
  26.              
  27.            @Override  
  28.            public void onAnimationRepeat(Animation animation) {  
  29.                //重复动画的时候,  
  30.                Toast.makeText(getBaseContext(), "Repeat!", Toast.LENGTH_SHORT).show();    
  31.            }  
  32.              
  33.            @Override  
  34.            public void onAnimationEnd(Animation animation) {  
  35.                // 结束动画的时候  
  36.                Toast.makeText(getBaseContext(), "End!", Toast.LENGTH_SHORT).show();    
  37.                  
  38.            }  
  39.        });  
  40.        return animation;  
  41.    }  

备注内容

动画运动完毕后,停止在动画完成的位置

  1. animation.setFillAfter(true);  
  2. animation.setFillEnabled(true);  

动画结束后,清除动画
  1. tempLayout.clearAnimation();  

动画结束后,改变View的偏移量
  1. tempLayout.offsetLeftAndRight(width); 
2 0