3

I'm using fragments and animations. I came out with this:

slide_in_up.xml :

<?xml version="1.0" encoding="utf-8"?>
<set>
  <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="y" 
    android:valueType="floatType"
    android:valueFrom="600"
    android:valueTo="0" 
    android:duration="500"/>
</set>

And this is where the animation is used:

public void showFirstFragment(View v) {
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.addToBackStack(null);
    ft.setCustomAnimations(R.animator.slide_in_up, R.animator.slide_out_up);    
    ft.replace(R.id.fragmentContainer, firstFragment);
    ft.commit();
}

It works, pretty well: the entering fragment shows up from the bottom while the previous one goes away up.

My problem is that i have a fixed dimension for the "vavlueFrom" tag in my animation XML (it is set to 600 because my fragment takes half the size of the screen and i'm thinking with 1200px resolution). With this fixed dimension when i have a screen resolution smaller than 1200px my fragment shows up a little bit late because it starts from more down (sorry if this sentence is wrong - I'm italian).

How can i set this dimension programmatically? I want to do something like: get screen resolution -> set "valueFrom" to half the y value.

Thaks in advance for your help!

aveschini
  • 1,632
  • 3
  • 22
  • 39
  • I have a similar issue. Have you found an answer to this problem? – Mugur May 22 '13 at 12:08
  • Nope, sorry! I've just created multiple animation with different sizes, checked the screen width and height and used the animation with the most similar sizes... – aveschini May 22 '13 at 13:33
  • With the view animation you could do something like that : but only if you don't have to stick to Fragments! – aveschini May 22 '13 at 13:34
  • 1
    maybe that post will be of some help http://stackoverflow.com/a/16282741/884195 – Deepscorn Apr 28 '15 at 14:04

1 Answers1

-3

Use a percentage instead of exact y value.

<?xml version="1.0" encoding="utf-8"?>
<set>
  <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="y" 
    android:valueFrom="80%"
    android:valueTo="0" 
    android:duration="500"/>
</set>
retromuz
  • 809
  • 9
  • 26
  • This doesn't works, the compiler will complain about using fraction types. – Mokkun Mar 06 '14 at 08:44
  • @Petter what android version you are trying to achieve this on? – retromuz Mar 06 '14 at 17:33
  • I'm using API14, but if I'm not wrong ObjectAnimator doesn't work with fraction types (AS complains about it btw), you need to make a custom implementation in order to achieve this AFAIK, e.g. http://stackoverflow.com/questions/10854940/android-using-objectanimator-to-translate-a-view-with-fractional-values-of-the – Mokkun Mar 07 '14 at 09:20
  • 1
    Using fractions (80%) will not work with an object animator. Compiler complains, as the commenter above noted. – dennisdrew Oct 15 '14 at 19:35