给嵌套视图设置动画时,Android 4.4旋转动画闪烁 [英] Android 4.4 rotate animation blinks when animating nested view

查看:90
本文介绍了给嵌套视图设置动画时,Android 4.4旋转动画闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I noticed a very strange situation when migrated my android app to 4.4 Suddenly the rotate animation in my HUD component started to blink and not working. I am trying to rotate object like this:

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();

}

上面的代码在任何其他未嵌套的视图对象上使用时都可以正常工作,但是当用于include标签中包含的ImageView时,它根本不起作用.奇怪的是,在同一上下文中调用的同一对象上的其他动画也有效.这是一个4.4错误吗?我包括以下视图:

The code above works just fine when used on any other not nested view object, but when used for ImageView included with the include tag it simply does not work. Whats strange, other animations on the same object called within the same context works. Is this a 4.4 bug? I Include the view as follows:

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

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

And the HUD component itself looks like this:

<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>

推荐答案

我遇到了类似的问题:

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

Try to set the view you are applying the animation like this (during the time you animate it):

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

别忘了把它放在您的清单中

Don´t forget to put this in your manifest:

android:hardwareAccelerated="true"

这篇关于给嵌套视图设置动画时,Android 4.4旋转动画闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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