安卓XML和java文件中定义基本动画

来源:互联网 发布:网络剧堪比权力的游戏 编辑:程序博客网 时间:2024/06/13 03:25

1.Android 动画的定义


android平台提供了两类动画    第一类是Tween ,即通过对场景里的对象不断做图像变换(平移,缩放,旋转)产生动画效果    第二类是Frame动画,即顺序播放事先做好的图像,跟电影类似  

围绕中心旋转的动画

动画效果如下:

这里写图片描述


做法

(1) xml中定义动画

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <rotate        android:duration="5000"        android:fromDegrees="0"        android:pivotX="50%"        android:pivotY="50%"        android:repeatCount="-1"        android:toDegrees="-359" /></set>

(2) java代码中定义动画

    RotateAnimation ra = new RotateAnimation(0f, -359f,            Animation.RELATIVE_TO_SELF, 0.5f,             Animation.RELATIVE_TO_SELF, 0.5f);    ra.setDuration(5000);    ra.setRepeatCount(-1); //无线循环    ra.setInterpolator(new LinearInterpolator());  //匀速运动    iv.startAnimation(ra);

相关解释:

pivotX          相对于自身x轴的对少 repeatCount     重复的次数          -1表示不停setInterpolator 表示设置旋转速率                                LinearInterpolator      匀速                                Accelerateinterpolator  加速                                Decelerateinterpolator  减速

开始和停止旋转

动画开始if (iv != null && ra != null) {        iv.startAnimation(ra);    }清除动画if (iv != null && ra != null) {        iv.clearAnimation();    }

java代码的一些动画

(1)透明动画(透明—>不透明)

        AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);        alphaAnimation.setDuration(1000);        alphaAnimation.setFillAfter(true);

(2)缩放动画

    ScaleAnimation scaleAnimation = new ScaleAnimation(            0, 1,             0, 1,             Animation.RELATIVE_TO_SELF, 0.5f,             Animation.RELATIVE_TO_SELF, 0.5f);    scaleAnimation.setDuration(1000);    alphaAnimation.setFillAfter(true);

(3)动画集合Set
AnimationSet animationSet = new AnimationSet(true);
//添加两个动画
animationSet.addAnimation(alphaAnimation);
animationSet.addAnimation(scaleAnimation);

1 0
原创粉丝点击