一.
补间(View)动画
Tween Animation:(透明,旋转,缩放,平移,动画集合).
创建方式2种.
<1>.java代码:
//fromXType:
//Animation.RELATIVE_TO_SELF:0.5f--->控件自身x轴的起点值+0.5*自身的宽度
//Animation.RELATIVE_TO_PARENT,0.5f--->自身起点的值+0.5*控件父窗体的宽度
RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 1f, Animation.RELATIVE_TO_SELF, 1f);
ra.setDuration(3000);
ra.setRepeatCount(2);
ra.setRepeatMode(Animation.REVERSE);
ra.setFillAfter(true);
ivHouzi.startAnimation(ra);
创建方式2种.
<1>.java代码:
//fromXType:
//Animation.RELATIVE_TO_SELF:0.5f--->控件自身x轴的起点值+0.5*自身的宽度
//Animation.RELATIVE_TO_PARENT,0.5f--->自身起点的值+0.5*控件父窗体的宽度
RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 1f, Animation.RELATIVE_TO_SELF, 1f);
ra.setDuration(3000);
ra.setRepeatCount(2);
ra.setRepeatMode(Animation.REVERSE);
ra.setFillAfter(true);
ivHouzi.startAnimation(ra);
动画集合:
//参数:是否共用同一个插值器效果.
AnimationSet set = new AnimationSet(true);
set.addAnimation(ta);
//参数:是否共用同一个插值器效果.
AnimationSet set = new AnimationSet(true);
set.addAnimation(ta);
<2>.xml文件:
在res目录下建立一个anim文件夹,然后建立对应的xml文件.
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fromXDelta="0"
android:fromYDelta="0"
android:repeatCount="2"
android:interpolator="@android:anim/anticipate_overshoot_interpolator"
android:toXDelta="200"
android:toYDelta="200">
</translate>
然后在代码中:
//补间动画资源引用的方式:
Animation ta = AnimationUtils.loadAnimation(this, R.anim.trans_animation);
ivHouzi.startAnimation(ta);
在res目录下建立一个anim文件夹,然后建立对应的xml文件.
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fromXDelta="0"
android:fromYDelta="0"
android:repeatCount="2"
android:interpolator="@android:anim/anticipate_overshoot_interpolator"
android:toXDelta="200"
android:toYDelta="200">
</translate>
然后在代码中:
//补间动画资源引用的方式:
Animation ta = AnimationUtils.loadAnimation(this, R.anim.trans_animation);
ivHouzi.startAnimation(ta);
二.帧动画Frame Animation:
步骤:
①.建立一个drawable-hdpi文件夹,放置多张图片;
②.在drawable目录,建立一个xml文件,该文件的根节点,animation-list.
在这个文件中,写多个item:drawable和duration.
③.把这个xml文件作为某个控件的background.
④.在java代码中,AnimationDrawable drawable=(AnimationDrawable)iv.getBackground();
drawable.start();
步骤:
①.建立一个drawable-hdpi文件夹,放置多张图片;
②.在drawable目录,建立一个xml文件,该文件的根节点,animation-list.
在这个文件中,写多个item:drawable和duration.
③.把这个xml文件作为某个控件的background.
④.在java代码中,AnimationDrawable drawable=(AnimationDrawable)iv.getBackground();
drawable.start();
三.属性动画(Property Animation):
通过改变控件自身的属性,来使得该控件动起来.
通过改变控件自身的属性,来使得该控件动起来.
使用方法:
<1>.java代码创建:
ObjectAnimator alpha = ObjectAnimator.ofFloat(ivShow, "alpha", 0, 0.2f, 0, 0.4f, 0, 0.8f, 1);
alpha.setRepeatCount(2);
alpha.setDuration(3000);
alpha.start();
<1>.java代码创建:
ObjectAnimator alpha = ObjectAnimator.ofFloat(ivShow, "alpha", 0, 0.2f, 0, 0.4f, 0, 0.8f, 1);
alpha.setRepeatCount(2);
alpha.setDuration(3000);
alpha.start();
<2>.xml文件创建:
在res/文件夹下,创建一个animator文件夹,然后创建一个根节点是objectAnimator的xml文件.
在res/文件夹下,创建一个animator文件夹,然后创建一个根节点是objectAnimator的xml文件.
//加载一个动画资源
Animator animator = AnimatorInflater.loadAnimator(this, R.animator.trans_animator);
//动画的执行目标
animator.setTarget(ivShow);
//2秒后开始执行
//animator.setStartDelay(2000);
animator.start();
Animator animator = AnimatorInflater.loadAnimator(this, R.animator.trans_animator);
//动画的执行目标
animator.setTarget(ivShow);
//2秒后开始执行
//animator.setStartDelay(2000);
animator.start();