ViewAnimation

作者: kjy_112233 | 来源:发表于2017-09-05 15:59 被阅读0次

一、动画效果

(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的属性。
  • 补间动画只能实现平移、旋转、缩放 & 透明度这些简单的动画需求。

相关文章

网友评论

    本文标题:ViewAnimation

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