三个简短的动画实现饿了么红包滑动效果

最近app里要实现类似饿了么 滑动列表或者滑动scrollview 隐藏红包的收起与弹出的效果 于是简单的实现了下

public class OrderDetailRedPacktHelper implements ISendPacktView{

    private String TAG = getClass().getSimpleName();
    private Activity mActivity;
    private ImageView mRedPacktView;
    private ListenedScrollView mScrollView;

    private AnimatorSet aRedPacktIn; // 红包进入动画
    private ObjectAnimator aRedPacktOut;// 红包滑出动画
    private int REDPACKT_WAITE_TIME = 2 * 1000;// 红包进入时再滑出时的等待时间
    private int REDPACK_SCROLL_SATTE = -1;// 0 滑出状态 1 进入状态 2 红包滑出等待状态
    private int ANIM_SATUE = 0;// 0 动画完成 1 动画执行中

    private CenterNoticeDialog mRedpackDialog; // 发送红包对话框
    private ShareDialog mShareDialog;
    private String redPacktImgURL = "http://i.dimg.cc/13/26/70/2b/fb/05/36/d8/7f/42/0f/92/b3/91/5b/69.jpg";// 测试图片

    public OrderDetailRedPacktHelper(Activity activity,ImageView redPacktView,ListenedScrollView scrollView) {
        mActivity = activity;
        mRedPacktView = redPacktView;
        mScrollView = scrollView;
        mShareDialog = new ShareDialog(activity,true);
        mShareDialog.setCanceledOnTouchOutside(true);
        mRedpackDialog = DialogUtils.orderRedPacktNoticeDialog(mActivity,mShareDialog);
        addListener();
        initRedPacktAnimation();

    }

    /**
     * 初始化红包动画
     */
    @Override
    public void initRedPacktAnimation() {
        aRedPacktIn = new AnimatorSet();
        int width = UIUtil.dp2px(56); // 红包view 滑动的距离
        ObjectAnimator waitAnim = ObjectAnimator.ofFloat(mRedPacktView,"waite",100,200);
        waitAnim.setDuration(REDPACKT_WAITE_TIME);
        waitAnim.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
                ANIM_SATUE = 1;
                REDPACK_SCROLL_SATTE = 2;
            }

            @Override
            public void onAnimationEnd(Animator animator) {
                ANIM_SATUE = 0;
            }

            @Override
            public void onAnimationCancel(Animator animator) {
                ANIM_SATUE = 0;
            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        ObjectAnimator inAnim = ObjectAnimator.ofFloat(mRedPacktView,"translationX",width,0);
        inAnim.setDuration(200);
        inAnim.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
                ANIM_SATUE = 1;
                REDPACK_SCROLL_SATTE = 1;
            }

            @Override
            public void onAnimationEnd(Animator animator) {
                ANIM_SATUE = 0;
            }

            @Override
            public void onAnimationCancel(Animator animator) {
                ANIM_SATUE = 0;
            }

            @Override
            public void onAnimationRepeat(Animator animator) {
                ANIM_SATUE = 0;
            }
        });
        aRedPacktIn.playSequentially(waitAnim,inAnim);

        aRedPacktOut = ObjectAnimator.ofFloat(mRedPacktView,"translationX",0,width);
        aRedPacktOut.setDuration(200);
        aRedPacktOut.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
                ANIM_SATUE = 1;
                REDPACK_SCROLL_SATTE = 0;
            }

            @Override
            public void onAnimationEnd(Animator animator) {
                ANIM_SATUE = 0;
            }

            @Override
            public void onAnimationCancel(Animator animator) {
                ANIM_SATUE = 0;
            }

            @Override
            public void onAnimationRepeat(Animator animator) {
                ANIM_SATUE = 0;
            }
        });

    }

    private void addListener() {
        mScrollView.setOnScrollListener(onScrollListener);
    }

    private ListenedScrollView.OnScrollListener onScrollListener = new ListenedScrollView.OnScrollListener() {
        @Override
        public void onBottomArrived() {

        }

        @Override
        public void onScrollStateChanged(ListenedScrollView view, int scrollState) {
            switch (scrollState) {
                case ListenedScrollView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
                    goOutRedPacktAnim();
                    break;
                case ListenedScrollView.OnScrollListener.SCROLL_STATE_IDLE:
                    goInRedPacktAnim();
                    break;
                case ListenedScrollView.OnScrollListener.SCROLL_STATE_FLING:
                    break;

            }
        }

        @Override
        public void onScrollChanged(int l, int t, int oldl, int oldt) {

        }
    };

    /**
     * 显示红包
     */
    @Override
    public void showRedPackt(String imgUrl, ShareInfo shareInfo) {
        if (!isSafe())
            return;
        if (mRedPacktView.getVisibility() != View.VISIBLE)
            mRedPacktView.setVisibility(View.VISIBLE);
        //redPacktImgURL = imgUrl;
        mShareDialog.setShareContent(shareInfo.title, shareInfo.content, shareInfo.image_url, shareInfo.wx_url, shareInfo.friend_url, shareInfo.sina_url);
    }

    /**
     * 隐藏红包控件
     */
    @Override
    public void hideRedPackt() {
        if (!isSafe())
            return;
        if (mRedPacktView.getVisibility() != View.GONE)
            mRedPacktView.setVisibility(View.GONE);
    }

    /**
     * 红包滑出动画
     */
    @Override
    public void goOutRedPacktAnim() {
        if (!isSafe())
            return;
        if (aRedPacktOut.isRunning())
            return;
        if (REDPACK_SCROLL_SATTE == 0)
            return;
        if (REDPACK_SCROLL_SATTE == 2) {
            REDPACK_SCROLL_SATTE = 0;
            aRedPacktIn.cancel();
            return;
        }
        aRedPacktIn.end();
        aRedPacktOut.start();
    }

    /**
     * 红包进入动画
     */
    @Override
    public void goInRedPacktAnim() {
        if (!isSafe())
            return;
        if (aRedPacktIn.isRunning())
            return;
        if (REDPACK_SCROLL_SATTE == 1 || REDPACK_SCROLL_SATTE == 2)
            return;
        aRedPacktOut.end();
        aRedPacktIn.start();
    }

    /**
     * 是否安全 判断activity 是否被销毁和view是否为空
     *
     * @return
     */
    @Override
    public boolean isSafe() {
        return !mActivity.isFinishing() && mRedPacktView != null;
    }

    /**
     * activity 关闭时回调 防止内存泄漏
     */
    @Override
    public void finish() {
        mScrollView.finish();
    }
}

这里需要 重写一个继承至ScrollView的自定义View 应为android 的scrollview的滑动监听是protect方法 不允许外部调用
有更好 更高效的实现方法请告诉我啊

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值