Android中Tween动画和Frame动画实例

Animation主要有两种动画模式:Tween动画和Frame动画

Tween动画由四种类型组成

alpha
渐变透明度动画效果
scale
渐变尺寸伸缩动画效果
translate
画面转换位置移动动画效果
rotate
画面转移旋转动画效果
res目录下新建anim创建Tween.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <!-- 透明 -->  
  4.     <alpha  
  5.         android:fromAlpha="1"  
  6.         android:toAlpha="0"   
  7.         android:duration="3000"  
  8.         />  
  9.     <!-- 旋转 -->  
  10.     <rotate  
  11.         android:fromDegrees="0"  
  12.         android:toDegrees="360"  
  13.         android:pivotX="50%"  
  14.         android:pivotY="50%"   
  15.         android:duration="3000"   
  16.         />  
  17.     <!-- 缩放 -->  
  18.     <scale  
  19.         android:fromXScale="1"   
  20.         android:fromYScale="1"   
  21.         android:toXScale="3"   
  22.         android:toYScale="3"   
  23.         android:pivotX="0"  
  24.         android:pivotY="0"  
  25.         android:duration="3000"   
  26.         />  
  27.     <!-- 移动 -->  
  28.     <translate  
  29.         android:fromXDelta="0"  
  30.         android:fromYDelta="0"  
  31.         android:toXDelta="50%p"  
  32.         android:toYDelta="50%p"   
  33.         android:duration="3000"   
  34.         />  
  35. </set>  

以上每个动画效果可放在不同的xml文件中已方便查看效果

下边是Activity中调用动画

[java]  view plain copy
  1. public void onCreate(Bundle savedInstanceState) {  
  2.     super.onCreate(savedInstanceState);  
  3.     setContentView(R.layout.main);  
  4.   
  5.     imageView = (ImageView) findViewById(R.id.img);  
  6. }  
  7.   
  8. public void onClick(View view) {  
  9.     Animation animation = null;  
  10.     switch (view.getId()) {  
  11.         case R.id.alpha:  
  12.             animation = AnimationUtils.loadAnimation(this, R.anim.alpha);  
  13.             break;  
  14.         case R.id.scale:  
  15.             animation = AnimationUtils.loadAnimation(this, R.anim.scale);  
  16.             break;  
  17.         case R.id.translate:  
  18.             animation = AnimationUtils.loadAnimation(this, R.anim.translate);  
  19.             break;  
  20.         case R.id.rotate:  
  21.             //animation = AnimationUtils.loadAnimation(this, R.anim.rotate);  
  22.             //令一种方式JavaCode中 创建RotateAnimation  
  23.             animation = new RotateAnimation(0180, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);  
  24.             animation.setDuration(3000);  
  25.             break;  
  26.         case R.id.all:  
  27.             animation = AnimationUtils.loadAnimation(this, R.anim.Tween);  
  28.             break;  
  29.     }  
  30.     //启动动画  
  31.     imageView.startAnimation(animation);  
  32. }  


Tween动画由四种类型组成

帧动画是有多张图片组成,多张图片循环。

示例:

Frame.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">  
  3.     <item android:drawable="@drawable/p1" android:duration="200" />  
  4.     <item android:drawable="@drawable/p2" android:duration="200" />  
  5.     <item android:drawable="@drawable/p3" android:duration="200" />  
  6.     <item android:drawable="@drawable/p4" android:duration="200" />  
  7.     <item android:drawable="@drawable/p5" android:duration="200" />  
  8.     <item android:drawable="@drawable/p6" android:duration="200" />  
  9.     <item android:drawable="@drawable/p7" android:duration="800" />  
  10.     <item android:drawable="@drawable/p8" android:duration="200" />  
  11.     <item android:drawable="@drawable/p9" android:duration="200" />  
  12.     <item android:drawable="@drawable/p10" android:duration="200" />  
  13.     <item android:drawable="@drawable/p11" android:duration="200" />  
  14. </animation-list>  
main.xml

[html]  view plain copy
  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.     >  
  7.     <ImageView  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:src="@anim/frame"  
  11.         android:onClick="go"  
  12.         />  
  13. </LinearLayout>  

Activity:

[java]  view plain copy
  1. public void go(View view) {  
  2.     // 获取ImageView  
  3.     ImageView imageView = (ImageView) view;  
  4.     // 获取ImageView上面的动画图片  
  5.     AnimationDrawable drawable = (AnimationDrawable) imageView.getDrawable();  
  6.     // 动画开始  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值