Android 4.4 rotate animation blinks when animating

2019-03-31 04:00发布

问题:

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

and:

public static void rotate(final View view) {

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

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

}

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

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>

回答1:

I had a similar issue:

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"


回答2:

i find the solution for this problem. Object Animator worked for me very well.

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