Android xml形式实现Tween动画
2014-05-09 20:52
441 查看
Android提供了两种动画,一种是Frame动画,这种动画的原理是顺序的播放事先准备好的图片,产生和gif图片类似的效果。
第二种动画是Tween动画,它有四种形式【旋转、缩放、移动、渐变】。
下面通过xml文件的方式实现Tween动画的四种效果。
首先在res中新建一个名称为anim的文件夹【固定的名称,不能改变】
渐变动画:alpha.xml:
旋转动画:rotate.xml:
缩放动画:scale.xml:
位移动画:translate.xml:
set.xml【这是动画的集合,就是多种动画拼凑在一起】:
MainActivity.java代码:
下载demo:http://download.csdn.net/detail/lzm1340458776/7323695
第二种动画是Tween动画,它有四种形式【旋转、缩放、移动、渐变】。
下面通过xml文件的方式实现Tween动画的四种效果。
首先在res中新建一个名称为anim的文件夹【固定的名称,不能改变】
渐变动画:alpha.xml:
<?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true" android:fromAlpha="1.0" android:toAlpha="0.5" android:duration="2000" > </alpha> <!-- 0.5表示半透明 ,1.0代表完全不透明,fillAfter表示动画播放完的效果保留最后的结果不变,duratioin 表示持续多久 -->
旋转动画:rotate.xml:
<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%" android:duration="2000"> </rotate> <!-- fromDgress表示从0度开始旋转,toDegress表示旋转360度,pivotX,pivotY表示旋转中心点的位置50%表示以自身为中心,50%p表示以父窗体为中心 -->
缩放动画:scale.xml:
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fillAfter="true" android:fromXScale="0.2" android:fromYScale="0.2" android:toXScale="2.0" android:toYScale="2.0" > </scale> <!-- fromXScale表示x轴从图片大小的0.2倍开始,toXScale表示x轴变为图片的2.0倍结束,Y轴也是这样 -->
位移动画:translate.xml:
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fillAfter="true" android:fromXDelta="0" android:fromYDelta="0" android:toYDelta="50%p" > </translate> <!-- fromXDelta 属性为动画起始时 X坐标上的位置 toXDelta 属性为动画结束时 X坐标上的位置 fromYDelta 属性为动画起始时 Y坐标上的位置 toYDelta 属性为动画结束时 Y坐标上的位置 注意: 没有指定fromXType toXType fromYType toYType 时候, 默认是以自己为相对参照物 -->
set.xml【这是动画的集合,就是多种动画拼凑在一起】:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="2000" android:fromXDelta="50%p" android:fromYDelta="0" android:repeatCount="2" android:repeatMode="reverse" android:toXDelta="50%p" android:toYDelta="50%p" > </translate> <rotate android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%" android:duration="2000"> </rotate> </set>
MainActivity.java代码:
public class MainActivity extends Activity { /*定义动画效果按钮*/ private Button alpha,rotate,scale,translate,set; /*定义操作的图片*/ private ImageView imgView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /*获取相应的控件*/ alpha = (Button) this.findViewById(R.id.my_alpha); rotate = (Button) this.findViewById(R.id.my_rotate); scale = (Button) this.findViewById(R.id.my_scale); translate = (Button) this.findViewById(R.id.my_translate); set = (Button) this.findViewById(R.id.my_set); imgView = (ImageView) this.findViewById(R.id.img); /*设置alpha按钮的监听事件*/ alpha.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { /*获取alpha动画*/ Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha); /*设置动画播放*/ imgView.startAnimation(animation); } }); /*设置rotate按钮的监听事件*/ rotate.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { /*获取rotate动画*/ Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate); /*设置动画*/ imgView.startAnimation(animation); } }); /*设置scale按钮的监听事件*/ scale.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { /*获取rotate动画*/ Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale); /*设置动画*/ imgView.startAnimation(animation); } }); /*设置translate按钮的监听事件*/ translate.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { /*获取rotate动画*/ Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate); /*设置动画*/ imgView.startAnimation(animation); } }); /*设置set按钮的监听事件*/ set.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { /*获取rotate动画*/ Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.set); /*设置动画*/ imgView.startAnimation(animation); } }); } }
下载demo:http://download.csdn.net/detail/lzm1340458776/7323695
相关文章推荐
- 【Android 基础】Animation 动画介绍和实现【帧动画和Tween动画【在代码和xml中实现】】
- 每日一学(八) Android动画---Tween动画的xml实现
- Android Tween动画之RotateAnimation实现图片不停旋转
- Android之XML方式实现渐变动画
- Android动画效果Animation之Tween实现简单动画
- Android Tween动画之RotateAnimation实现图片不停旋转
- Android Tween动画之RotateAnimation实现图片不停旋转效果实例介绍
- Android动画Animation之Tween用代码实现动画
- Android动画之Tween动画实现
- android 动画实现(一)Tween动画
- Android游戏开发学习笔记(一):tweened animation自定义动画的实现
- Android旋转动画rotate动画,xml配置set实现
- Android Tween动画之RotateAnimation实现图片360°不停旋转
- Android自定义控件:动画类(十一)----联合动画的XML实现与使用示例
- Android开发之Tween(补间动画)完全解析(上)——xml文件配置的实现
- Android Tween动画之RotateAnimation实现图片不停旋转
- android动画-tween动画实现原理
- 【Android动画】之Tween动画 (渐变、缩放、位移、旋转)代码中的实现
- Android控件Tween动画(补间动画)实现方法示例
- Android Tween动画之RotateAnimation实现图片不停旋转