Android ObjectAnimator animate translationY same proportion on all screen sizes
Asked Answered
H

1

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();
Handtohand answered 23/9, 2013 at 2:4 Comment(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.Demodulator
The Animation just happens when the user taps a button. Will give your idea a try and report back.Handtohand
H
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!

Handtohand answered 23/9, 2013 at 4:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.