android动画入门
动画在我们开发的过程当中是十分的有用,因此掌握对动画的了解是必要了,对android的基础动画进行简单的入门介绍
Frame animation(帧动画)
- 该动画是将不同的图片资源连续的播放,来形成的动画,就好像小时候看过的露天的电影一样;
- 现在资源目录 res 文件夹下面 创建 drawable文件夹
- 在drawable 文件下创建以 animation-list 为根节点的 xml 文件 (动画集合),放入图片的资源
在 xml文件中指定播放的图片的资源:
效果如下:
代码如下:
//xml的文件内容,放在drawable文件下,与资源在同一目录
Tween Animation(补间动画)
- 只能应用于 View 对象;主要有 alpha (透明度) translate(平移) scale (缩放) rotate (旋转) set(动画集合)
步骤:
- 在res 文件夹创建 anim ;
- 在 anim 下创建 动画的配置文件;
- 开要开启的 Activity 之后(需要 在startActivity 或 finish 之后调用) 调用 overridePendingTransition(R.anim.anim_in, R.anim.anim_out);
- 完成界面之间的切换;
代码的示例:
<set android:shareInterpolator="false"> <scale android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromXScale="1.0" android:toXScale="1.4" android:fromYScale="1.0" android:toYScale="0.6" android:pivotX="50%" android:pivotY="50%" android:fillAfter="false" android:duration="700" /> <set android:interpolator="@android:anim/decelerate_interpolator"> <scale android:fromXScale="1.4" android:toXScale="0.0" android:fromYScale="0.6" android:toYScale="0.0" android:pivotX="50%" android:pivotY="50%" android:startOffset="700" android:duration="400" android:fillBefore="false" /> <rotate android:fromDegrees="0" android:toDegrees="-45" android:toYScale="0.0" android:pivotX="50%" android:pivotY="50%" android:startOffset="700" android:duration="400" /> </set>
代码实现动画的效果:
AlphaAnimation aa = new AlphaAnimation(0.0f, 1.0f);//透明动画;
aa.setFillAfter(true);
RotateAnimation ra =
new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
ra.setDuration(3000);//旋转动画
ra.setFillAfter(true);
ScaleAnimation sa = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f);//比例动画;
sa.setFillAfter(true);
sa.setDuration(3000);
aa.setDuration(3000);//设置显示动画的时间;
AnimationSet as = new AnimationSet(true);
as.addAnimation(ra);
as.addAnimation(sa);
as.addAnimation(aa);
rl_spalsh.startAnimation(as)
Property Animation(属性动画)
- 属性动画同样可以在 xml总配置:
res/animator/filename.xm
//代码的实现; ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "alpha", new float[]{0.0f,0.2f,0.4f,0.6f,0.8f,1.0f}); oa.setDuration(3000); oa.setRepeatCount(ObjectAnimator.INFINITE); oa.setRepeatMode(ObjectAnimator.REVERSE); oa.start();
// xml文件的方式设置属性动画 Animator animator = AnimatorInflater.loadAnimator(this, R.animator.alpha); animator.setTarget(iv); animator.start();