Search in sources :

Example 1 with Animator

use of com.marshalchen.common.uimodule.nineoldandroids.animation.Animator in project UltimateAndroid by cymcsg.

the class SwipeDismissListViewTouchListener method onTouch.

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    if (mViewWidth < 2) {
        mViewWidth = mListView.getWidth();
    }
    switch(MotionEventCompat.getActionMasked(motionEvent)) {
        case MotionEvent.ACTION_DOWN:
            {
                if (mPaused) {
                    return false;
                }
                // TODO: ensure this is a finger, and set a flag
                // Find the child view that was touched (perform a hit test)
                Rect rect = new Rect();
                int childCount = mListView.getChildCount();
                int[] listViewCoords = new int[2];
                mListView.getLocationOnScreen(listViewCoords);
                int x = (int) motionEvent.getRawX() - listViewCoords[0];
                int y = (int) motionEvent.getRawY() - listViewCoords[1];
                View child;
                for (int i = 0; i < childCount; i++) {
                    child = mListView.getChildAt(i);
                    child.getHitRect(rect);
                    if (rect.contains(x, y)) {
                        mDownView = child;
                        mDownViewProxy = AnimatorProxy.wrap(child);
                        break;
                    }
                }
                if (mDownView != null) {
                    mDownX = motionEvent.getRawX();
                    mDownY = motionEvent.getRawY();
                    mDownPosition = mListView.getPositionForView(mDownView);
                    if (mCallbacks.canDismiss(mDownPosition)) {
                        mVelocityTracker = VelocityTracker.obtain();
                        mVelocityTracker.addMovement(motionEvent);
                    } else {
                        mDownView = null;
                        mDownViewProxy = null;
                    }
                }
                return false;
            }
        case MotionEvent.ACTION_CANCEL:
            {
                if (mVelocityTracker == null) {
                    break;
                }
                if (mDownView != null && mSwiping) {
                    // cancel
                    animate(mDownView).translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null);
                }
                mVelocityTracker.recycle();
                mVelocityTracker = null;
                mDownX = 0;
                mDownY = 0;
                mDownView = null;
                mDownViewProxy = null;
                mDownPosition = ListView.INVALID_POSITION;
                mSwiping = false;
                break;
            }
        case MotionEvent.ACTION_UP:
            {
                if (mVelocityTracker == null) {
                    break;
                }
                float deltaX = motionEvent.getRawX() - mDownX;
                mVelocityTracker.addMovement(motionEvent);
                mVelocityTracker.computeCurrentVelocity(1000);
                float velocityX = mVelocityTracker.getXVelocity();
                float absVelocityX = Math.abs(velocityX);
                float absVelocityY = Math.abs(mVelocityTracker.getYVelocity());
                boolean dismiss = false;
                boolean dismissRight = false;
                if (Math.abs(deltaX) > mViewWidth / 2 && mSwiping) {
                    dismiss = true;
                    dismissRight = deltaX > 0;
                } else if (mMinFlingVelocity <= absVelocityX && absVelocityX <= mMaxFlingVelocity && absVelocityY < absVelocityX && mSwiping) {
                    // dismiss only if flinging in the same direction as dragging
                    dismiss = (velocityX < 0) == (deltaX < 0);
                    dismissRight = mVelocityTracker.getXVelocity() > 0;
                }
                if (dismiss && mDownPosition != ListView.INVALID_POSITION) {
                    // dismiss
                    // mDownView gets null'd before animation ends
                    final View downView = mDownView;
                    final int downPosition = mDownPosition;
                    ++mDismissAnimationRefCount;
                    animate(mDownView).translationX(dismissRight ? mViewWidth : -mViewWidth).alpha(0).setDuration(mAnimationTime).setListener(new AnimatorListenerAdapter() {

                        @Override
                        public void onAnimationEnd(Animator animation) {
                            performDismiss(downView, downPosition);
                        }
                    });
                } else {
                    animate(mDownView).translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null);
                }
                mVelocityTracker.recycle();
                mVelocityTracker = null;
                mDownX = 0;
                mDownY = 0;
                mDownView = null;
                mDownViewProxy = null;
                mDownPosition = ListView.INVALID_POSITION;
                mSwiping = false;
                break;
            }
        case MotionEvent.ACTION_MOVE:
            {
                if (mVelocityTracker == null || mPaused) {
                    break;
                }
                mVelocityTracker.addMovement(motionEvent);
                float deltaX = motionEvent.getRawX() - mDownX;
                float deltaY = motionEvent.getRawY() - mDownY;
                if (Math.abs(deltaX) > mSlop && Math.abs(deltaY) < Math.abs(deltaX) / 2) {
                    mSwiping = true;
                    mSwipingSlop = (deltaX > 0 ? mSlop : -mSlop);
                    mListView.requestDisallowInterceptTouchEvent(true);
                    // Cancel ListView's touch (un-highlighting the item)
                    MotionEvent cancelEvent = MotionEvent.obtain(motionEvent);
                    cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (MotionEventCompat.getActionIndex(motionEvent) << MotionEventCompat.ACTION_POINTER_INDEX_SHIFT));
                    mListView.onTouchEvent(cancelEvent);
                    cancelEvent.recycle();
                }
                if (mSwiping) {
                    mDownViewProxy.setTranslationX(deltaX - mSwipingSlop);
                    mDownViewProxy.setAlpha(Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth)));
                    return true;
                }
                break;
            }
    }
    return false;
}
Also used : Rect(android.graphics.Rect) ValueAnimator(com.marshalchen.common.uimodule.nineoldandroids.animation.ValueAnimator) Animator(com.marshalchen.common.uimodule.nineoldandroids.animation.Animator) AnimatorListenerAdapter(com.marshalchen.common.uimodule.nineoldandroids.animation.AnimatorListenerAdapter) AbsListView(android.widget.AbsListView) ListView(android.widget.ListView)

