Androids ObjectAnimator.ofFloat doesn't work p

2019-02-12 11:00发布

问题:

I've used ObjectAnimator.ofFloat in an Android App wich doesn't work on every Device the same way.

MainActivity (extends Activity):

Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        startAnimation();
    }
});


public void startAnimation() {
    ImageView aniView = (ImageView) findViewById(R.id.imageView1);
    ObjectAnimator fadeOut = ObjectAnimator.ofFloat(aniView, "alpha", 0f);
    fadeOut.setDuration(2000);

    ObjectAnimator mover = ObjectAnimator.ofFloat(aniView, "translationX", -500f, 0f);
    mover.setInterpolator(new TimeInterpolator() {
        @Override
        public float getInterpolation(float input) {
            Log.v("MainActivity", "getInterpolation() " + String.format("%.4f", input));
            return input;
        }
    });
    mover.setDuration(2000);

    ObjectAnimator fadeIn = ObjectAnimator.ofFloat(aniView, "alpha", 0f, 1f);
    fadeIn.setDuration(2000);

    AnimatorSet animatorSet = new AnimatorSet();

    animatorSet.play(mover).with(fadeIn).after(fadeOut);
    animatorSet.start();
}

Samsung Galaxy S4 (Android 4.4.2):

    getInterpolation() 1,0000
    getInterpolation() 1,0000

Samsung Galaxy S5 (Android 4.4.2):

    getInterpolation() 0,0000
    getInterpolation() 0,0000
    getInterpolation() 0,0085
    getInterpolation() 0,0170
    getInterpolation() 0,0255
    ...
    ...
    getInterpolation() 0,9740
    getInterpolation() 0,9825
    getInterpolation() 0,9910
    getInterpolation() 0,9995
    getInterpolation() 1,0000

Has anyone an Idea, why this doesn't work properly?

回答1:

On the Galaxy S4, under Developer Options, there is the option Animator duration scale. For some wild reason, this is off by default. After switching this to 1x, my animations on the S4 started to work perfectly. This may be what is causing your problem.