Android帧动画与补间动画
2015-11-06 18:07
603 查看
动画
帧动画Drawable Animation, frame Animation 类似于电影胶片,一组图片快速的播放,显示出来一个动画效果
官方的API文档
使用:1. 在项目中的res文件夹下的drawable放置帧动画的资源文件,创建一个xml资源文件,XML文件包含一个< animation-list >元素作为根节点和一系列子< item >节点,每个定义一个框架:框架和框架的可拉的资源持续时间。下面是一个示例XML文件为可拉的动画,
指定android:oneshot=""表示设置循环 为true表示开启循环,为false表示不循环:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true"> <item android:drawable="@drawable/rocket_thrust1" 表示当前这幅画面停留的时间 android:duration="200" /> <item android:drawable="@drawable/rocket_thrust2" android:duration="200" /> <item android:drawable="@drawable/rocket_thrust3" android:duration="200" /> </animation-list>
2.代码中播放动画
setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.iv); iv.setBackgroundResource(R.drawable.girl); //获取到动画资源对象 girlAnimation = (AnimationDrawable) iv.getBackground();//可能在子线程花费时间。 girlAnimation.start(); new Thread(){ public void run() { try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } }; }.start();
补间动画
private ImageView iv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = ((ImageView) findViewById(R.id.iv)); } /** * 透明度 * * @param v */ public void alpha(View v) { //获取透明度动画对象,参数是从哪个透明度到哪个透明度 0完全透明 1.0 AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1.0f); //设置持续时间 alphaAnimation.setDuration(2000); //设置重复次数 alphaAnimation.setRepeatCount(5); //设置循环播放模式 ResTaRT 重头播放 //Reverse 倒叙播放 alphaAnimation.setRepeatMode(Animation.RESTART); //设置动画 iv.startAnimation(alphaAnimation); } /** * 旋转 * * @param v */ public void rotate(View v) { //构造方法参数:开始的角度 结束的角度 RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); ra.setDuration(2000); ra.setRepeatCount(5); iv.startAnimation(ra); } /** * 位移 * * @param v */ public void trans(View v) { TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0.5f); ta.setRepeatCount(5); ta.setDuration(2000); iv.startAnimation(ta); } /** * 缩放 * * @param v */ public void scale(View v) { //参数:开始的x的比例,开始的y比例,缩放的x类型,缩放的中心点x,y的类型,缩放的中心点y ScaleAnimation sa = new ScaleAnimation(0, 0.5f, 0, 0.5f, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0.5f); sa.setRepeatCount(5); sa.setDuration(2000); //设置播放前后的效果,注意,这里会覆盖,只会生效最后设置的 sa.setFillAfter(true); sa.setFillBefore(false); sa.setRepeatMode(Animation.RESTART); iv.startAnimation(sa); } /** * 设置动画集合 * @param v */ public void setAnimation(View v) { //动画集合 参数:false 表示先快后慢 true可以自定义动画播放速度 AnimationSet set = new AnimationSet(false); AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1.0f); //设置持续时间 alphaAnimation.setDuration(2000); //设置重复次数 alphaAnimation.setRepeatCount(5); //设置循环播放模式 ResTaRT 重头播放 //Reverse 倒叙播放 alphaAnimation.setRepeatMode(Animation.RESTART); //构造方法参数:开始的角度 结束的角度 RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); ra.setDuration(2000); ra.setRepeatCount(5); ScaleAnimation sa = new ScaleAnimation(0, 0.5f, 0, 0.5f, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0.5f); sa.setRepeatCount(5); sa.setDuration(2000); //设置播放前后的效果,注意,这里会覆盖,只会生效最后设置的 sa.setFillAfter(true); sa.setFillBefore(false); sa.setRepeatMode(Animation.RESTART); TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0.5f); ta.setRepeatCount(5); ta.setDuration(2000); set.addAnimation(alphaAnimation); set.addAnimation(ra); set.addAnimation(sa); set.addAnimation(ta); iv.startAnimation(set); }
xml定义补间动画
动画一般都是需要复用的,我们可以在res文件夹下定义一个动画的文件夹anim在anim文件夹下可以创建我们的动画xml文件 alpha.xml
<?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:fromAlpha="0" android:toAlpha="1.0" android:duration="2000" android:repeatCount="2" android:repeatMode="reverse" > </alpha>
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%p" android:pivotY="50%p" android:duration="2000" android:repeatCount="2" android:repeatMode="reverse" > </rotate>
scale.xml
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:fromXScale="0.1" android:toXScale="2.0" android:fromYScale="0.1" android:toYScale="2.0" android:pivotX="50%" android:pivotY="50%" android:duration="2000" android:repeatCount="2" android:fillAfter="true" android:repeatMode="reverse" > </scale>
set.xml
<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:repeatCount="2" android:repeatMode="reverse" android:toDegrees="360"
>
<scale xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fillAfter="true" android:fromXScale="0.1" android:fromYScale="0.1" android:pivotX="50%" android:pivotY="50%" android:repeatCount="2" android:repeatMode="reverse" android:toXScale="2.0" android:toYScale="2.0" > </scale> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fromXDelta="-50%p" android:fromYDelta="-50%p" android:repeatCount="2" android:repeatMode="reverse" android:toXDelta="50%p" android:toYDelta="50%p" > </translate> </set>
trans.xml
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" 这里加p表示parent相对于父亲 android:fromXDelta="-50%p" android:toXDelta="50%p" android:fromYDelta="-50%p" android:toYDelta="50%p" android:duration="2000" android:repeatCount="2" android:repeatMode="reverse" > </translate>
书写动画的xml语法就是:
创建相应的动画 动画节点下 android:动画的属性名= value
代码中加载动画文件:
Animation aa = AnimationUtils.loadAnimation(this, R.anim.alpha); iv.startAnimation(aa);
相关文章推荐
- android中的数据保存
- Android 多渠道打包详细教程(一)-ant
- Android studio 快捷键
- Android 4.03 编译系统------lunch
- Android---ViewPager循环滑动+自动滑动
- android_01Activity_知识小结
- 判断当前android系统的尺寸。用来区分平板和手机,亲测。
- 【Android】如何调节屏幕亮度,关闭屏幕
- 详解:Android嵌套滑动机制 (NestedScrolling)
- 详解:Android嵌套滑动机制 (NestedScrolling)
- Android 之 内存管理
- 【Android】Broadcast控制音乐暂停继续等
- [Android] ImageView.ScaleType设置图解
- Android studio常用快捷键
- Android 从java字节码告诉你 为什么Handler会造成内存泄露
- Android RSA加密对象数据
- Android开发的那些坑和小技巧
- Android控件之Spinner
- Android初学习 - 国际化
- android开发--Activity之间的跳转实现、activity传参