Android 动画类型详解

什么是动画
  •  动画有下面两种情况
    • 同一个图形通过视图在界面上进行透明度,缩放,旋转,平移的变化(View 动画)
    •  同一个位置上不断切换显示不同的图片(Drawable 动画)
  • 动画的分类
    • View Animation  视图动画
    • Drawable Animation 图片动画
  •  Android 中提供了两种实现动画的方式
    • 纯编码的方式
    • Xml配置的方式


View动画分类
  • 单一动画(Animation)
    • 缩放动画(ScaleAnimation)
    • 透明度动画(AlphaAnimation)
    • 旋转动画(RotateAnimation)
    • 平移动画(TranslateAnimation)
  • 复合动画(AnimationSet)
    • 由多个单一动画组合在一起的动画




Animation 的公用功能
  • setDuration(long durationMills)  设置持续时间
  • setStartOffset(long startOffset) 设置延迟时间
  • setFillBefore(boolean fillBefore) 是否停在起始状态
  • setFillAfter(boolean fillAfter) 是否停在最终状态
  • setAnimationListener(AnimationListener listener)
  • 坐标类型
    • Animation.ABSOLUTE
    • Animation.RELATIVE_TO_SELF
    • Animation.RELATIVE_TO_PARENT
  • 启动动画 view.startAnimation(animation)
  • 结束动画 view.clearAnimation()
  • 动画监听器 AnimationListener
    • onAnimationStart(Animation animation)
    • onAnimationEnd(Animation animation)
    • onAnimationRepeat(Animation animation)


缩放动画


//编码实现缩放
//1.创建动画对象
ScaleAnimation scaleAnimation =
        new ScaleAnimation(0.5f, 1.5f, 0, 1,
                Animation.ABSOLUTE, img.getWidth() / 2, Animation.ABSOLUTE, 0);
//2.设置
// 延时开始
scaleAnimation.setStartOffset(1000);
//持续时间
scaleAnimation.setDuration(2000);
//最终还原
scaleAnimation.setFillBefore(true);
//3.启动动画
img.startAnimation(scaleAnimation);

Xml ScaleAnimation


<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromXScale="0"
    android:fromYScale="0"
    android:pivotX="100%"
    android:pivotY="100%"
    android:startOffset="1000"
    android:toXScale="1.5"
    android:toYScale="1.5">
</scale>

//.创建动画对象
//2.设置
Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_scale);
//3.启动动画
img.startAnimation(animation);



旋转动画

//编码实现缩放
//1.创建动画对象
RotateAnimation rotateAnimation =
        new RotateAnimation(0, 90, Animation.RELATIVE_TO_SELF
                , 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//2.设置
// 延时开始
rotateAnimation.setStartOffset(1000);
//持续时间
rotateAnimation.setDuration(5000);
//最终还原
rotateAnimation.setFillBefore(true);
//3.启动动画
img.startAnimation(rotateAnimation);

xml

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



透明度动画

AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
alphaAnimation.setDuration(2000);
img.startAnimation(alphaAnimation);

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:toAlpha="1"
    android:duration="5000"
    android:fromAlpha="0">
</alpha>


平移动画

TranslateAnimation translateAnimation
        = new TranslateAnimation(Animation.RELATIVE_TO_SELF
        , 0, Animation.RELATIVE_TO_SELF, 1, Animation.RELATIVE_TO_SELF, 0,
        Animation.RELATIVE_TO_SELF, 1);
translateAnimation.setDuration(2000);
img.startAnimation(translateAnimation);

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



复合动画

//创建透明度动画  并设置
AlphaAnimation alphaAnimation1 = new AlphaAnimation(0, 1);
alphaAnimation1.setDuration(2000);
RotateAnimation rotateAnimation1 = new RotateAnimation(0, 360,
        Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation1.setStartOffset(2000);
rotateAnimation1.setDuration(1000);
//创建旋转动画 并设置
// 创建复合动画对象
AnimationSet animationSet = new AnimationSet(false);
// 添加两个单一动画
animationSet.addAnimation(alphaAnimation1);
animationSet.addAnimation(rotateAnimation1);
//启动复合动画
img.startAnimation(animationSet);

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


Interpilator 属性的使用
  • 用来修饰动画效果,定义东动画的变化率,可以使存在的动画效果accelerated 加速,decelerated 减速 repeated 重复等


Animation rotateAnim1 = AnimationUtils.loadAnimation(this, R.anim.anim_rotate);
rotateAnim1.setRepeatCount(Animation.INFINITE);
//设置线性变化
rotateAnim1.setInterpolator(new LinearInterpolator());
img.startAnimation(rotateAnim1);



图片动画



<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
    <item
        android:drawable="@mipmap/img"
        android:duration="500"/>
    <item
        android:drawable="@mipmap/cat"
        android:duration="500"/>
    <item
        android:drawable="@mipmap/img"
        android:duration="500"/>
</animation-list>


AnimationDrawable background = (AnimationDrawable) img.getBackground();
background.start();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值