Example 2 with Animator

use of com.marshalchen.common.uimodule.nineoldandroids.animation.Animator in project UltimateAndroid by cymcsg.

the class EnhancedListView method slideOutView.

/**
     * Slide out a view to the right or left of the list. After the animation has finished, the
     * view will be dismissed by calling {@link #performDismiss(android.view.View, android.view.View, int)}.
     *
     * @param view        The view, that should be slided out.
     * @param childView   The whole view of the list item.
     * @param position    The item position of the item.
     * @param toRightSide Whether it should slide out to the right side.
     */
private void slideOutView(final View view, final View childView, final int position, boolean toRightSide) {
    // Only start new animation, if this view isn't already animated (too fast swiping bug)
    synchronized (mAnimationLock) {
        if (mAnimatedViews.contains(view)) {
            return;
        }
        ++mDismissAnimationRefCount;
        mAnimatedViews.add(view);
    }
    ViewPropertyAnimator.animate(view).translationX(toRightSide ? mViewWidth : -mViewWidth).setDuration(mAnimationTime).setListener(new AnimatorListenerAdapter() {

        @Override
        public void onAnimationEnd(Animator animation) {
            performDismiss(view, childView, position);
        }
    });
}
Also used : ValueAnimator(com.marshalchen.common.uimodule.nineoldandroids.animation.ValueAnimator) Animator(com.marshalchen.common.uimodule.nineoldandroids.animation.Animator) ViewPropertyAnimator(com.marshalchen.common.uimodule.nineoldandroids.view.ViewPropertyAnimator) AnimatorListenerAdapter(com.marshalchen.common.uimodule.nineoldandroids.animation.AnimatorListenerAdapter)

Example 3 with Animator

use of com.marshalchen.common.uimodule.nineoldandroids.animation.Animator in project UltimateAndroid by cymcsg.

the class EnhancedListView method performDismiss.

/**
     * Animate the dismissed list item to zero-height and fire the dismiss callback when
     * all dismissed list item animations have completed.
     *
     * @param dismissView     The view that has been slided out.
     * @param listItemView    The list item view. This is the whole view of the list item, and not just
     *                        the part, that the user swiped.
     * @param dismissPosition The position of the view inside the list.
     */
