当前位置: 移动技术网 > 移动技术>移动开发>Android > Android编程中Tween动画和Frame动画实例分析

Android编程中Tween动画和Frame动画实例分析

2019年07月24日  | 移动技术网移动技术  | 我要评论
本文实例讲述了android编程中tween动画和frame动画实现方法。分享给大家供大家参考,具体如下: animation主要有两种动画模式:tween动画和fram

本文实例讲述了android编程中tween动画和frame动画实现方法。分享给大家供大家参考,具体如下:

animation主要有两种动画模式:tween动画和frame动画

tween动画由四种类型组成

alpha
渐变透明度动画效果
scale
渐变尺寸伸缩动画效果
translate
画面转换位置移动动画效果
rotate
画面转移旋转动画效果

res目录下新建anim创建tween.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
 <!-- 透明 -->
 <alpha
 android:fromalpha="1"
 android:toalpha="0"
 android:duration="3000"
 />
 <!-- 旋转 -->
 <rotate
 android:fromdegrees="0"
 android:todegrees="360"
 android:pivotx="50%"
 android:pivoty="50%"
 android:duration="3000"
 />
 <!-- 缩放 -->
 <scale
 android:fromxscale="1"
 android:fromyscale="1"
 android:toxscale="3"
 android:toyscale="3"
 android:pivotx="0"
 android:pivoty="0"
 android:duration="3000"
 />
 <!-- 移动 -->
 <translate
 android:fromxdelta="0"
 android:fromydelta="0"
 android:toxdelta="50%p"
 android:toydelta="50%p"
 android:duration="3000"
 />
</set>

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

下边是activity中调用动画

public void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.main);
 imageview = (imageview) findviewbyid(r.id.img);
}
public void onclick(view view) {
 animation animation = null;
 switch (view.getid()) {
 case r.id.alpha:
  animation = animationutils.loadanimation(this, r.anim.alpha);
  break;
 case r.id.scale:
  animation = animationutils.loadanimation(this, r.anim.scale);
  break;
 case r.id.translate:
  animation = animationutils.loadanimation(this, r.anim.translate);
  break;
 case r.id.rotate:
  //animation = animationutils.loadanimation(this, r.anim.rotate);
  //令一种方式javacode中 创建rotateanimation
  animation = new rotateanimation(0, 180, rotateanimation.relative_to_self, 0.5f, rotateanimation.relative_to_self, 0.5f);
  animation.setduration(3000);
  break;
 case r.id.all:
  animation = animationutils.loadanimation(this, r.anim.tween);
  break;
 }
 //启动动画
 imageview.startanimation(animation);
}

tween动画由四种类型组成

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

示例:

frame.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
 <item android:drawable="@drawable/p1" android:duration="200" />
 <item android:drawable="@drawable/p2" android:duration="200" />
 <item android:drawable="@drawable/p3" android:duration="200" />
 <item android:drawable="@drawable/p4" android:duration="200" />
 <item android:drawable="@drawable/p5" android:duration="200" />
 <item android:drawable="@drawable/p6" android:duration="200" />
 <item android:drawable="@drawable/p7" android:duration="800" />
 <item android:drawable="@drawable/p8" android:duration="200" />
 <item android:drawable="@drawable/p9" android:duration="200" />
 <item android:drawable="@drawable/p10" android:duration="200" />
 <item android:drawable="@drawable/p11" android:duration="200" />
</animation-list>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 >
 <imageview
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:src="@anim/frame"
 android:onclick="go"
 />
</linearlayout>

activity:

public void go(view view) {
 // 获取imageview
 imageview imageview = (imageview) view;
 // 获取imageview上面的动画图片
 animationdrawable drawable = (animationdrawable) imageview.getdrawable();
 // 动画开始
 drawable.start();
}

希望本文所述对大家android程序设计有所帮助。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网