美文网首页
android:自定义循环移动的View

android:自定义循环移动的View

作者: 江左灬梅郎 | 来源:发表于2018-08-15 19:34 被阅读0次

最近,项目需求中有一个循环左右移动的View,从中间移动到最左边,然后又移动到最右边,如此来回循环。
效果图如下:


循环移动.gif

其中LoopAnim 是控件循环移动的动画。
核心代码:


import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.DecelerateInterpolator;


public class LoopAnim {

    /**
     * 对外调用的方法
     *
     * @param moveView     移动的View
     * @param moveZoneView 移动区域
     */
    public static void startAnim(View moveView, View moveZoneView) {
        int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        moveZoneView.measure(w, h);
        int height = moveZoneView.getMeasuredHeight();
        int width = moveZoneView.getMeasuredWidth();
        LoopAnim.addAnim(moveView, width);
    }

    /**
     * 定义动画
     *
     * @param view  view对象
     * @param width 移动的区域的宽度
     *              动画流程   首先从中间开始往左边移动对应的距离
     *              <p>
     *              再移动到最右边,再移动到最左边,然后反复执行此方法
     */
    private static void addAnim(final View view, final int width) {
        ObjectAnimator statrRight = ObjectAnimator.ofFloat(view, "translationX", 0F, -(width));
//        statrRight.setInterpolator(new Inte));//设置动画插入器,减速,默认是先加速再减速,符合要求
        statrRight.setDuration(4000);
        statrRight.start();
        statrRight.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                loop(view, width);
            }
        });

    }

    private static void loop(final View view, final int width) {
        ObjectAnimator loopRight = ObjectAnimator.ofFloat(view, "translationX", -(width), width);
//        loopRight.setInterpolator(new DecelerateInterpolator(2.0f));//设置动画插入器,减速
        loopRight.setDuration(8000);
        loopRight.start();
        loopRight.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                ObjectAnimator loopLeft = ObjectAnimator.ofFloat(view, "translationX", width, -(width));
//                loopLeft.setInterpolator(new DecelerateInterpolator(1.0f));//设置动画插入器,减速
                loopLeft.setDuration(8000);
                loopLeft.start();
                loopLeft.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);
                        loop(view, width);
                    }
                });

            }
        });
    }
}

接下来,调用就很简单了,一句代码就OK。

LoopAnim.startAnim(moveView, moveZoneView);

其中 moveView 是指移动的 View
moveZoneView 是指 View移动的区域

搞定。


相关文章

网友评论

      本文标题:android:自定义循环移动的View

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