首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ViewPropertyAnimator扰乱视图布局位置

ViewPropertyAnimator扰乱视图布局位置
EN

Stack Overflow用户
提问于 2017-11-07 11:50:53
回答 1查看 140关注 0票数 0

我创建了一个按钮,使用顶部和左侧边距设置位置,并将其添加到布局中。如果我这么做,按钮就在正确的位置。

现在我需要添加一个翻译动画,我正在使用viewPropertyAnimator。不幸的是,这会使初始按钮位置空出,使动画从(0,0)开始。

下面是我写的代码。

代码语言:javascript
复制
        final Button bonus_button = new Button(this);

        int bonus_y_start, bonus_x_start, bonus_y_end, bonus_x_end;

        bonus_x_start = random.nextInt(2) == 1 ? layout_width + button_size : -1 * button_size;
        bonus_y_start = random.nextInt(layout_height + 2 * button_size) - button_size;

        if (bonus_x_start < 0) 
            bonus_x_end = layout_width + button_size;
        else
            bonus_x_end = -1 * button_size;

        bonus_y_end = random.nextInt(layout_height + button_size) - button_size;

        RelativeLayout.LayoutParams bonus_l = new RelativeLayout.LayoutParams(button_size, button_size);
        bonus_l.leftMargin = bonus_x_start;
        bonus_l.topMargin = bonus_y_start;
        bonus_button.setLayoutParams(bonus_l);

        bonus_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mHandler.sendEmptyMessage(MSG_PAUSE_TIMER);
                final Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mHandler.sendEmptyMessage(MSG_RESUME_TIMER);
                    }
                }, 2000);
            }
        });

        bonus_button.bringToFront();

        ViewPropertyAnimator animator = bonus_button.animate().x(bonus_x_end).y(bonus_y_end);
        animator.setDuration(2000);
        animator.withEndAction(new Runnable() {
            @Override
            public void run() {
                layout.removeView(bonus_button);
            }
        });

        layout.addView(bonus_button);

有人知道我哪里做错了吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-07 13:21:38

使用bonus_l.setX(bonus_x_start)bonus_l.setY(bonus_x_start)代替页边距

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47157270

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档