Android补间动画之平移动画

平移动画简介

    平移动画可以实现使一个view从一个坐标点向另一个坐标点的平移效果,平移的方向是水平、垂直或者任意方向。 

平移动画的属性

平移动画在xml中使用translate标签表示,它有以下特有的属性: 

属性描述
fromXDelta起始点的x坐标
toXDelta结束点的x坐标
fromYDelta起始点的y坐标
toYDelta结束点的y坐标

这四个属性每个属性都有三种取值类型,这三种类型分别是float、%和%p。 

1. float 取值类型为浮点类型,表示绝对值,默认单位为px。比如100表示当前view的左上角x/y坐标+100px。
2. % 相对自身的百分比,以当前view的宽度或高度为基数,比如50%表示当前view的左上角x/y坐标+当前view高度/宽度的50%。
3. %p 相对父布局的百分比,以父布局的宽度或高度为基数,比如50%p表示当前view的左上角x/y坐标+当前view父布局宽度/高度的50%。

平移动画的实现方式

平移动画的实现有xml和代码两种方式:

  • xml方式 

    • 首先创建动画文件
      <set xmlns:android="http://schemas.android.com/apk/res/android"
        android:shareInterpolator="false">
        <translate
            android:fromYDelta="0%p"
            android:toYDelta="50%p"
            android:duration="30000"
            android:fillBefore="true"
            android:fillAfter="false"
            android:repeatCount="2"
            android:repeatMode="reverse"
            android:interpolator="@android:anim/linear_interpolator"
           />
      </set>
      
    • 其次用代码获取并启动动画
      Animation animation =  AnimationUtils.loadAnimation(this,R.anim.translation);
      imageView.startAnimation(animation);
      
  • Java代码方式

//创建平移动画对象
        Animation animation = new TranslateAnimation(0,0,0,500);
        animation.setDuration(2000);//时长
        animation.setFillBefore(true);
        animation.setFillAfter(false);//动画结束之后回到原点,默认
        animation.setFillEnabled(true);//填充效果不回到原地
         animation.setRepeatMode(Animation.REVERSE);//循环模式
        animation.setRepeatCount(2);//循环次数
        imageView.setAnimation(animation);//为控件绑定动画
//        imageView.startAnimation(animation);
        animation.startNow();

关于setAnimation()和startAnimation()的区别:setAnimation()告诉程序将 要执行什么动画,方法的参数就是将要执行的动画。startAnimation()告诉程序将要执行什么动画,并且马上执行,参数就是已经配置好的将要执行的动画。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值