Skip to content

Commit

Permalink
add hover control functions
Browse files Browse the repository at this point in the history
  • Loading branch information
daimajia committed Aug 12, 2014
1 parent af30731 commit dcb49cb
Showing 1 changed file with 88 additions and 32 deletions.
120 changes: 88 additions & 32 deletions library/src/main/java/com/daimajia/androidviewhover/BlurLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ public class BlurLayout extends RelativeLayout {

private long mBlurDuration = DURATION;

public enum HOVER_STATUS {
APPEARING, APPEARED, DISAPPEARING, DISAPPEARED
};

private HOVER_STATUS mHoverStatus = HOVER_STATUS.DISAPPEARED;

public BlurLayout(Context context) {
super(context);
}
Expand All @@ -72,7 +78,6 @@ public BlurLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}


@Override
public boolean onTouchEvent(MotionEvent event) {
return gestureDetector.onTouchEvent(event);
Expand All @@ -89,41 +94,85 @@ public boolean onDown(MotionEvent e) {

@Override
public boolean onSingleTapUp(MotionEvent e) {
if(mHoverView != null){
if(hover())
return true;
else
return super.onSingleTapConfirmed(e);
}
};

public void showHover(){
hover();
}

if(!mPlayingAnimators.isEmpty()) return true;
/**
* Let hover show.
* @return
*/
private boolean hover(){
if(mHoverView == null) return false;

removeView(mBlurImage);
if(enableBlurBackground)
addBlurImage();
if(getHoverStatus() != HOVER_STATUS.DISAPPEARED || !mPlayingAnimators.isEmpty()) return true;

if(mHoverView.getParent() != null){
((ViewGroup)(mHoverView.getParent())).removeView(mHoverView);
}
removeView(mBlurImage);
if(enableBlurBackground)
addBlurImage();

if(mHoverView.getParent() != null){
((ViewGroup)(mHoverView.getParent())).removeView(mHoverView);
}

addView(mHoverView, getFullParentSizeLayoutParams());
addView(mHoverView, getFullParentSizeLayoutParams());

mHoverView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mHoverView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {

startBlurImageAppearAnimator();
startBlurImageAppearAnimator();

startHoverAppearAnimator();
startHoverAppearAnimator();

startChildrenAppearAnimations();
startChildrenAppearAnimations();

if(Build.VERSION.SDK_INT >= 16)
mHoverView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
else
mHoverView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
return true;
if(Build.VERSION.SDK_INT >= 16)
mHoverView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
else
mHoverView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
return super.onSingleTapConfirmed(e);
}
};
});
return true;

}

/**
* Let hover view dismiss.
* Notice: only when hover view status is appeared, then, this may work.
*/
public void dismissHover(){
if(getHoverStatus() != HOVER_STATUS.APPEARED || !mPlayingAnimators.isEmpty())
return;

startBlurImageDisappearAnimator();

startHoverDisappearAnimator();

startChildrenDisappearAnimations();
}

public void toggleHover(){
if(getHoverStatus() == HOVER_STATUS.DISAPPEARED)
showHover();
else if(getHoverStatus() == HOVER_STATUS.APPEARED)
dismissHover();
}

/**
* get currently hover status.
* @return
*/
public HOVER_STATUS getHoverStatus(){
return mHoverStatus;
}

private void addBlurImage(){
Bitmap bm = Blur.apply(getContext(), Util.getViewBitmap(this));
Expand All @@ -133,11 +182,19 @@ private void addBlurImage(){
this.addView(im);
}

/**
* set background blur duration.
* @param duration
*/
public void setBlurDuration(long duration){
if(duration > 100)
mBlurDuration = duration;
}

/**
* bind a hover view with BlurLayout.
* @param hover
*/
public void setHoverView(final View hover){
mHoverView = hover;

Expand All @@ -150,13 +207,8 @@ public void setHoverView(final View hover){
@Override
public void onClick(View v) {

if(!mPlayingAnimators.isEmpty()) return;

startBlurImageDisappearAnimator();

startHoverDisappearAnimator();
dismissHover();

startChildrenDisappearAnimations();
}
});
}
Expand Down Expand Up @@ -291,6 +343,7 @@ public void removeDisappearListener(DisappearListener l){
public void onAnimationStart(Animator animation) {
mAppearingAnimators.add(animation);
if(mAppearingAnimators.size() == 1){
mHoverStatus = HOVER_STATUS.APPEARING;
for(AppearListener l : mAppearListeners){
l.onStart();
}
Expand All @@ -301,6 +354,7 @@ public void onAnimationStart(Animator animation) {
public void onAnimationEnd(Animator animation) {
mAppearingAnimators.remove(animation);
if(mAppearListeners.isEmpty()){
mHoverStatus = HOVER_STATUS.APPEARED;
for(AppearListener l : mAppearListeners){
l.onEnd();
}
Expand All @@ -324,6 +378,7 @@ public void onAnimationStart(Animator animation) {
mDisappearingAnimators.add(animation);
if(mDisappearListeners.size() == 1){
for(DisappearListener l : mDisappearListeners){
mHoverStatus = HOVER_STATUS.DISAPPEARING;
l.onStart();
}
}
Expand All @@ -333,6 +388,7 @@ public void onAnimationStart(Animator animation) {
public void onAnimationEnd(Animator animation) {
mDisappearingAnimators.remove(animation);
if(mPlayingAnimators.isEmpty()){
mHoverStatus = HOVER_STATUS.DISAPPEARED;
removeView(mBlurImage);
removeView(mHoverView);
for(DisappearListener l : mDisappearListeners){
Expand Down

0 comments on commit dcb49cb

Please sign in to comment.