Android动画之TranslateAnimation使用

TranslateAnimation即位移动画,很多常见APP里都带有该效果

直接上干货~~


  • 先看下xml文件:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="1000"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:fillAfter="true"
        android:fillEnabled="true"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:repeatCount="1"
        android:repeatMode="reverse"
        android:toXDelta="100%"
        android:toYDelta="0" />

    <!--android:toXDelta="50%"
    这里填写百分比则以自己或者父控件为参照物
    若填写具体数字如1000则为绝对坐标,即该手机的像素点对应的长度-->
</set>

//属性解析:

float fromXDelta,这个参数表示动画开始的点离当前View X坐标上的差值

float toXDelta,这个参数表示动画结束的点离当前View X坐标上的差值

float fromYDelta,这个参数表示动画开始的点离当前View Y坐标上的差值

float toYDelta,这个参数表示动画开始的点离当前View Y坐标上的差值

如果view在A(x,y)点 那么动画就是从B点(x+fromXDelta, y+fromYDelta)点移动到C 点(x+toXDelta,y+toYDelta)点.
如上面属性表示view的起始位置A(x,y),从A(x+0,y+0)移动到B(x+x,y+0)
  • Java代码调用该xml文件
   /**
     * 位移动画
     *
     * @param context
     * @param view 目标view
     */
    public static void startTranslateAnim(Context context, View view) {
        Animation animation = AnimationUtils.loadAnimation(context, R.anim.anim_translate);
        if (view != null)
            view.startAnimation(animation);
    }
  • 最终效果如下:

这里写图片描述


  • 下面看纯Java代码实现:
    public static void animTranslate(View view){
//        TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,int fromYType, float fromYValue, int toYType, float toYValue)
        //当这里的 type=Animation.ABSOLUTE时,就和上面的xml效果一样
        //经测试这里的 value 取值为当前参照物的多少倍,即:
        // type = Animation.RELATIVE_TO_SELF 表示x坐标移动到相对自己的0.5倍距离
        // type = Animation.RELATIVE_TO_PARENT 表示x坐标移动到自己父控件x轴的0.5倍距离 即 x + 0.5ParentX

        TranslateAnimation animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0.5f,
                                                              Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0);
        animation.setDuration(1000);
        animation.setRepeatMode(Animation.REVERSE);
        animation.setInterpolator(new AccelerateInterpolator());
        animation.setFillAfter(true);
        if (view != null)
            view.startAnimation(animation);
    }
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值