csnewb
csnewb

Reputation: 1280

Android - How to use ObjectAnimator in XML

I simply want to use an objectAnimator which is defined in a XML-file. I want to put two objectAnimators and want to choose in my Code which I want to use.

This is how my XML-File looks like where I set the propertyName, that I wanna access later on in the code:

<?xml version"1.0" encodin="utf-8"?>
<set xmlns:android="..."
    <objectAnimator
        android:propertyName="animX"
        android:duration="1000"
        android:valueFrom="FFFFFF"
        android:valueTo="FF0000" />

    <objectAnimator
        android:propertyName="animY"
        android:duration="1000"
        android:valueFrom="FF0000"
        android:valueTo="FFFFFF" />

</set>

That is the code, where I want to access to a propertyName defined objectAnimator:

ObjectAnimator anim = ObjectAnimator.ofFloat(view, "animX");
anim.setTarget(anim);
anim.start();

Unfortunately, that is not how it works and I am really struggling to find a solution to access the objectAnimators I want.

Upvotes: 4

Views: 10087

Answers (3)

Hayk Mkrtchyan
Hayk Mkrtchyan

Reputation: 3265

Instead of putting 2 ObjectAnimators in your XML file, you could rather use PropertyValuesHolder and wrap them in one ObjectAnimator like this:

<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="5000"
    android:interpolator="@android:interpolator/linear"
    android:repeatCount="1"
    android:repeatMode="reverse">

    <propertyValuesHolder
        android:propertyName="scaleX"
        android:valueFrom="1"
        android:valueTo="2" />

    <propertyValuesHolder
        android:propertyName="scaleY"
        android:valueFrom="1"
        android:valueTo="2" />

</objectAnimator>
    

Then you can retrieve this animation like this:

val scaleAnimation = AnimatorInflater.loadAnimator(
        context, R.animator.your_anim_file_name)
scaleAnimation.setTarget('your_view')
scaleAnimation.start()

Or if you have multiple animations like this, you can add them in AnimatorSet and play sequentially or together.

val animatorSet = AnimatorSet()
animatorSet.playSequentially(scaleAnimation, translateAnimation)
animatorSet.start()

Upvotes: 2

AllDayAmazing
AllDayAmazing

Reputation: 2383

One of two things are the issue:

1) each ObjectAnimator needs to be it's own and then added to a set after you inflate the animator (via final ObjectAnimator animator = (ObjectAnimator) AnimatorInflater.loadAnimator(context, resID);) and set it on the view

2) If the XML is giving you IDE errors, check that your ObjectAnimators are in the /animator folder and not the /anim folder in the /res directory

Edited: /animators was not recognized, but /animator was

Upvotes: 3

j2emanue
j2emanue

Reputation: 62559

Can you not create your own objectanimators programatically ?

ObjectAnimator objectAnimator1 = new ObjectAnimator();
objectAnimator.setPropertyName("animX");
objectAnimator.setFloatValues(0, 1);
objectAnimator.setTarget(logoView);//call this when your ready to set target
objectAnimator.setDuration(1000);

and then create another one and store them as instance variables perhaps ?

Update

So just create a anim folder in res. and create Two different xml files. Call each one respectively.

Upvotes: 0

Related Questions