美文网首页Android知识Android开发
Android动画之视图动画

Android动画之视图动画

作者: 墨革 | 来源:发表于2016-09-17 17:31 被阅读93次

    分类

    Android动画主要包括视图动画和属性动画。视图动画包括Tween动画和Frame动画。Tween动画又包括渐变动画、平移动画、缩放动画、旋转动画。

    Tween动画的基本属性

    • 目标 View;
    • 时常 duration;
    • 开始状态 fromXXX;
    • 结束动画 toXXX;
    • 开始时间 startOffset;
    • 重复次数 repeatCount;
    • 时间轴 interpolator(插值器)。

    代码示例

    xml实现
    <?xml version="1.0" encoding="utf-8"?>   
    <translate xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="100%"
        android:toYDelta="0"
        android:fillAfter="true"
        android:duration="3000">
    </translate>
    

    在代码中调用

    Animation translate = AnimationUtils.loadAnimation(context,R.anim.translate);
    imageView.startAnimation(translate);  
    

    补充:
    1.对于缩放和旋转动画,有一个pivotX或者pivotY,表示的是缩放或旋转的中心点。对应的属性值
    有三种写法。

    • 数值 50 表示当前控件的左上角加上50px;
    • 百分数 50% 表示当前控件的50%;
    • 百分数p 50%p 表示父控件的50%。

    2.在一个动画集合里,可以通过设置stratOffset属性,来实现多个动画并行和串行的效果。

    Frame动画

    Frame动画的配置文件放在drawable目录下

    <?xml version="1.0" encoding="utf-8"?>
    <animation-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/image1" android:duration="50"/>
        <item android:drawable="@drawable/image2" android:duration="50"/>
        <item android:drawable="@drawable/image3" android:duration="50"/>
    </animation-list>
    
    // 需要先设置成背景
    imageView.setBackgroundResource(R.drawable.frame_anim);
    AnimationDrawable frameAnimation = (AnimationDrawable) imageView.getBackground();
    frameAnimation.start();
    

    相关文章

      网友评论

        本文标题:Android动画之视图动画

        本文链接:https://www.haomeiwen.com/subject/emhwettx.html