javaandroidxmlandroid-animationandroid-percent-library

Android Animations on Percent Library


I have been researching the topic of animations on Android but have come against a brick wall when it comes to implementing these findings against a project that utilises the Percent library.

In particular, I have the following element in my layout xml:

 <ImageView
        android:id="@+id/aImage"
        android:src="@drawable/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_marginTopPercent="35%"
        android:layout_centerHorizontal="true"
        android:visibility="invisible"/>

With the following attribute assigned to the root PercentRelativeLayout element:

xmlns:app="http://schemas.android.com/apk/res-auto"

Now, I want to create an AnimatorSetobject that can carry out multiple manipulations with the focus being on the app:layout_marginTopPercent="35%" attribute.

I have tried creating an ObjectAnimator and adding it to an AnimatorSet but this has no effect:

ObjectAnimator anim1 = ObjectAnimator.ofFloat(logoImageView, "layout_marginTopPercent", 0.35f, 0.1f);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playSequentially(anim1);
animatorSet.start(); 

Could anybody please explain where I am going wrong. Interestingly, I am able to create a animation .xml file and use the translate element to perform a successful animation but I need to do this in code. Below is an example of the successful .XML animation:

<set>
        <translate
         android:fromYDelta="0%p"
         android:toYDelta="-25%p"
         android:duration="1000" />
</set>

Thanks in advance


Solution

  • The new version (>=1.3.0) of my library ViewPropertyObjectAnimator provides a way to animate percent parameters from Percent Support Library. Obtaining an ObjectAnimator (which you can use inside your AnimatorSet) is as easy as using ViewPropertyAnimator.

    ObjectAnimator logoMarginAnimator = 
                ViewPropertyObjectAnimator.animate(logoImageView).topMarginPercent(0.1f).get();
    AnimatorSet animatorSet = new AnimatorSet();
    animatorSet.play(logoMarginAnimator);
    animatorSet.start();