6

I am using ObjectAnimator to slide up a background image to reveal a listView below. The listView has a weight of 0.7f so that it will be the same proportions on all screen sizes.

Using ObjectAnimator is it possible to then slide up my background image with that same 0.7 proportion, being the same on all screens?

Background image and listView:

<LinearLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 android:weightSum="1" >

 <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="0dp"
     android:layout_weight="0.3" >
 </LinearLayout>

 <ListView
     android:id="@+id/listView"
     android:layout_width="match_parent"
     android:layout_height="0dp"
     android:layout_weight="0.7"
     android:background="#303030"
     android:divider="#555555"
     android:dividerHeight="1dp" >

 </ListView>

 </LinearLayout>

Animation code:

FrameLayout mainView = (FrameLayout)findViewById(R.id.mainView);
ObjectAnimator mover = ObjectAnimator.ofFloat(mainView, "translationY", !historyShown ? -500 : 0);
mover.setDuration(300);
mover.start();
2
  • You could potentially use View.getLocationInWindow() or View.getLocationOnScreen() to determine the vertical offset of the ListView in terms of a number of pixels. That should give you all the information you need to figure out how far up you should translate the background image. Not sure when your animation runs, but if you kick it off before the view hierarchy has been fully laid out, you may want to wrap the logic inside a Runnable and post it to the root view.
    – MH.
    Sep 23, 2013 at 3:47
  • The Animation just happens when the user taps a button. Will give your idea a try and report back. Sep 23, 2013 at 4:23

1 Answer 1

8

Actually figured out a quicker and easier way of doing that. Since my listView is using a weight setting of 0.7f it will always be proportional for all screen sizes. So, I just needed to get the height of the listView and use that in my ObjectAnimator:

 // Figure out where listView is
 int listViewHeight = list.getHeight();

 FrameLayout mainView = (FrameLayout)findViewById(R.id.mainView);
 ObjectAnimator mover = ObjectAnimator.ofFloat(mainView, "translationY", !historyShown ? -listViewHeight : 0);
 mover.setDuration(300);
 mover.start();

Worked perfectly!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.