2

I have a problem. I created a custom amimation using the following code:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

  <objectAnimator
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:duration="500"
      android:propertyName="x"
      android:valueFrom="0"
      android:valueTo="-1000"
      android:valueType="floatType" />

</set>

Now it works almost like I want, because I am missing a part of my screen. So my question is: How can I make this animation using percentages instead of values?

EDIT:
Here is the code about my fragmentanimation:

var trans = FragmentManager.BeginTransaction();

DisplayMetrics metrics = Resources.DisplayMetrics;
float screenWidth = metrics.WidthPixels;//calculate screen width
double wantValue = 0.5;//set a percentages

ObjectAnimator SlideLeft = ObjectAnimator.OfFloat(trans, "translationX", 0, (float)(screenWidth * wantValue));
SlideLeft.SetDuration(500);
SlideLeft.Start();

ObjectAnimator SlideRight = ObjectAnimator.OfFloat(trans, "translationX", 0, (float)(screenWidth * -wantValue));
SlideLeft.SetDuration(500);
SlideLeft.Start();

trans.SetCustomAnimations(SlideRight, SlideLeft);

With the error:

cannot convert from 'Android.Animation.ObjectAnimator' to 'int'

A. Vreeswijk
  • 822
  • 1
  • 19
  • 57

1 Answers1

1

This is impossible in XML to do.However you can set percentages in cs code.

TextView textView = FindViewById<TextView>(Resource.Id.text_content);
// Here using a textView to have a test    

DisplayMetrics metrics = Resources.DisplayMetrics;
float screenWidth = metrics.WidthPixels;//calculate screen width
double wantValue = 0.5;//set a percentages
ObjectAnimator objectAnimator = ObjectAnimator.OfFloat(textView, "translationX", 0, (float)(screenWidth*wantValue));
objectAnimator.SetDuration(3000);
objectAnimator.Start();

UPDATE:

And how can I use this for fragments then, because I tried your code with trans.SetCustomAnimations(SlideRight, SlideLeft);

If you want to use SetCustomAnimations to set animations , you shoud can set percentages in XML.However not using objectAnimator, is translate.

slide_left_in.xml:

<?xml version="1.0" encoding="utf-8" ?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

  <translate
      android:fromXDelta="-100%p"
      android:toXDelta="0%p"
      android:duration="500"/>

  <alpha
      android:fromAlpha="0.5"
      android:toAlpha="1.0"
      android:duration="500"/>

</set>

slide_left_out.xml:

<?xml version="1.0" encoding="utf-8" ?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

  <translate
      android:fromXDelta="0%p"
      android:toXDelta="-100%p"
      android:duration="500"/>

  <alpha
      android:fromAlpha="1.0"
      android:toAlpha="0.5"
      android:duration="500"/>

</set>

slide_right_in.xml:

<?xml version="1.0" encoding="utf-8" ?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

  <translate
      android:fromXDelta="100%p"
      android:toXDelta="0%p"
      android:duration="@integer/card_flip_time_full"/>

  <alpha
      android:fromAlpha="0.5"
      android:toAlpha="1.0"
      android:duration="@integer/card_flip_time_full"/>

</set>

slide_right_out.xml:

<?xml version="1.0" encoding="utf-8" ?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

  <translate
      android:fromXDelta="0%p"
      android:toXDelta="100%p"
      android:duration="500"/>

  <alpha
      android:fromAlpha="1.0"
      android:toAlpha="0.5"
      android:duration="500"/>

</set>

Finally , set in code:

var details = Android.Support.V4.App.Fragment.FragmentManager.FindFragmentById(Resource.Id.details) as DetailsFragment;
var trans = Android.Support.V4.App.Fragment.FragmentManager.BeginTransaction();
trans.Replace(Resource.Id.details, details);
trans.SetCustomAnimations(Resource.Animation.slide_right_in,Resource.Animation.slide_left_out,Resource.Animation.slide_left_in,Resource.Animation.slide_right_out);
trans.Commit();

If happen this error :

Unknown animator name: translate occurred

you need to change to V4 Version of Fragment:

using Fragment = Android.Support.V4.App.Fragment;
Junior Jiang
  • 12,430
  • 1
  • 10
  • 30
  • And what is `textView`? – A. Vreeswijk May 08 '19 at 05:57
  • @A.Vreeswijk `textView` is a TextView in my project, you can replace it with your view.I have updated answer. – Junior Jiang May 08 '19 at 05:59
  • And how can I use this for fragments then, because I tried your code with `trans.SetCustomAnimations(SlideRight, SlideLeft);` But then it says that it can't convert Animation to int. What am I doing wrong? I created 2 ObjectAnimators btw. – A. Vreeswijk May 08 '19 at 06:07
  • @A.Vreeswijk Okey, you can show more code about `SetCustomAnimations` in question.I will check it. – Junior Jiang May 08 '19 at 06:12
  • @A.Vreeswijk Thanks for updating question.I have updated answer. – Junior Jiang May 08 '19 at 06:51
  • I get this error: `Unhandled Exception: Java.Lang.RuntimeException: Unable to start activity ComponentInfo{appname.appname/md5daf804607252bb921d2f5ad4f62be09e.MainActivity}: java.lang.RuntimeException: Unknown animator name: translate occurred` – A. Vreeswijk May 08 '19 at 07:00
  • @A.Vreeswijk Update code in question.And here is a similar discussion for refer.https://stackoverflow.com/questions/4932462/animate-the-transition-between-fragments?lq=1 – Junior Jiang May 08 '19 at 07:16
  • @A.Vreeswijk You should set four paramaters in `SetCustomAnimations` , this is not similar with `objectAnimator`. – Junior Jiang May 08 '19 at 07:33
  • I did exactly what he did: https://stackoverflow.com/a/33992609/10673107. But still error with `translate`?! – A. Vreeswijk May 08 '19 at 07:42
  • @A.Vreeswijk I have reproduced this error, you should use `Android.Support.V4.App.Fragment` to **SetCustomAnimations**.Then this error will disappear. – Junior Jiang May 08 '19 at 09:36
  • @A.Vreeswijk Have you solved it, you can share sample project.I will check it. – Junior Jiang May 09 '19 at 06:12
  • I actually did. I forgot to set the Activity to FragmentActivity, because only then I can use translate! – A. Vreeswijk May 09 '19 at 07:29
  • @A.Vreeswijk Yeah , this need to change to `FragmentActivity`. *.^ – Junior Jiang May 09 '19 at 07:31
  • @A.Vreeswijk Glad be helpful .Remember to vote up ,thanks in advance :) – Junior Jiang May 09 '19 at 08:45