From f93204908f43b6b1ff46b78a1bca97b6f1f44d63 Mon Sep 17 00:00:00 2001 From: daimajia Date: Wed, 27 Aug 2014 15:11:18 +0800 Subject: [PATCH] fix #4 --- .../androidviewhover/demo/MainActivity.java | 12 +- library/gradle-mvn-push.gradle | 1 - .../daimajia/androidviewhover/BlurLayout.java | 147 ++++++++---------- .../proxy/AnimationProxy.java | 124 +++++++++++++++ 4 files changed, 193 insertions(+), 91 deletions(-) create mode 100644 library/src/main/java/com/daimajia/androidviewhover/proxy/AnimationProxy.java diff --git a/demo/src/main/java/com/daimajia/androidviewhover/demo/MainActivity.java b/demo/src/main/java/com/daimajia/androidviewhover/demo/MainActivity.java index e401376..5df7d42 100644 --- a/demo/src/main/java/com/daimajia/androidviewhover/demo/MainActivity.java +++ b/demo/src/main/java/com/daimajia/androidviewhover/demo/MainActivity.java @@ -45,13 +45,13 @@ public void onClick(View v) { }); mSampleLayout.setHoverView(hover); mSampleLayout.setBlurDuration(550); - mSampleLayout.addChildAppearAnimator(hover, R.id.heart, Techniques.FlipInX); - mSampleLayout.addChildAppearAnimator(hover, R.id.share, Techniques.FlipInX); - mSampleLayout.addChildAppearAnimator(hover, R.id.more, Techniques.FlipInX); + mSampleLayout.addChildAppearAnimator(hover, R.id.heart, Techniques.FlipInX, 550, 0); + mSampleLayout.addChildAppearAnimator(hover, R.id.share, Techniques.FlipInX, 550, 250); + mSampleLayout.addChildAppearAnimator(hover, R.id.more, Techniques.FlipInX, 550, 500); - mSampleLayout.addChildDisappearAnimator(hover, R.id.heart, Techniques.FlipOutX); - mSampleLayout.addChildDisappearAnimator(hover, R.id.share, Techniques.FlipOutX); - mSampleLayout.addChildDisappearAnimator(hover, R.id.more, Techniques.FlipOutX); + mSampleLayout.addChildDisappearAnimator(hover, R.id.heart, Techniques.FlipOutX, 550, 500); + mSampleLayout.addChildDisappearAnimator(hover, R.id.share, Techniques.FlipOutX, 550, 250); + mSampleLayout.addChildDisappearAnimator(hover, R.id.more, Techniques.FlipOutX, 550, 0); mSampleLayout.addChildAppearAnimator(hover, R.id.description, Techniques.FadeInUp); mSampleLayout.addChildDisappearAnimator(hover, R.id.description, Techniques.FadeOutDown); diff --git a/library/gradle-mvn-push.gradle b/library/gradle-mvn-push.gradle index a3b5bf8..ab2ef18 100644 --- a/library/gradle-mvn-push.gradle +++ b/library/gradle-mvn-push.gradle @@ -127,6 +127,5 @@ afterEvaluate { project -> archives androidSourcesJar archives androidJavadocsJar archives apklib - archives jar } } \ No newline at end of file diff --git a/library/src/main/java/com/daimajia/androidviewhover/BlurLayout.java b/library/src/main/java/com/daimajia/androidviewhover/BlurLayout.java index ae29ead..3d5d47f 100644 --- a/library/src/main/java/com/daimajia/androidviewhover/BlurLayout.java +++ b/library/src/main/java/com/daimajia/androidviewhover/BlurLayout.java @@ -15,6 +15,7 @@ import com.daimajia.androidanimations.library.Techniques; import com.daimajia.androidanimations.library.YoYo; +import com.daimajia.androidviewhover.proxy.AnimationProxy; import com.daimajia.androidviewhover.tools.Blur; import com.daimajia.androidviewhover.tools.Util; import com.nineoldandroids.animation.Animator; @@ -55,10 +56,8 @@ public class BlurLayout extends RelativeLayout { private Animator mHoverDisappearAnimator; private YoYo.AnimationComposer mHoverDisappearAnimationComposer; - private HashMap> mChildAppearAnimationComposers = new HashMap>(); - private HashMap> mChildDisappearAnimationComposers = new HashMap>(); - private HashMap> mChildAppearAnimators = new HashMap>(); - private HashMap> mChildDisappearAnimators = new HashMap>(); + private HashMap> mChildAppearAnimators = new HashMap>(); + private HashMap> mChildDisappearAnimators = new HashMap>(); private long mBlurDuration = DURATION; @@ -130,12 +129,12 @@ private boolean hover(){ @Override public void onGlobalLayout() { + startChildrenAppearAnimations(); + startBlurImageAppearAnimator(); startHoverAppearAnimator(); - startChildrenAppearAnimations(); - if(Build.VERSION.SDK_INT >= 16) mHoverView.getViewTreeObserver().removeOnGlobalLayoutListener(this); else @@ -311,15 +310,8 @@ private void startHoverDisappearAnimator(){ } private void startChildrenAppearAnimations(){ - for(Map.Entry> entry : mChildAppearAnimationComposers.entrySet()){ - Util.reset(entry.getKey()); - for(YoYo.AnimationComposer composer : entry.getValue()){ - composer.playOn(entry.getKey()); - } - } - for(Map.Entry> entry : mChildAppearAnimators.entrySet()){ - Util.reset(entry.getKey()); - for(Animator animator : entry.getValue()){ + for(Map.Entry> entry : mChildAppearAnimators.entrySet()){ + for(AnimationProxy animator : entry.getValue()){ animator.start(); } } @@ -327,15 +319,10 @@ private void startChildrenAppearAnimations(){ private void startChildrenDisappearAnimations(){ for(View view : mChildDisappearAnimators.keySet()){ - for(Animator animator : mChildDisappearAnimators.get(view)){ + for(AnimationProxy animator : mChildDisappearAnimators.get(view)){ animator.start(); } } - for(Map.Entry> entry : mChildDisappearAnimationComposers.entrySet()){ - for(YoYo.AnimationComposer composer : entry.getValue()){ - composer.playOn(entry.getKey()); - } - } } public interface AppearListener { @@ -529,97 +516,89 @@ public void addChildAppearAnimator(View hoverView, int resId, Techniques techniq } public void addChildAppearAnimator(View hoverView, int resId, Techniques technique, long duration, long delay){ - addChildAppearAnimator(hoverView, resId, technique, duration, delay, null); + addChildAppearAnimator(hoverView, resId, technique, duration, delay, true, null); } - public void addChildAppearAnimator(View hoverView, int resId, Techniques technique, long duration, long delay, Interpolator interpolator){ - addChildAppearAnimator(hoverView, resId, technique, duration, delay, interpolator, new Animator.AnimatorListener[]{}); + public void addChildAppearAnimator(View hoverView, int resId, Techniques technique, long duration, long delay, boolean hiddenWhenDelaying){ + addChildAppearAnimator(hoverView, resId, technique, duration, delay, hiddenWhenDelaying, null); } - public void addChildAppearAnimator(View hoverView, int resId, Techniques technique, long duration, long delay, Interpolator interpolator, Animator.AnimatorListener... listeners){ - if(hoverView == null) - throw new IllegalStateException("Hover view is null"); - if(hoverView.findViewById(resId) == null) - throw new IllegalStateException("Can not find the child view"); - View child = hoverView.findViewById(resId); + public void addChildAppearAnimator(View hoverView, int resId, Techniques technique, long duration, long delay, boolean hiddenWhenDelaying, Interpolator interpolator){ + addChildAppearAnimator(hoverView, resId, technique, duration, delay, hiddenWhenDelaying, interpolator, new Animator.AnimatorListener[]{}); + } - YoYo.AnimationComposer composer = YoYo.with(technique).duration(duration).delay(delay).interpolate(interpolator); - for(Animator.AnimatorListener l : listeners) - composer.withListener(l); + public void addChildAppearAnimator(View hoverView, int resId, Techniques technique, long duration, long delay, boolean hiddenWhenDelaying, Interpolator interpolator, Animator.AnimatorListener... listeners){ + AnimationProxy executor = AnimationProxy.build(hoverView, resId, technique, duration, delay, hiddenWhenDelaying, interpolator, listeners); - if(mChildAppearAnimationComposers.get(child) == null){ - mChildAppearAnimationComposers.put(child, new ArrayList()); - } - composer.withListener(mGlobalListener); - composer.withListener(mGlobalAppearingAnimators); - mChildAppearAnimationComposers.get(child).add(composer); - } + View child = executor.getTarget(); - public void addChildAppearAnimator(View child, Animator animator){ - if(animator == null) - throw new IllegalStateException("animator can not be null"); - if(child == null) - throw new IllegalStateException("child view can not be null"); + if(mChildAppearAnimators.get(child) == null) + mChildAppearAnimators.put(child, new ArrayList()); - animator.setTarget(child); - if(mChildAppearAnimators.containsKey(child) == false){ - mChildAppearAnimators.put(child, new ArrayList()); - } - animator.addListener(mGlobalListener); - animator.addListener(mGlobalAppearingAnimators); - mChildAppearAnimators.get(child).add(animator); + executor.withListener(mGlobalListener); + executor.withListener(mGlobalAppearingAnimators); + child.setVisibility(INVISIBLE); + + mChildAppearAnimators.get(child).add(executor); } + public void addChildAppearAnimator(View hoverView, int childId, Animator animator){ + AnimationProxy executor = AnimationProxy.build(hoverView, childId, animator); + + View child = executor.getTarget(); + + if(mChildAppearAnimators.get(child) == null) + mChildAppearAnimators.put(child, new ArrayList()); + + executor.withListener(mGlobalListener); + executor.withListener(mGlobalAppearingAnimators); + mChildAppearAnimators.get(child).add(executor); + } public void addChildDisappearAnimator(View hoverView, int resId, Techniques technique){ addChildDisappearAnimator(hoverView, resId, technique, DURATION); } public void addChildDisappearAnimator(View hoverView, int resId, Techniques technique, long duration){ - addChildDisappearAnimator(hoverView, resId, technique, duration, 0); + addChildDisappearAnimator(hoverView, resId, technique, duration, 0, false); } public void addChildDisappearAnimator(View hoverView, int resId, Techniques technique, long duration, long delay){ - addChildDisappearAnimator(hoverView, resId, technique, duration, delay, null); + addChildDisappearAnimator(hoverView, resId, technique, duration, delay, false, null); } - public void addChildDisappearAnimator(View hoverView, int resId, Techniques technique, long duration, long delay, Interpolator interpolator){ - addChildDisappearAnimator(hoverView, resId, technique, duration, delay, interpolator, new Animator.AnimatorListener[]{}); + public void addChildDisappearAnimator(View hoverView, int resId, Techniques technique, long duration, long delay, boolean invisibleWhenDelaying){ + addChildDisappearAnimator(hoverView, resId, technique, duration, delay,invisibleWhenDelaying, null); } - public void addChildDisappearAnimator(View hoverView, int resId, Techniques technique, long duration, long delay, Interpolator interpolator, Animator.AnimatorListener... listeners){ - if(hoverView == null) - throw new IllegalStateException("Hover view is null"); - if(hoverView.findViewById(resId) == null) - throw new IllegalStateException("Can not find the child view"); + public void addChildDisappearAnimator(View hoverView, int resId, Techniques technique, long duration, long delay, boolean invisibleWhenDelaying, Interpolator interpolator){ + addChildDisappearAnimator(hoverView, resId, technique, duration, delay, invisibleWhenDelaying, interpolator, new Animator.AnimatorListener[]{}); + } - View child = hoverView.findViewById(resId); + public void addChildDisappearAnimator(View hoverView, int resId, Techniques technique, long duration, long delay, boolean invisibleWhenDelaying, Interpolator interpolator, Animator.AnimatorListener... listeners){ - YoYo.AnimationComposer composer = YoYo.with(technique).duration(duration).delay(delay).interpolate(interpolator); - for(Animator.AnimatorListener l : listeners){ - composer.withListener(l); - } - if(mChildDisappearAnimationComposers.containsKey(child) == false){ - mChildDisappearAnimationComposers.put(child, new ArrayList()); - } - composer.withListener(mGlobalListener); - composer.withListener(mGlobalDisappearAnimators); - mChildDisappearAnimationComposers.get(child).add(composer); + AnimationProxy executor = AnimationProxy.build(hoverView, resId, technique, duration, delay, invisibleWhenDelaying, interpolator, listeners); + + View child = executor.getTarget(); + if(mChildDisappearAnimators.containsKey(child) == false) + mChildDisappearAnimators.put(child, new ArrayList()); + + executor.withListener(mGlobalListener); + executor.withListener(mGlobalDisappearAnimators); + mChildDisappearAnimators.get(child).add(executor); } - public void addChildDisappearAnimator(View child, Animator animator){ - if(animator == null) - throw new IllegalStateException("animator can not be null"); - if(child == null) - throw new IllegalStateException("child view can not be null"); + public void addChildDisappearAnimator(View hoverView, int childId, Animator animator){ + AnimationProxy executor = AnimationProxy.build(hoverView, childId, animator); - animator.setTarget(child); - animator.addListener(mGlobalListener); - animator.addListener(mGlobalDisappearAnimators); - if(mChildDisappearAnimators.containsKey(child) == false){ - mChildDisappearAnimators.put(child, new ArrayList()); - } - mChildDisappearAnimators.get(child).add(animator); + View child = executor.getTarget(); + + if(mChildDisappearAnimators.get(child) == null) + mChildDisappearAnimators.put(child, new ArrayList()); + + executor.withListener(mGlobalListener); + executor.withListener(mGlobalDisappearAnimators); + mChildDisappearAnimators.get(child).add(executor); } public LayoutParams getFullParentSizeLayoutParams(){ diff --git a/library/src/main/java/com/daimajia/androidviewhover/proxy/AnimationProxy.java b/library/src/main/java/com/daimajia/androidviewhover/proxy/AnimationProxy.java new file mode 100644 index 0000000..00159a9 --- /dev/null +++ b/library/src/main/java/com/daimajia/androidviewhover/proxy/AnimationProxy.java @@ -0,0 +1,124 @@ +package com.daimajia.androidviewhover.proxy; + +import android.view.View; +import android.view.animation.Interpolator; + +import com.daimajia.androidanimations.library.Techniques; +import com.daimajia.androidanimations.library.YoYo; +import com.nineoldandroids.animation.Animator; + +public class AnimationProxy implements Runnable { + + private YoYo.AnimationComposer composer = null; + private Animator animator = null; + + private long delay; + private long duration; + private boolean invisibleWhenDelaying; + private View targetView; + private Interpolator interpolator; + + private long startTime = -1; + + private AnimationProxy(View hoverView, int resId, Techniques technique, long duration, long delay, boolean invisibleWhenDelaying, Interpolator interpolator, Animator.AnimatorListener... listeners){ + if(hoverView == null) + throw new IllegalStateException("Hover view is null"); + + View child = hoverView.findViewById(resId); + + if(child == null) + throw new IllegalStateException("Can not find the child view"); + + if(duration < 0) + throw new IllegalArgumentException("Duration can not be less than 0"); + + if(delay < 0) + throw new IllegalArgumentException("Delay can not be less than 0"); + + this.composer = YoYo.with(technique).duration(duration).delay(delay).interpolate(interpolator); + + this.targetView = child; + this.interpolator = interpolator; + this.delay = delay; + this.duration = duration; + this.invisibleWhenDelaying = invisibleWhenDelaying; + + for(Animator.AnimatorListener l : listeners) + this.composer.withListener(l); + } + + public static AnimationProxy build(View hoverView, int resId, Techniques technique, long duration, long delay, boolean invisibleWhenDelaying, Interpolator interpolator, Animator.AnimatorListener... listeners){ + return new AnimationProxy(hoverView, resId, technique, duration, delay, invisibleWhenDelaying, interpolator, listeners); + } + + public static AnimationProxy build(View hoverView, int childId, Animator animator){ + return new AnimationProxy(hoverView, childId, animator); + } + + private AnimationProxy(View hoverView, int childId, Animator animator){ + if(animator == null) + throw new IllegalArgumentException("Animator can not be null"); + + if(hoverView == null) + throw new IllegalArgumentException("hoverView can not be null"); + + View child = hoverView.findViewById(childId); + if(child == null) + throw new IllegalArgumentException("Can not find child"); + + this.targetView = child; + this.duration = animator.getDuration(); + this.delay = animator.getStartDelay(); + this.interpolator = null; + this.animator = animator; + } + + public void start(){ + startTime = System.currentTimeMillis(); + targetView.post(this); + } + + public boolean isDelaying(){ + long current = System.currentTimeMillis(); + if(current - startTime <= delay) + return true; + else + return false; + } + + @Override + public void run() { + if(startTime == -1) + throw new IllegalStateException("You can not call run directly, you should call start!"); + + if(!isDelaying()){ + if(targetView.getVisibility() != View.VISIBLE) + targetView.setVisibility(View.VISIBLE); + + if(composer!=null){ + composer.delay(0); + composer.playOn(targetView); + } + if(animator != null){ + animator.setStartDelay(0); + animator.start(); + } + }else{ + if(invisibleWhenDelaying && targetView.getVisibility() != View.INVISIBLE){ + targetView.setVisibility(View.INVISIBLE); + } + targetView.post(this); + } + } + + public View getTarget(){ + return this.targetView; + } + + public void withListener(Animator.AnimatorListener l){ + if(composer != null) + composer.withListener(l); + if(animator != null) + animator.addListener(l); + } +}