匀速动画封装

function animate(obj,target){
    clearInterval(obj.timer);
    var speed = obj.offsetLeft < target ? 5 : -5;
    obj.timer = setInterval(function(){
        obj.style.left = obj.offsetLeft + speed + "px";
        var result = target - obj.offsetLeft;
        if(Math.abs(result) <= 10){
            clearInterval(obj.timer);
            obj.style.left = target + "px";
        }
    },30)
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现 Android 匀速飘屏动画可以借助 ValueAnimator 和 ObjectAnimator 类。下面是一个简单实现方式: 1. 在 XML 文件中定义一个 ImageView 控件,作为需要飘屏的对象。 ``` <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/your_image" /> ``` 2. 在 Java 代码中获取该控件的引用,并创建一个 ValueAnimator 对象。 ``` ImageView imageView = findViewById(R.id.imageView); ValueAnimator animator = ValueAnimator.ofFloat(0, 1); animator.setDuration(5000); // 设置动画时长为 5 秒 animator.setRepeatCount(ValueAnimator.INFINITE); // 设置重复次数为无限次 animator.setInterpolator(new LinearInterpolator()); // 设置匀速插值器 ``` 3. 为 ValueAnimator 对象添加一个监听器,实现动画过程中 ImageView 控件的位置变换。 ``` animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { float progress = (float) animation.getAnimatedValue(); int width = imageView.getWidth(); int height = imageView.getHeight(); int parentWidth = ((ViewGroup) imageView.getParent()).getWidth(); int parentHeight = ((ViewGroup) imageView.getParent()).getHeight(); float x = parentWidth * progress - width / 2; float y = parentHeight * progress - height / 2; imageView.setX(x); imageView.setY(y); } }); ``` 4. 启动动画。 ``` animator.start(); ``` 这样就可以实现一个简单的 Android 匀速飘屏动画了。需要注意的是,这里使用的匀速插值器只适用于匀速飘屏动画,如果需要实现其他类型的动画,可以使用其他插值器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值