【问题标题】:Android 4.4 rotate animation blinks when animating nested view动画嵌套视图时Android 4.4旋转动画闪烁
【发布时间】:2016-05-18 16:04:11
【问题描述】:

当我将我的 android 应用程序迁移到 4.4 时,我注意到一个非常奇怪的情况,突然我的 HUD 组件中的旋转动画开始闪烁并且无法正常工作。我正在尝试像这样旋转对象:

rotateAnimation = new RotateAnimation(180, 360, Animation.RELATIVE_TO_SELF,
        0.5f,  Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(1000);
rotateAnimation.setRepeatCount(Animation.INFINITE);
rotateAnimation.setRepeatMode(Animation.RESTART);

和:

public static void rotate(final View view) {

if(view.getAnimation() != null) {
    view.getAnimation().cancel();
    view.clearAnimation();
}

view.setAnimation(rotateAnimation);
view.getAnimation().start();

}

上面的代码在用于任何其他非嵌套视图对象时工作得很好,但当用于包含标签的ImageView 时,它根本不起作用。奇怪的是,在同一上下文中调用的同一对象上的其他动画有效。这是4.4的错误吗?我包括如下视图:

    <include
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        layout="@layout/view_trip_hud"
        android:layout_gravity="bottom"/>

HUD 组件本身看起来像这样:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:alpha="0.6"
    android:background="@drawable/background_texture_dark"
    tools:context=".app.ui.fragment.TripSummaryFragment"
    android:gravity="center_vertical|left"
    android:id="@+id/relativeLayoutHUDContainer">


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_vertical"
        android:padding="@dimen/ui_default_element_margin">

        <ImageView
                android:id="@+id/imageViewTrackingStatus"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/status_preparing"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true" />

        <TextView
                android:id="@+id/textViewDriveQuality"
                style="@style/HUDFont"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:text="@string/track_status_inactive"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true" />

    </LinearLayout>

    <view
        android:id="@+id/linearLayoutHUDCurrentSpeed"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        class="pl.com.infinitysoftware.carassistant.util.ui.HUDBlockLayout"
        style="@style/HUDBlock"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="@dimen/ui_min_element_margin">

    <TextView
            android:id="@+id/textViewCurrentSpeed"
            style="@style/HUDFont"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="-,-Km/h"
            android:clickable="false"
            android:layout_gravity="center_vertical"
            android:gravity="center_vertical"/>
        </view>

    <view
        class="pl.com.infinitysoftware.carassistant.util.ui.HUDBlockLayout"
        style="@style/HUDBlock"
        android:id="@+id/linearLayoutHUDCurrentDistance"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/linearLayoutHUDCurrentSpeed"
        android:layout_marginLeft="@dimen/ui_min_element_margin">

        <TextView
            style="@style/HUDFont"
            android:id="@+id/textViewCurrentDistance"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="25 Km"
            android:clickable="false"
            android:layout_gravity="center_vertical"
            android:gravity="center_vertical" />
    </view>


</RelativeLayout>

【问题讨论】:

    标签: android android-animation


    【解决方案1】:

    我也有类似的问题:

    尝试像这样设置您正在应用动画的视图(在动画期间):

    view.setLayerType(View.LAYER_TYPE_HARDWARE,null);
    //And then apply the animation
    view.startAnimation(myAnimation);
    

    别忘了把它放在你的清单中:

    android:hardwareAccelerated="true"
    

    【讨论】:

    • 关闭硬件加速后,硬件层的行为与软件层完全相同。请阅读:link
    • 它在 Android 5.0 中停止工作(再次闪烁)。有谁知道怎么回事?
    • 在 5.0 中,我可以通过将 android:layerType="hardware" 添加到视图 xml 中而不是动态调用 view.setLayerType 来修复它。也许这会对某人有所帮助:)
    • 谢谢,你让我的生活更轻松了。
    【解决方案2】:

    我找到了解决这个问题的方法。 Object Animator 非常适合我。

    ObjectAnimator floatingButtonAnimator = ObjectAnimator.ofFloat(floatingActionButton,
    ANIMATION_NAME, ANIMATION_STARTING_DEGREE, ANIMATION_ENDING_DEGREE);
    floatingButtonAnimator.setDuration(ANIMATION_DURATION).addListener(new AnimatorListenerAdapter() {
      @Override
      public void onAnimationStart(Animator animation) {
        super.onAnimationStart(animation);
      }
    
      @Override
      public void onAnimationEnd(Animator animation) {
        super.onAnimationEnd(animation);
        exchangeValues();
      }
    });
    floatingButtonAnimator.start();
    

    【讨论】:

      猜你喜欢
      • 2020-03-04
      • 2012-03-12
      • 1970-01-01
      • 2012-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-09
      • 2011-12-06
      相关资源
      最近更新 更多