Android动画效果 切换Activity动画

Android的动画效果分为两种,一种是tweened animation(补间动画),第二种是frame by frame animation。一般我们用的是第一种。补间动画又分为AlphaAnimation,透明度转换 RotateAnimation,旋转转换 ScaleAnimation,缩放转换 TranslateAnimation 位置转换(移动)。

       动画效果在anim目录下的xml文件中定义,在程序中用AnimationUtils.loadAnimation(Context context,int ResourcesId)载入成Animation对象,在需要显示动画效果时,执行需要动画的View的startAnimation方法,传入Animation,即可。切换Activity也可以应用动画效果,在startActivity方法后,执行overridePendingTransition方法,两个参数分别是切换前的动画效果,切换后的动画效果,下面的例子中传入的是两个alpha动画,以实现切换Activity时淡出淡入,渐隐渐现效果。

       下面贴出代码:
       两个Activity的布局文件 main.xml:


activity2.xml:

java代码:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:id="@+id/ll2"
  7. >
  8. <TextView 
  9. android:layout_width="fill_parent" 
  10. android:layout_height="wrap_content" 
  11. android:text="Activity2"
  12. />
  13. <Button android:id="@+id/bt2"
  14. android:layout_width="fill_parent" 
  15. android:layout_height="wrap_content" 
  16. android:text="返回main"
  17. />
  18. </LinearLayout> 
复制代码
动画效果XML文件,全部存放在anim目录下:
        a1.xml 淡出效果

java代码:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">
  3. <alpha
  4. android:fromAlpha="1.0"
  5. android:toAlpha="0.0"
  6. android:duration="500"
  7. />
  8. </set>
  9. <!-- 
  10. fromAlpha:开始时透明度
  11. toAlpha:结束时透明度
  12. duration:动画持续时间
  13. --> 
复制代码
a2.xml 淡入效果:

java代码:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">
  3. <alpha
  4. android:fromAlpha="0.0"
  5. android:toAlpha="1.0"
  6. android:duration="500"
  7. />
  8. </set> 
复制代码
rotate.xml 旋转效果:

java代码:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">
  3. <rotate
  4. android:interpolator="@android:anim/accelerate_decelerate_interpolator"
  5. android:fromDegrees="300"
  6. android:toDegrees="-360"
  7. android:pivotX="10%"
  8. android:pivotY="100%"
  9. android:duration="10000" />
  10. </set>
  11. <!-- 
  12. fromDegrees开始时的角度
  13. toDegrees动画结束时角度
  14. pivotX,pivotY不太清楚,看效果应该是定义旋转的圆心的
  15. --> 
复制代码
  scale.xml 缩放效果:

java代码:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">
  3. <scale 
  4. android:interpolator= "@android:anim/decelerate_interpolator" 
  5. android:fromXScale="0.0" 
  6. android:toXScale="1.5" 
  7. android:fromYScale="0.0" 
  8. android:toYScale="1.5" 
  9. android:pivotX="50%" 
  10. android:pivotY="50%" 
  11. android:startOffset="0" 
  12. android:duration="10000"
  13. android:repeatCount="1" 
  14. android:repeatMode="reverse"
  15. /> 
  16. </set>
  17. <!-- 
  18. interpolator指定动画插入器,常见的有加速减速插入器accelerate_decelerate_interpolator,加速插入器accelerate_interpolator,减速插入器decelerate_interpolator。
  19. fromXScale,fromYScale,动画开始前X,Y的缩放,0.0为不显示,1.0为正常大小
  20. toXScale,toYScale,动画最终缩放的倍数,1.0为正常大小,大于1.0放大
  21. pivotX,pivotY动画起始位置,相对于屏幕的百分比,两个都为50%表示动画从屏幕中间开始
  22. startOffset,动画多次执行的间隔时间,如果只执行一次,执行前会暂停这段时间,单位毫秒
  23. duration,一次动画效果消耗的时间,单位毫秒,值越小动画速度越快
  24. repeatCount,动画重复的计数,动画将会执行该值+1次
  25. repeatMode,动画重复的模式,reverse为反向,当第偶次执行时,动画方向会相反。restart为重新执行,方向不变
  26. -->
复制代码

       translate.xml 移动效果:

java代码:

复制代码

       下面是程序代码,main.java:

java代码:

复制代码

       activity2.java:

java代码:
  1. import android.app.Activity;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.view.View.OnClickListener;
  6. import android.widget.Button;

  7. public class activity2 extends Activity {
  8. Button bt2;
  9. public void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity2);
  12. bt2=(Button)findViewById(R.id.bt2);
  13. bt2.setOnClickListener(new OnClickListener(){

  14. @Override
  15. public void onClick(View v) {
  16. // TODO Auto-generated method stub
  17. Intent intent=new Intent(activity2.this,main.class);
  18. startActivity(intent);
  19. overridePendingTransition(R.anim.a2,R.anim.a1);
  20. }

  21. });
  22. }

复制代码


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值