wujek
wujek

Reputation: 11040

Translation X animation ends immediately no matter the duration

I have the following code in a click listener on the whole view:

    float newPosition = value / MAX_VALUE * scaleWidth; // value is float
    ObjectAnimator a = ObjectAnimator.ofFloat(indicatorView, "translationX", indicatorView.getTranslationX(), newPosition);
    a.setDuration(2000);
    a.addListener(new Animator.AnimatorListener() {

        @Override
        public void onAnimationStart(Animator animation) {
            Log.e("test", "start");
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            Log.e("test", "end");
        }

        @Override
        public void onAnimationCancel(Animator animation) {
            Log.e("test", "cancel");
        }

        @Override
        public void onAnimationRepeat(Animator animation) {
            Log.e("test", "repeat");
        }
    });
    a.start();

No matter what I do, no matter the duration I set, the animation is not played and the moved view is just moved to the new (correct, though) position. The log output looks like this:

05-25 12:46:22.477: ERROR/test(2266): start
05-25 12:46:22.477: ERROR/test(2266): end

which means the animation is finished right after it is started. I'm testing the code on a 4.4.2 device, the min and target SDK in the manifest are 19. I'm not using NineOldAndroids. I'm trying to follow this tutorial: http://cogitolearning.co.uk/?p=1290, where it apparently seems to be working.

I have the same issue with this line of code:

indicatorView.animate().translationX(newPosition).setDuration(2000).start();

This is the layout XML:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scale"
    android:layout_marginLeft="50dp"
    android:layout_marginTop="100dp"
    android:layout_width="500dp"
    android:layout_height="75dp"
    android:background="@android:color/holo_red_light">

    <ImageView
        android:id="@+id/indicator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/indicator" />

</FrameLayout>

Any help?

EDIT: I downloaded the mentioned project and built and none of the animations there seem to work for me, neither do any of the code snippets found in the internet. Seems to me that something with my phone is going awry.

Upvotes: 2

Views: 587

Answers (1)

wujek
wujek

Reputation: 11040

I feel so stupid now. It turns out I disabled animations in Android Developer Settings. What I did now was to enable them again (by setting scale to 1x) and restarted the phone - and it works. The weird thing is the 'old' View animations worked without any problems, and the 'new' ones (from API level 11) did not. Huh?

Upvotes: 5

Related Questions