Android ObjectAnimator animate translationY same proportion on all screen sizes

Nic Hubbard picture Nic Hubbard · Sep 23, 2013 · Viewed 16.7k times · Source

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

Answer

Nic Hubbard picture Nic Hubbard · Sep 23, 2013

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!