从Android 4.4(nexus 5)上的帧跳转开始的ObjectAnimator,但在4.1设备上没有 [英] ObjectAnimator starting with a frame jump on Android 4.4 (nexus 5) but not in 4.1 device

查看:141
本文介绍了从Android 4.4(nexus 5)上的帧跳转开始的ObjectAnimator,但在4.1设备上没有的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的活动,它显示了ObjectAnimator的动画.该动画是在活动的onCreate方法中创建并启动的,它是一个非常简单的动画:

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.

问题是,在我的nexus 5(Android 4.4原始版)中,活动开始时云正在跳帧.

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

此跳转仅在我的关系5中可见,因为我也在使用android 4.1的华为asyend y300 devide中测试了该应用,并且跳转不可见,移动非常顺畅.

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.

ObjectAnimator和Android 4.4有什么问题?

What is wrong with ObjectAnimator and Android 4.4?

谢谢

推荐答案

在onCreate中启动动画不是一个好主意.当用户最终能够看到此动画时(在将活动放大并显示在屏幕上并带有动画等之后),该动画不是在它的开始,而是在它的后面,因此用户将错过该动画的开始或也许然后也会看到一些帧下降.最终结果确实取决于设备,Android版本,标准的窗口动画样式等.

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.

如果要在创建活动后立即启动动画,请使用 onWindowFocusChanged 方法: http://developer.android.com/reference/android/app/Activity.html#onWindowFocusChanged(boolean)

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.窗口具有焦点( hasFocus == true)-用户可见
    1. Window has focus (hasFocus==true) - it's visible to the user

    2.创建指示动画已经开始的布尔变量,因此将仅启动一次
    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();
   }
}

因此,在onCreate中创建 cloudAnim 对象很好,但应使用onWindowFocusChanged方法启动它.

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

这篇关于从Android 4.4(nexus 5)上的帧跳转开始的ObjectAnimator,但在4.1设备上没有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