Android中的视图动画

来源:互联网 发布:国内外治疗癌症知乎 编辑:程序博客网 时间:2024/06/05 09:51

一、动画简介
动画基本的一点是能够随一定的时间间隔而变化
动画的共有属性
(1)作用的对象–》目标target
(2)持续的时间–》时长duration
(3)从哪儿来–》开始状态from
(4)到哪儿去–》结束状态to
(5)什么时候开始–》开始时间(beginTime)
(6)执行多少次–》重复次数(repeatCount)
(7)怎么去–》时间轴(timeLine)

二、动画组合
分为单个动画,组合动画。
组合动画:串行动画,并行动画,并行串行组合。

三、Android动画分类
(1)视图动画
又分为补间动画,帧动画。
补间动画分为渐变动画,平移动画,缩放动画,旋转动画。
(2)属性动画

四、Android中的动画属性对应关系
目标target–>View
时长duration–>duration
开始状态from–>fromXXX
结束状态to–>toXXX
开始时间beginTime–>startOffset
重复次数(repeatCount)–>repeatCount
时间轴(timeline)–>interpolator

五、动画实现方式
(一)xml实现
在res/anim目录下创建动画文件

<?xml version="1.0" encoding="utf-8"?><alpha xmlns:android="http://schemas.android.com/apk/res/android"       android:duration="2000"       android:fromAlpha="1.0"       android:startOffset="1000"       android:toAlpha="0.0"></alpha>

使用AnmationUtils.loadAnmation加载

        Animation alpha = AnimationUtils.loadAnimation(this, R.anim.rotate);        imageView.startAnimation(alpha);
<!--旋转中心点为百分数时为View本身,单纯数值是根据坐标值,同理Scale一样。100%p表示父控件的相应数值---><rotate xmlns:android="http://schemas.android.com/apk/res/android"        android:duration="3000"        android:fromDegrees="0"        android:toDegrees="360"        android:pivotX="50%"        android:pivotY="50%"></rotate>

(二)代码实现

        TranslateAnimation translate = new TranslateAnimation(0, 200, 0, 0);        translate.setDuration(3000);        imageView.startAnimation(translate);
        ScaleAnimation scale = new ScaleAnimation(1,2, 1, 2, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);        scale.setDuration(3000);        imageView.startAnimation(scale);

这里写图片描述

六、动画的开始和结束

        scale.setAnimationListener(new Animation.AnimationListener() {            @Override            public void onAnimationStart(Animation animation) {            }            @Override            public void onAnimationEnd(Animation animation) {            }            @Override            public void onAnimationRepeat(Animation animation) {            }        });

七、实现并行与串行动画
这里写图片描述

<set xmlns:android="http://schemas.android.com/apk/res/android">    <translate        android:duration="3000"        android:fromXDelta="0"        android:fromYDelta="0"        android:toXDelta="0"        android:toYDelta="-300"></translate>    <rotate        android:duration="3000"        android:fromDegrees="0"        android:pivotX="50%"        android:pivotY="50%"        android:startOffset="3000"        android:toDegrees="-45"></rotate>    <scale        android:duration="3000"        android:fromXScale="1"        android:fromYScale="1"        android:pivotX="50%"        android:pivotY="50%"        android:startOffset="3000"        android:toXScale="2"        android:toYScale="2"></scale>    <alpha        android:duration="3000"        android:fromAlpha="1.0"        android:startOffset="3000"        android:toAlpha="0.0"></alpha></set>
        Animation animation  = AnimationUtils.loadAnimation(this, R.anim.love_fly);        love.startAnimation(animation);

八、插值器
插值器控制动画在时间轴上的变换效果
(1)加速插值器

        Animation animation = getTranslate();        animation.setInterpolator(new AccelerateDecelerateInterpolator());        iv1.startAnimation(animation);

(2)循环插值器

        Animation animation = getTranslate();        animation.setInterpolator(new CycleInterpolator(2));        iv3.startAnimation(animation);

(3)

        Animation animation = getTranslate();        animation.setInterpolator(new OvershootInterpolator());        iv2.startAnimation(animation);

(4)

        Animation animation = getTranslate();        animation.setInterpolator(new AnticipateInterpolator(2));        iv4.startAnimation(animation);

九、动画的最终位置

        TranslateAnimation translate = new TranslateAnimation(0,0,0,-200);        translate.setDuration(3000);        //停到动画最终执行位置        translate.setFillAfter(true);        love.startAnimation(translate);

十、Frame动画
示例代码:

 private AnimationDrawable drawable;             frame.setBackgroundResource(R.drawable.play_list);            drawable = (AnimationDrawable) frame.getBackground();            //是否只执行一次            drawable.setOneShot(true);            drawable.start();

xml文件:

<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@drawable/play_1" android:duration="50" />    <item android:drawable="@drawable/play_13" android:duration="50" /></animation-list>
0 0
原创粉丝点击