private void performDismiss(final View dismissView, final View listItemView, final int dismissPosition) {
    final ViewGroup.LayoutParams lp = listItemView.getLayoutParams();
    final int originalLayoutHeight = lp.height;
    int originalHeight = listItemView.getHeight();
    ValueAnimator animator = ValueAnimator.ofInt(originalHeight, 1).setDuration(mAnimationTime);
    animator.addListener(new AnimatorListenerAdapter() {

        @Override
        public void onAnimationEnd(Animator animation) {
            // Make sure no other animation is running. Remove animation from running list, that just finished
            boolean noAnimationLeft;
            synchronized (mAnimationLock) {
                --mDismissAnimationRefCount;
                mAnimatedViews.remove(dismissView);
                noAnimationLeft = mDismissAnimationRefCount == 0;
            }
            if (noAnimationLeft) {
                for (PendingDismissData dismiss : mPendingDismisses) {
                    if (mUndoStyle == UndoStyle.SINGLE_POPUP) {
                        for (Undoable undoable : mUndoActions) {
                            undoable.discard();
                        }
                        mUndoActions.clear();
                    }
                    Undoable undoable = mDismissCallback.onDismiss(EnhancedListView.this, dismiss.position);
                    if (undoable != null) {
                        mUndoActions.add(undoable);
                    }
                    mValidDelayedMsgId++;
                }
                if (!mUndoActions.isEmpty()) {
                    changePopupText();
                    changeButtonLabel();
                    // Show undo popup
                    float yLocationOffset = getResources().getDimension(R.dimen.elv_undo_bottom_offset);
                    mUndoPopup.setWidth((int) Math.min(mScreenDensity * 400, getWidth() * 0.9f));
                    mUndoPopup.showAtLocation(EnhancedListView.this, Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, (int) yLocationOffset);
                    // Queue the dismiss only if required
                    if (!mTouchBeforeAutoHide) {
                        // Send a delayed message to hide popup
                        mHideUndoHandler.sendMessageDelayed(mHideUndoHandler.obtainMessage(mValidDelayedMsgId), mUndoHideDelay);
                    }
                }
                ViewGroup.LayoutParams lp;
                for (PendingDismissData pendingDismiss : mPendingDismisses) {
                    ViewHelper.setAlpha(pendingDismiss.view, 1f);
                    ViewHelper.setTranslationX(pendingDismiss.view, 0);
                    lp = pendingDismiss.childView.getLayoutParams();
                    lp.height = originalLayoutHeight;
                    pendingDismiss.childView.setLayoutParams(lp);
                }
                mPendingDismisses.clear();
            }
        }
    });
    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            lp.height = (Integer) valueAnimator.getAnimatedValue();
            listItemView.setLayoutParams(lp);
        }
    });
    mPendingDismisses.add(new PendingDismissData(dismissPosition, dismissView, listItemView));
    animator.start();
}
Also used : ValueAnimator(com.marshalchen.common.uimodule.nineoldandroids.animation.ValueAnimator) Animator(com.marshalchen.common.uimodule.nineoldandroids.animation.Animator) ViewPropertyAnimator(com.marshalchen.common.uimodule.nineoldandroids.view.ViewPropertyAnimator) AnimatorListenerAdapter(com.marshalchen.common.uimodule.nineoldandroids.animation.AnimatorListenerAdapter) ValueAnimator(com.marshalchen.common.uimodule.nineoldandroids.animation.ValueAnimator)

Example 4 with Animator

use of com.marshalchen.common.uimodule.nineoldandroids.animation.Animator in project UltimateAndroid by cymcsg.

the class AnimateDismissAdapter method createAnimatorForView.

private Animator createAnimatorForView(final View view) {
    final ViewGroup.LayoutParams lp = view.getLayoutParams();
    final int originalHeight = view.getHeight();
    ValueAnimator animator = ValueAnimator.ofInt(originalHeight, 0);
    animator.addListener(new AnimatorListenerAdapter() {

        @Override
        public void onAnimationEnd(final Animator animator) {
            lp.height = 0;
            view.setLayoutParams(lp);
        }
    });
    animator.addUpdateListener(new AnimatorUpdateListener() {

        @Override
        public void onAnimationUpdate(final ValueAnimator valueAnimator) {
            lp.height = (Integer) valueAnimator.getAnimatedValue();
            view.setLayoutParams(lp);
        }
    });
    return animator;
}
Also used : ValueAnimator(com.marshalchen.common.uimodule.nineoldandroids.animation.ValueAnimator) Animator(com.marshalchen.common.uimodule.nineoldandroids.animation.Animator) ViewGroup(android.view.ViewGroup) AnimatorListenerAdapter(com.marshalchen.common.uimodule.nineoldandroids.animation.AnimatorListenerAdapter) AnimatorUpdateListener(com.marshalchen.common.uimodule.nineoldandroids.animation.ValueAnimator.AnimatorUpdateListener) ValueAnimator(com.marshalchen.common.uimodule.nineoldandroids.animation.ValueAnimator)

