ObjectAnimator starting with a frame jump on Android 4.4 (nexus 5) but not in 4.1 device

ぐ巨炮叔叔 提交于 2019-12-11 05:29:38

问题


I have a simple activity that shows an animation with ObjectAnimator. The animation is created and started in onCreate method of the activity, it is a very simple animation:

cloudAnim = ObjectAnimator.ofFloat(cloud1ImageView, "x", sw);
        cloudAnim.setDuration(35000);
        cloudAnim.setRepeatCount(ValueAnimator.INFINITE);
        cloudAnim.setRepeatMode(ValueAnimator.RESTART);
        cloudAnim.setInterpolator(null);
        cloudAnim.start();

it simply displays a cloud on the left of the screen and moves from the left to the right.

The problem is that in my nexus 5 (android 4.4 lastet version) the cloud is doing a frame jump when the activity starts.

This jump is only visible in my nexus 5, because i'm testing the app also in a huawei ascend y300 devide with android 4.1 and the jump is not visible, the movement is very smooth.

What is wrong with ObjectAnimator and Android 4.4?

Thanks


回答1:


Starting animations in onCreate is not a good idea. When user will finally be able to see this animation (after activity being inflated and displayed on the screen with animation etc.) the animation is not in it's beginning but a bit after it, so user will miss the very beginning of the animation or perhaps will see some frame drops then as well. The final result really depends on the device, android version, standard window animations styles etc.

If you want to launch an animation right after creating an activity make use of onWindowFocusChanged method: http://developer.android.com/reference/android/app/Activity.html#onWindowFocusChanged(boolean)

Called when the current Window of the activity gains or loses focus. This is the best indicator of whether this activity is visible to the user.


In addition you need to do some checks:

    1. Window has focus (hasFocus==true) - it's visible to the user
    2. Create boolean variable indicating that animation was already started, so it will be launched only once
private boolean cloudAnimStarted;

@Override
public void onWindowFocusChanged (boolean hasFocus) {
   super.onWindowFocusChanged(hasFocus);
   if (hasFocus && !cloudAnimStarted) {
       cloudAnimStarted = true;
       cloudAnim.start();
   }
}

So creating a cloudAnim object is fine in onCreate, but launching it should be done in onWindowFocusChanged method instead.



来源:https://stackoverflow.com/questions/24721378/objectanimator-starting-with-a-frame-jump-on-android-4-4-nexus-5-but-not-in-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!