0

I am trying to implement a bunch of animations on different views which are components of a splash screen.

I have read up on Property Animations on the developer site but I can't figure out how to animate a view(translate + scale) to the centre of the parent layout. If I pass the value 50% or 50% p to the valueFrom attribute in it crashes saying that it is an invalid value.

How do I achieve this? My current animator xml is below.

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<set>
    <objectAnimator
        android:duration="1000"
        android:propertyName="x"
        android:valueFrom="50%"
        android:valueTo="50%" />
    <objectAnimator
        android:duration="1000"
        android:propertyName="y"
        android:valueFrom="0"
        android:valueTo="50%" />
</set>

1
  • Can the person who down voted please elaborate or suggest an edit to the above question?
    – Rohan
    Oct 1, 2015 at 14:24

1 Answer 1

1

You can do it in code programmatically:

The following code animates a view from the top left to the center of the screen.

    // Run on UI thread so that the height and widths are set
    Runnable r = new Runnable() {

        @Override
        public void run() {
            // Assign the animation which makes it drop down into view
            View view = yourParentView.findViewById(R.id.view);
            View yourParentView = findViewById(R.id.parent);
            int parentHeight = yourParentView.getHeight();
            int parentWidth = yourParentView.getWidth();
            int viewHeight = view.getHeight();
            int viewWidth = view.getWidth();

            Animation animation = getAnimation((parentHeight / 2) - (viewHeight / 2), (parentWidth / 2) - (viewWidth / 2));
            view.startAnimation(animation);
        }

    };
    new Handler(Looper.getMainLooper()).post(r );

private static AnimationSet getAnimation(int x, int y)  {

    AnimationSet animSet = new AnimationSet(false);
    animSet.addAnimation(new TranslateAnimation(0, 0, x, y));
    animSet.setDuration(DURATION);
    return animSet;
}
7
  • Hi @Rob. I am trying to achieve this using property animations. Also I want to keep animations in xmls(as far as possible)
    – Rohan
    Oct 1, 2015 at 13:06
  • @Rohan check my edit and see if that is useful, if so I will amend my answer to remove the first bit.
    – RobVoisey
    Oct 1, 2015 at 14:04
  • Thanks for the quick response. The xml bit did not work as well.
    – Rohan
    Oct 1, 2015 at 14:15
  • xFraction and yFraction are not really properties of an ImageView(Or view) right? How is it supposed to animate between those values if the property itself does not exist?
    – Rohan
    Oct 1, 2015 at 14:19
  • @Rohan Is there any reason it has to be an object animator? I think using percentages is supported using a Translation animation e.g. <translate android:fromYDelta="0%" android:toYDelta="-70%" android:duration="1000"/> but not sure about objectAnimator.
    – RobVoisey
    Oct 1, 2015 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.