Example 5 with Animator

use of com.marshalchen.common.uimodule.nineoldandroids.animation.Animator in project UltimateAndroid by cymcsg.

the class SwipeDismissListViewTouchListener method handleUpEvent.

private boolean handleUpEvent(final MotionEvent motionEvent) {
    if (mVelocityTracker == null) {
        return false;
    }
    float deltaX = motionEvent.getRawX() - mDownX;
    mVelocityTracker.addMovement(motionEvent);
    mVelocityTracker.computeCurrentVelocity(1000);
    float velocityX = Math.abs(mVelocityTracker.getXVelocity());
    float velocityY = Math.abs(mVelocityTracker.getYVelocity());
    boolean dismiss = false;
    boolean dismissRight = false;
    if (Math.abs(deltaX) > mViewWidth / 2) {
        dismiss = true;
        dismissRight = deltaX > 0;
    } else if (mMinFlingVelocity <= velocityX && velocityX <= mMaxFlingVelocity && velocityY < velocityX) {
        dismiss = true;
        dismissRight = mVelocityTracker.getXVelocity() > 0;
    }
    if (mSwiping) {
        if (dismiss) {
            // mDownView gets null'd before animation ends
            final PendingDismissData pendingDismissData = mCurrentDismissData;
            ++mDismissAnimationRefCount;
            animate(mCurrentDismissData.view).translationX(dismissRight ? mViewWidth : -mViewWidth).alpha(0).setDuration(mAnimationTime).setListener(new AnimatorListenerAdapter() {

                @Override
                public void onAnimationEnd(final Animator animation) {
                    onDismiss(pendingDismissData);
                }
            });
            mVirtualListCount--;
            mPendingDismisses.add(mCurrentDismissData);
        } else {
            // cancel
            animate(mCurrentDismissData.view).translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null);
        }
    }
    mVelocityTracker.recycle();
    mVelocityTracker = null;
    mDownX = 0;
    mDownY = 0;
    mCurrentDismissData = null;
    mSwiping = false;
    return false;
}
Also used : ValueAnimator(com.marshalchen.common.uimodule.nineoldandroids.animation.ValueAnimator) Animator(com.marshalchen.common.uimodule.nineoldandroids.animation.Animator) ViewPropertyAnimator(com.marshalchen.common.uimodule.nineoldandroids.view.ViewPropertyAnimator) AnimatorListenerAdapter(com.marshalchen.common.uimodule.nineoldandroids.animation.AnimatorListenerAdapter)

Aggregations

Animator (com.marshalchen.common.uimodule.nineoldandroids.animation.Animator)24 ValueAnimator (com.marshalchen.common.uimodule.nineoldandroids.animation.ValueAnimator)15 AnimatorListenerAdapter (com.marshalchen.common.uimodule.nineoldandroids.animation.AnimatorListenerAdapter)14 View (android.view.View)8 ObjectAnimator (com.marshalchen.common.uimodule.nineoldandroids.animation.ObjectAnimator)8 AnimatorSet (com.marshalchen.common.uimodule.nineoldandroids.animation.AnimatorSet)5 ListView (android.widget.ListView)4 ViewPropertyAnimator (com.marshalchen.common.uimodule.nineoldandroids.view.ViewPropertyAnimator)4 SuppressLint (android.annotation.SuppressLint)3 HashMap (java.util.HashMap)2 Rect (android.graphics.Rect)1 ViewGroup (android.view.ViewGroup)1 OnPreDrawListener (android.view.ViewTreeObserver.OnPreDrawListener)1 AccelerateDecelerateInterpolator (android.view.animation.AccelerateDecelerateInterpolator)1 AccelerateInterpolator (android.view.animation.AccelerateInterpolator)1 LinearInterpolator (android.view.animation.LinearInterpolator)1 AbsListView (android.widget.AbsListView)1 AdapterView (android.widget.AdapterView)1 Techniques (com.marshalchen.common.uimodule.androidanimations.Techniques)1 AnimatorUpdateListener (com.marshalchen.common.uimodule.nineoldandroids.animation.ValueAnimator.AnimatorUpdateListener)1