Reputation: 3408
I have a question, not so much an issue that I needed an explanation on. Currently I in my app I have a button that permits a view to slide down from below the toolbar. In this view there are edittexts etc that provide advanced search functionalities.
I have created the view with a size of 100dp and a top margin of -100dp. This works properly and hides the view. My question moreso comes into play when I am animating the views below. I am "pushing" them down with the main view. I figured the down and up should match however I have to set the second view to a value of -30 for it to properly re-align.
Can anyone explain this?
Note - this is a test layout
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="-100dp"
android:background="#Fa45"
android:id="@+id/viewtest">
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/contentMain">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Slide Up" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Slide Down"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
This doesn't work
By this doesn't work, what I mean is that the buttons end up missing or half cut-off as they slide right back up with the other view.
public void startSlideUpAnimation(View view) {
v.animate().translationY(-400).setInterpolator(new BounceInterpolator()).setDuration(1000);
x.animate().translationY(-400).setInterpolator(new BounceInterpolator()).setDuration(1000);
}
public void startSlideDownAnimation(View view) {
v.animate().translationY(400).setInterpolator(new BounceInterpolator()).setDuration(1000);
x.animate().translationY(400).setInterpolator(new BounceInterpolator()).setDuration(1000);
}
But if I change the line:
x.animate().translationY(-400).setInterpolator(new BounceInterpolator()).setDuration(1000);
to
x.animate().translationY(-30).setInterpolator(new BounceInterpolator()).setDuration(1000);
It works just fine. Am I crazy or fudging math somewhere here?
EDIT
Ok I have found that if I set translation y to 0 on both views they both match identically on the way back up and go back to their origin's. This makes even less sense to me as I thought the float offset was the value it should be returned or translated.
Any idea? Is setting it to zero returning it to the origin truly or am I missing a piece of the puzzle?
Upvotes: 0
Views: 93
Reputation: 39836
every view translation value originally is of zero
and the call to translationY
sends it to the absolute value specified.
If you want to can call translationYBy
to offset the current value by the specified value
With that explanation in mind if you code the following code, you will get the correct results, by animating to one position and then coming back to the zero
original.
// first
.translationY(-400) // translate to -400
// and then later
.translationY(0) // go back to the original position
or if you follow the way you're thinking, you could code like the following, to translate 400 offseting from current value and then 400 offset to the other direction
// first
.translationYBy(-400) // offset 400 to one direction
// and then later
.translationYBy(400) // offset 400 to the other direction
here is the official docs: https://developer.android.com/reference/android/view/ViewPropertyAnimator.html
Upvotes: 1