简体   繁体   中英

How do I move a button into the screen's center programmatically?

At first the Button is created with the XML-tag android:layout_centerHorizontal="true" which works fine.

But this Button is moved along the x-axis using an ObjectAnimator during run time.

Now I want to move that Button back to where it has been at the beginning but I can't figure out how to do that (I want to keep the animation!). The Button is never moved along the y-axis.

I have tried

Display display = getWindowManager().getDefaultDisplay();
        Point point = new Point();
        display.getSize(point);
        ObjectAnimator animator = ObjectAnimator.ofFloat(button, "X", (point.x/2)-(button.getWidth()/2));
        animator.start();

already but my Button somehow is a little bit too far on the right every time (~30px). It does not seem to be affected by the margin. Padding affects this though. When I set the padding to 0dp, the offset is way smaller (~5px) but still there.

Hopefully you are able to point out my mistake(s), know a solution or even know a better way to move Views around. I used to edit the transitionX value which could be reset to 0 . That method does not work for me anymore because I try to check if my Button overlaps with another View (which it does on smaller devices but not on bigger ones) and react to it.

Googling or browsing stackoverflow did not help either...

Thanks in advance Tafelbomber

EDIT:

I figured out that button.getX() gives me wrong values by calling it immediatly before moving the button away and after that moving it to the queried X-value. This has the same effect as before.


EDIT 2:

The above problem in a way solved itself after a PC reboot. But the other problem I only explained a little is still there: I want to check programmatically if two views overlap. In the screenshot I colored the spinner for convenience. To check that I tried this method:

private boolean viewsOverlap(View leftView, View rightView){
    float leftViewRight = leftView.getRight();
    float rightViewLeft = rightView.getLeft();
    System.out.println("leftViewRight "+leftViewRight);
    System.out.println("rightViewLeft "+rightViewLeft);
    System.out.println("viewsOverlap: " + (leftViewRight > rightViewLeft));
    return leftViewRight > rightViewLeft;
}

But the output is not as expected:

System.out﹕ leftViewRight 718.0
System.out﹕ rightViewLeft 0.0
System.out﹕ viewsOverlap: true

Screenshot of Button and Spinner: http://i.stack.imgur.com/G8DYP.jpg (sorry for that link but I am not allowed to post pictures with less than 10 reputation)

The leftView is the Button and rightView the Spinner. Both have been moved around already as shown in the very first example. As you can clearly see they are not overlapping and the Spinners left edge also is not at 0.0 and the button's right edge is not at 718 but at ~400. You can even estimate that the value has to be wrong since the screen is fullHD.

The view that it overlaps with sometimes when you use the translateX(dx) method--what if you do getRight() on, and then translate the Button by the difference? That way it should always butt up against that view, which sounds like what you want?

Or do something similar but using ObjectAnimator again to reverse the animation?

The other way to consider tackling it is re-apply the layout params:
xxx.LayoutParams lps = new xxx.LayoutParams(WRAP_CONTENT, WRAP_CONTENT); lps.gravity = Gravity.CENTER_IN_HORIZONTAL; mButtonView.setLayoutParams(lps);

when the animation ends.

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