当前位置:操作系统 > 安卓/Android >>

Android中使用Animation实现控件的动画效果以及Interpolator和AnimationListener的使用

Animation的4个基本动画效果
1、AlphaAnimation:淡入淡出效果
[java] 
在代码中实现动画效果的方法: 
  
 
ImageView imageView = (ImageView) findViewById(R.id.imageView1); 
AnimationSet animationSet = new AnimationSet(true); 
AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1); 
alphaAnimation.setDuration(1000); 
alphaAnimation.setStartOffset(10000); 
animationSet.addAnimation(alphaAnimation); 
//animationSet.setStartOffset(10000); 
animationSet.setFillBefore(false); 
animationSet.setFillAfter(true); 
imageView.startAnimation(animationSet);  
 
在XML文件中实现动画效果的方法: 
  
① 在res目录下创建一个anim文件夹,在里面添加一个alpha.xml文件: 
  
<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:interpolator="@android:anim/accelerate_interpolator" 
    android:fillAfter="true" 
    android:fillBefore="false"> 
    <alpha 
        android:fromAlpha="1.0" 
        android:toAlpha="0.0" 
        android:startOffset="1000" 
        android:duration="1000" /> 
 
</set>  
② 在Activity中使用AnimationUtils获取Animation并进行设置: 
  
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha); 
imageView.startAnimation(animation); 

2、ScaleAnimation:缩放效果
[java]
在代码中实现动画效果: 
  
 
ImageView imageView = (ImageView) findViewById(R.id.imageView1); 
AnimationSet animationSet = new AnimationSet(true); 
ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.5f, 1, 0.5f, 
        Animation.RELATIVE_TO_SELF, 1f, 
        Animation.RELATIVE_TO_SELF, 1f); 
animationSet.addAnimation(scaleAnimation); 
animationSet.setDuration(1000); 
imageView.startAnimation(animationSet);  
 
在XML文件中实现动画效果的方法: 
  
① 在res的anim文件夹下,创建一个scale.xml文件: 
  
<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:interpolator="@android:anim/accelerate_interpolator"> 
 
    <scale android:fromXScale="1.0" 
        android:toXScale="0.0" 
        android:fromYScale="1.0" 
        android:toYScale="0.0" 
        android:pivotX="50%" 
        android:pivotY="50%" 
        android:duration="2000" /> 
 
</set>  
② 在Activity中使用AnimationUtils获取Animation并进行设置: 
  
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale); 
imageView.startAnimation(animation); 
3、Rotate:旋转效果
[java]
在代码中实现动画效果: 
  
 
ImageView imageView = (ImageView) findViewById(R.id.imageView1); 
AnimationSet animationSet = new AnimationSet(true); 
RotateAnimation rotateAnimation = new RotateAnimation(0, 360, 
        Animation.RELATIVE_TO_PARENT, 0.5f, 
        Animation.RELATIVE_TO_PARENT, 0.5f); 
rotateAnimation.setDuration(1000); 
animationSet.addAnimation(rotateAnimation); 
imageView.startAnimation(animationSet);  
 
在XML文件中实现动画效果的方法: 
  
① 在res的anim文件夹下,创建一个rotate.xml文件: 
  
<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:interpolator="@android:anim/accelerate_interpolator"> 
 
    <rotate android:fromDegrees="0" 
        android:toDegrees="+360" 
        android:pivotX="50%" 
        android:pivotY="50%" 
        android:duration="1000" /> 
</set>  
② 在Activity中使用AnimationUtils获取Animation并进行设置: 
  
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate); 
imageView.startAnimation(animation); 

4、 Translate:移动效果
[java]
在代码中实现动画效果: 
  
 
ImageView imageView = (ImageView) findViewById(R.id.imageView1); 
AnimationSet animationSet = new AnimationSet(true); 
TranslateAnimation translateAnimation = new TranslateAnimation( 
        Animation.RELATIVE_TO_SELF, 0f, 
        Animation.RELATIVE_TO_SELF, 1.0f, 
        Animation.RELATIVE_TO_SELF, 0f, 
        Animation.RELATIVE_TO_SELF, 1.0f); 
translateAnimation.setDuration(1000); 
animationSet.addAnimation(translateAnimation); 
imageView.startAnimation(animationSet);  
 
在XML文件中实现动画效果的方法: 
  
① 在res的anim文件夹下,创建一个translate.xml文件: 
  
<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:interpolator="@android:anim/accelerate_interpolator"> 
 
    <translate 
        android:fromXDelta="0%p" 
        android:toXDelta="100%p" 
        android:fromYDelta="0%p" 
        android:toYDelta="100%p" 
        android:duration="1000" /> 
 
</set>  
其中100%p表示相对于父空间的位置 
  
② 在Activity中使用AnimationUtils获取Animation并进行设置: 
  
Animation animation = (Animation) AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate); 
imageView.startAnimation(animation); 

也可以使用AnimationSet为一个控件添加多个动画,或者

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,