ViewAnimation

一、动画效果

(1)AlphaAnimation

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:duration="3000"
        android:fromAlpha="1.0"
        android:toAlpha="0.2" />
</set>
  • duration:设置当前动画时长
  • fromAlpha:设置透明度的初始值
  • toAlpha :设置透明度的结束值

(2)RotateAnimation

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate
        android:duration="3000"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="360" />
</set>
  • fromDegrees:旋转初始的角度
  • toDegrees:旋转结束的角度
  • pivotX: 旋转中心点x坐标
  • pivotY: 旋转中心点y坐标

(3)ScaleAnimation

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:duration="3000"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:toXScale="0.2"
        android:toYScale="0.2"
        android:pivotX="50%"
        android:pivotY="50%" />
</set>
  • fromXScale:水平方向缩放比例的初始值
  • fromYScale:竖直方向缩放比例的初始值
  • toXScale:水平方向缩放比例的结束值
  • toYScale:竖直方向缩放比例的结束值
  • pivotX:缩放中心点的x坐标
  • pivotY:缩放中心点的y坐标

(4)TranslateAnimation

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="3000"
        android:fromXDelta="20%"
        android:fromYDelta="20%"
        android:toXDelta="100%"
        android:toYDelta="100%" />
</set>
  • fromXDelta:移动起始点的x坐标
  • fromYDelta:移动起始点的y坐标
  • toXDelta:移动结束点的x坐标
  • toYDelta:移动结束点的y坐标

(5)组合动画

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="4000"
    android:fillAfter="true"
    android:fillBefore="true"
    android:startOffset="1000">
    <alpha
        android:duration="3000"
        android:fromAlpha="1.0"
        android:repeatCount="infinite"
        android:repeatMode="reverse"
        android:toAlpha="0.2" />
    <rotate
        android:duration="3000"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="infinite"
        android:repeatMode="reverse"
        android:toDegrees="360" />
    <scale
        android:duration="3000"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:repeatCount="infinite"
        android:repeatMode="reverse"
        android:toXScale="0.2"
        android:toYScale="0.2" />
    <translate
        android:duration="3000"
        android:fromXDelta="20%"
        android:fromYDelta="20%"
        android:repeatCount="infinite"
        android:repeatMode="reverse"
        android:toXDelta="100%"
        android:toYDelta="100%" />
</set>
  • fillAfter:视图是否会停留在动画结束的状态,默认为false
  • fillBefore:视图是否会停留在动画开始的状态,默认为true
  • startOffset:动画延迟开始时间

二、动画实现

(1)通过xml文件实现动画

  • 在res/anim文件下创建xml文件
  • 在代码中通过引用xml文件实现动画效果
Animation loadAnimation = AnimationUtils.loadAnimation(this, R.anim.animation_translate);
loadAnimation.setFillAfter(true);
View.startAnimation(loadAnimation);
  • setDuration:动画的运行时间
  • setFillAfter:动画结束时是否保持动画最后的状态
  • setFillBefore:动画结束时是否保持动画最后的状态
  • setInterpolator:设定插值器
  • setRepeatCount:重复次数
  • setRepeatMode:重复类型有两个值,reverse表示倒序回放,restart表示从头播放
  • setStartOffset:调用start函数之后等待开始运行的时间

(2)通过代码实现动画

AnimationSet setAnimation = new AnimationSet(true);
// 设置组合动画的重复类型
setAnimation.setRepeatMode(Animation.RESTART);
//透明度
Animation alpha = new AlphaAnimation(1,0);
alpha.setDuration(3000);
//旋转
Animation rotate = new RotateAnimation(0, 360, 
    Animation.RELATIVE_TO_SELF, 0.5f, 
    Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(1000);
rotate.setRepeatMode(Animation.RESTART);
rotate.setRepeatCount(Animation.INFINITE);
//缩放
Animation scale = new ScaleAnimation(1, 0.5f, 1, 0.5f, 
    Animation.RELATIVE_TO_SELF, 0.5f, 
    Animation.RELATIVE_TO_SELF, 0.5f);
scale.setDuration(1000);
//平移动画
Animation translate = new TranslateAnimation(
    TranslateAnimation.RELATIVE_TO_PARENT, -0.5f, 
    TranslateAnimation.RELATIVE_TO_PARENT, 0.5f, 
    TranslateAnimation.RELATIVE_TO_SELF, 0, 
    TranslateAnimation.RELATIVE_TO_SELF, 0);
translate.setDuration(10000);
//设置到组合动画
setAnimation.addAnimation(alpha);
setAnimation.addAnimation(rotate);
setAnimation.addAnimation(translate);
setAnimation.addAnimation(scale1);
//添加到imageView中
imageView.startAnimation(setAnimation);
  • 补间动画只能够作用在视图View上,无法对非View的对象进行动画操作。
  • 补间动画只是改变了View的视觉效果,而不会真正去改变View的属性。
  • 补间动画只能实现平移、旋转、缩放 & 透明度这些简单的动画需求。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,349评论 25 708
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,524评论 0 17
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,245评论 4 61
  • Arm处理器,因其低功耗和小尺寸而闻名,几乎所有的手机处理器都基于arm,其在嵌入式系统中的应用非常广泛,它的性能...
    meimeiDa阅读 606评论 0 1
  • 定义 逆变与协变用来描述类型转换(type transformation)后的继承关系,其定义:如果A、B表示类型...
    开发者小王阅读 25,731评论 4 61