Ver código fonte

[메뉴][New] 에니메이션 darwable hardware layer 옵션 적용

hyodong.min 6 anos atrás
pai
commit
016eea7379

+ 85 - 0
app/src/main/java/kr/co/zumo/app/lifeplus/view/AnimationDrawableCallback.java

@@ -0,0 +1,85 @@
+/*
+ * COPYRIGHT (c) 2018 All rights reserved by HANWHA LIFE.
+ */
+package kr.co.zumo.app.lifeplus.view;
+
+import android.graphics.drawable.AnimationDrawable;
+import android.graphics.drawable.Drawable;
+import android.view.View;
+
+
+/**
+ * Provides a callback when a non-looping {@link AnimationDrawable} completes its animation sequence. More precisely,
+ * {@link #onAnimationComplete()} is triggered when {@link View#invalidateDrawable(Drawable)} has been called on the
+ * last frame.
+ *
+ * @author Benedict Lau
+ */
+public abstract class AnimationDrawableCallback implements Drawable.Callback {
+
+  /**
+   * The last frame of {@link Drawable} in the {@link AnimationDrawable}.
+   */
+  private Drawable mLastFrame;
+
+  /**
+   * The client's {@link Drawable.Callback} implementation. All calls are proxied to this wrapped {@link Drawable.Callback}
+   * implementation after intercepting the events we need.
+   */
+  private Drawable.Callback mWrappedCallback;
+
+  /**
+   * Flag to ensure that {@link #onAnimationComplete()} is called only once, since
+   * {@link #invalidateDrawable(Drawable)} may be called multiple times.
+   */
+  private boolean mIsCallbackTriggered = false;
+
+  /**
+   *
+   * @param animationDrawable
+   *            the {@link AnimationDrawable}.
+   * @param callback
+   *            the client's {@link Drawable.Callback} implementation. This is usually the {@link View} the has the
+   *            {@link AnimationDrawable} as background.
+   */
+  public AnimationDrawableCallback(AnimationDrawable animationDrawable, Drawable.Callback callback) {
+    mLastFrame = animationDrawable.getFrame(animationDrawable.getNumberOfFrames() - 1);
+    mWrappedCallback = callback;
+  }
+
+  @Override
+  public void invalidateDrawable(Drawable who) {
+    if (mWrappedCallback != null) {
+      mWrappedCallback.invalidateDrawable(who);
+    }
+
+    if (!mIsCallbackTriggered && mLastFrame != null && mLastFrame.equals(who.getCurrent())) {
+      mIsCallbackTriggered = true;
+      onAnimationComplete();
+    }
+  }
+
+  @Override
+  public void scheduleDrawable(Drawable who, Runnable what, long when) {
+    if (mWrappedCallback != null) {
+      mWrappedCallback.scheduleDrawable(who, what, when);
+    }
+  }
+
+  @Override
+  public void unscheduleDrawable(Drawable who, Runnable what) {
+    if (mWrappedCallback != null) {
+      mWrappedCallback.unscheduleDrawable(who, what);
+    }
+  }
+
+  //
+  // Public methods.
+  //
+
+  /**
+   * Callback triggered when {@link View#invalidateDrawable(Drawable)} has been called on the last frame, which marks
+   * the end of a non-looping animation sequence.
+   */
+  public abstract void onAnimationComplete();
+}

+ 9 - 0
app/src/main/java/kr/co/zumo/app/lifeplus/view/animation/NotiAnimation.java

@@ -4,9 +4,11 @@
 package kr.co.zumo.app.lifeplus.view.animation;
 
 import android.graphics.drawable.AnimationDrawable;
+import android.view.View;
 import android.widget.ImageView;
 
 import kr.co.zumo.app.R;
+import kr.co.zumo.app.lifeplus.view.AnimationDrawableCallback;
 
 /**
  * NotiAnimation
@@ -23,8 +25,15 @@ public class NotiAnimation {
   private AnimationDrawable animationDrawable;
 
   public void show(ImageView target) {
+    target.setLayerType(View.LAYER_TYPE_HARDWARE, null);
     target.setImageResource(R.drawable.noti_animation);
     animationDrawable = (AnimationDrawable) target.getDrawable();
+    animationDrawable.setCallback(new AnimationDrawableCallback(animationDrawable, target) {
+      @Override
+      public void onAnimationComplete() {
+        target.setLayerType(View.LAYER_TYPE_NONE, null);
+      }
+    });
     animationDrawable.setOneShot(true);
     animationDrawable.start();
   }