简体   繁体   中英

ViewPropertyAnimator messing with view layout position

I created a button, set the position using top and left margin and added to the layout. If i just do this the buttons sits there in the right location.

Now I need to add a translation animation, and I am using viewPropertyAnimator . Unfortunately this voids the initial button position, making the animation start from (0, 0).

Following is the code I wrote.

        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);

Anybody has any idea on where I'm doing wrong?

使用bonus_l.setX(bonus_x_start)bonus_l.setY(bonus_x_start)代替保证金

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM