示例#1
0
        /**
         * Constructor that is called when inflating SwipeRefreshLayout from XML.
         *
         * @param context
         * @param attrs
         */
        public SwipeRefreshLayout(Context context, IAttributeSet attrs)
            : base(context, attrs)
        {
            mRefreshListener          = new CustomRefreshListener(this);
            mAnimateToCorrectPosition = new CustommAnimateToCorrectPosition(this);
            mAnimateToStartPosition   = new CustommAnimateToStartPosition(this);

            mTouchSlop = ViewConfiguration.Get(context).ScaledTouchSlop;

            mMediumAnimationDuration = Resources.GetInteger(
                AndroidResource.Integer.ConfigMediumAnimTime);

            SetWillNotDraw(false);
            mDecelerateInterpolator = new DecelerateInterpolator(DECELERATE_INTERPOLATION_FACTOR);

            TypedArray a = context.ObtainStyledAttributes(attrs, LAYOUT_ATTRS);

            Enabled = (a.GetBoolean(0, true));
            a.Recycle();

            DisplayMetrics metrics = Resources.DisplayMetrics;

            mCircleWidth  = (int)(CIRCLE_DIAMETER * metrics.Density);
            mCircleHeight = (int)(CIRCLE_DIAMETER * metrics.Density);

            createProgressView();
            ViewCompat.SetChildrenDrawingOrderEnabled(this, true);
            // the absolute offset has to take into account that the circle starts at an offset
            mSpinnerFinalOffset = DEFAULT_CIRCLE_TARGET * metrics.Density;
            mTotalDragDistance  = mSpinnerFinalOffset;

            RequestDisallowInterceptTouchEvent(true);
        }
        private void StartScaleUpAnimation(IAnimationListener listener)
        {
            _circleView.Visibility = ViewStates.Visible;
            if (Build.VERSION.SdkInt >= BuildVersionCodes.Honeycomb)
            {
                // 11
                // Pre API 11, alpha is used in place of scale up to show the
                // progress circle appearing.
                // Don't adjust the alpha during appearance otherwise.
                _progress.SetAlpha(MaxAlpha);
            }

            _scaleAnimation = new CustomScaleAnimation(this)
            {
                Duration = _mediumAnimationDuration
            };

            if (listener != null)
            {
                _circleView.SetAnimationListener(listener);
            }

            _circleView.ClearAnimation();
            _circleView.StartAnimation(_scaleAnimation);
        }
        private void StartScaleDownReturnToStartAnimation(int from,
                                                          IAnimationListener listener)
        {
            From = from;
            if (IsAlphaUsedForScale())
            {
                _startingScale = _progress.Alpha;
            }
            else
            {
                _startingScale = ViewCompat.GetScaleX(_circleView);
            }

            _scaleDownToStartAnimation = new CustommScaleDownToStartAnimation(this)
            {
                Duration = ScaleDownDuration
            };

            if (listener != null)
            {
                _circleView.SetAnimationListener(listener);
            }

            _circleView.ClearAnimation();
            _circleView.StartAnimation(_scaleDownToStartAnimation);
        }
        /**
         * Constructor that is called when inflating SwipeRefreshLayout from XML.
         *
         * @param context
         * @param attrs
         */
        public SwipeRefreshLayout(Context context, IAttributeSet attrs = null)
            : base(context, attrs)
        {
            _refreshListener          = new CustomRefreshListener(this);
            _animateToCorrectPosition = new CustommAnimateToCorrectPosition(this);
            _animateToStartPosition   = new CustommAnimateToStartPosition(this);

            _touchSlop = ViewConfiguration.Get(context).ScaledTouchSlop;

            _mediumAnimationDuration = Resources.GetInteger(
                AndroidResource.Integer.ConfigMediumAnimTime);

            SetWillNotDraw(false);
            _decelerateInterpolator = new DecelerateInterpolator(DecelerateInterpolationFactor);

            var a = context.ObtainStyledAttributes(attrs, LayoutAttrs);

            Enabled = (a.GetBoolean(0, true));
            a.Recycle();

            var metrics = Resources.DisplayMetrics;

            _circleWidth  = (int)(CircleDiameter * metrics.Density);
            _circleHeight = (int)(CircleDiameter * metrics.Density);

            CreateProgressView();
#pragma warning disable 618
            ViewCompat.SetChildrenDrawingOrderEnabled(this, true);
#pragma warning restore 618
            // the absolute offset has to take into account that the circle starts at an offset
            _spinnerFinalOffset = DefaultCircleTarget * metrics.Density;
            _totalDragDistance  = _spinnerFinalOffset;

            RequestDisallowInterceptTouchEvent(true);
        }
示例#5
0
 private void startScaleDownAnimation(IAnimationListener listener)
 {
     mScaleDownAnimation          = new CustommScaleDownAnimation(this);
     mScaleDownAnimation.Duration = (SCALE_DOWN_DURATION);
     mCircleView.setAnimationListener(listener);
     mCircleView.ClearAnimation();
     mCircleView.StartAnimation(mScaleDownAnimation);
 }
        private void StartScaleDownAnimation(IAnimationListener listener)
        {
            _scaleDownAnimation = new CustommScaleDownAnimation(this)
            {
                Duration = ScaleDownDuration
            };

            _circleView.SetAnimationListener(listener);
            _circleView.ClearAnimation();
            _circleView.StartAnimation(_scaleDownAnimation);
        }
示例#7
0
        /// <summary>
        /// AnimationClipを単発再生
        /// </summary>
        public static void PlayOnce(MonoBehaviour obj, AnimationClip clip, IAnimationListener listener = null)
        {
            if (clip == null || obj.gameObject.TryGetComponent <Animation>(out _))
            {
                return;
            }
            var animation = obj.gameObject.AddComponent <Animation>();

            animation.AddClip(clip.EnableLegacy().WrapModeClampForever(), ClipName);
            animation.Play(ClipName);
            obj.StartCoroutine(AsyncPlay(animation, listener));
        }
示例#8
0
 private void animateOffsetToCorrectPosition(int from, IAnimationListener listener)
 {
     mFrom = from;
     mAnimateToCorrectPosition.Reset();
     mAnimateToCorrectPosition.Duration     = (ANIMATE_TO_TRIGGER_DURATION);
     mAnimateToCorrectPosition.Interpolator = (mDecelerateInterpolator);
     if (listener != null)
     {
         mCircleView.setAnimationListener(listener);
     }
     mCircleView.ClearAnimation();
     mCircleView.StartAnimation(mAnimateToCorrectPosition);
 }
        private void AnimateOffsetToCorrectPosition(int from, IAnimationListener listener)
        {
            From = from;
            _animateToCorrectPosition.Reset();
            _animateToCorrectPosition.Duration     = (AnimateToTriggerDuration);
            _animateToCorrectPosition.Interpolator = (_decelerateInterpolator);
            if (listener != null)
            {
                _circleView.SetAnimationListener(listener);
            }

            _circleView.ClearAnimation();
            _circleView.StartAnimation(_animateToCorrectPosition);
        }
示例#10
0
        private static IEnumerator AsyncPlay(Animation animation, IAnimationListener listener)
        {
            while (animation["clip"].length > 0)
            {
                if (animation["clip"].length < animation[ClipName].time)
                {
                    break;
                }
                yield return(null);
            }

            listener?.OnAnimationFinished(animation[ClipName].clip);
            animation.gameObject.RemoveComponent <Animation>();
        }
示例#11
0
 private void startScaleUpAnimation(IAnimationListener listener)
 {
     mCircleView.Visibility = ViewStates.Visible;
     if (Build.VERSION.SdkInt >= BuildVersionCodes.Honeycomb)
     { // 11
       // Pre API 11, alpha is used in place of scale up to show the
       // progress circle appearing.
       // Don't adjust the alpha during appearance otherwise.
         mProgress.SetAlpha(MAX_ALPHA);
     }
     mScaleAnimation          = new CustomScaleAnimation(this);
     mScaleAnimation.Duration = (mMediumAnimationDuration);
     if (listener != null)
     {
         mCircleView.setAnimationListener(listener);
     }
     mCircleView.ClearAnimation();
     mCircleView.StartAnimation(mScaleAnimation);
 }
示例#12
0
 private void startScaleDownReturnToStartAnimation(int from,
                                                   IAnimationListener listener)
 {
     mFrom = from;
     if (isAlphaUsedForScale())
     {
         mStartingScale = mProgress.Alpha;
     }
     else
     {
         mStartingScale = ViewCompat.GetScaleX(mCircleView);
     }
     mScaleDownToStartAnimation          = new CustommScaleDownToStartAnimation(this);
     mScaleDownToStartAnimation.Duration = (SCALE_DOWN_DURATION);
     if (listener != null)
     {
         mCircleView.setAnimationListener(listener);
     }
     mCircleView.ClearAnimation();
     mCircleView.StartAnimation(mScaleDownToStartAnimation);
 }
示例#13
0
 private void animateOffsetToStartPosition(int from, IAnimationListener listener)
 {
     if (mScale)
     {
         // Scale the item back down
         startScaleDownReturnToStartAnimation(from, listener);
     }
     else
     {
         mFrom = from;
         mAnimateToStartPosition.Reset();
         mAnimateToStartPosition.Duration     = (ANIMATE_TO_START_DURATION);
         mAnimateToStartPosition.Interpolator = (mDecelerateInterpolator);
         if (listener != null)
         {
             mCircleView.setAnimationListener(listener);
         }
         mCircleView.ClearAnimation();
         mCircleView.StartAnimation(mAnimateToStartPosition);
     }
 }
示例#14
0
        private void AnimateOffsetToStartPosition(int from, IAnimationListener listener)
        {
            if (_scale)
            {
                // Scale the item back down
                StartScaleDownReturnToStartAnimation(from, listener);
            }
            else
            {
                From = from;
                _animateToStartPosition.Reset();
                _animateToStartPosition.Duration     = (AnimateToStartDuration);
                _animateToStartPosition.Interpolator = (_decelerateInterpolator);
                if (listener != null)
                {
                    _circleView.SetAnimationListener(listener);
                }

                _circleView.ClearAnimation();
                _circleView.StartAnimation(_animateToStartPosition);
            }
        }
示例#15
0
 //设置监听者
 public void SetAnimationListener(IAnimationListener listener)
 {
     mListener = listener;
 }
示例#16
0
        public override bool OnTouchEvent(MotionEvent ev)
        {
            var action = ev.Action;

            if (_returningToStart && action == MotionEventActions.Down)
            {
                _returningToStart = false;
            }

            if (!Enabled || _returningToStart || CanChildScrollUp())
            {
                // Fail fast if we're not in a state where a swipe is possible
                return(false);
            }

            switch (action)
            {
            case MotionEventActions.Down:
                _activePointerId = ev.GetPointerId(0);
                _isBeingDragged  = false;
                break;

            case MotionEventActions.Move:
                var pointerIndex = ev.FindPointerIndex(_activePointerId);
                if (pointerIndex < 0)
                {
                    Log.Error(LogTag, "Got ACTION_MOVE event but have an invalid active pointer id.");
                    return(false);
                }

                var y             = ev.GetY(pointerIndex);
                var overscrollTop = (y - _initialMotionY) * DragRate;
                if (_isBeingDragged)
                {
                    _progress.ShowArrow(true);
                    var originalDragPercent = overscrollTop / _totalDragDistance;
                    if (originalDragPercent < 0)
                    {
                        return(false);
                    }

                    var dragPercent     = System.Math.Min(1f, System.Math.Abs(originalDragPercent));
                    var adjustedPercent = (float)System.Math.Max(dragPercent - .4, 0) * 5 / 3;
                    var extraOS         = System.Math.Abs(overscrollTop) - _totalDragDistance;
                    var slingshotDist   = _usingCustomStart
                            ? _spinnerFinalOffset
                                          - OriginalOffsetTop
                            : _spinnerFinalOffset;
                    var tensionSlingshotPercent = System.Math.Max(0,
                                                                  System.Math.Min(extraOS, slingshotDist * 2) / slingshotDist);
                    var tensionPercent = (float)((tensionSlingshotPercent / 4) - System.Math.Pow(
                                                     (tensionSlingshotPercent / 4), 2)) * 2f;
                    var extraMove = (slingshotDist) * tensionPercent * 2;

                    var targetY = OriginalOffsetTop
                                  + (int)((slingshotDist * dragPercent) + extraMove);
                    // where 1.0f is a full circle
                    if (_circleView.Visibility != ViewStates.Visible)
                    {
                        _circleView.Visibility = ViewStates.Visible;
                    }

                    if (!_scale)
                    {
                        ViewCompat.SetScaleX(_circleView, 1f);
                        ViewCompat.SetScaleY(_circleView, 1f);
                    }

                    if (overscrollTop < _totalDragDistance)
                    {
                        if (_scale)
                        {
                            SetAnimationProgress(overscrollTop / _totalDragDistance);
                        }

                        if (_progress.Alpha > StartingProgressAlpha &&
                            !IsAnimationRunning(_alphaStartAnimation))
                        {
                            // Animate the alpha
                            StartProgressAlphaStartAnimation();
                        }

                        var strokeStart = adjustedPercent * .8f;
                        _progress.SetStartEndTrim(0f, System.Math.Min(MaxProgressAngle, strokeStart));
                        _progress.SetArrowScale(System.Math.Min(1f, adjustedPercent));
                    }
                    else
                    {
                        if (_progress.Alpha < MaxAlpha &&
                            !IsAnimationRunning(_alphaMaxAnimation))
                        {
                            // Animate the alpha
                            StartProgressAlphaMaxAnimation();
                        }
                    }

                    var rotation = (-0.25f + .4f * adjustedPercent + tensionPercent * 2) * .5f;
                    _progress.SetProgressRotation(rotation);
                    SetTargetOffsetTopAndBottom(targetY - _currentTargetOffsetTop,
                                                true /* requires update */);
                }

                break;

            //case MotionEventActions.Down:
            //    int index = ev.ActionIndex;
            //    mActivePointerId = ev.GetPointerId(index);
            //    break;


            case MotionEventActions.PointerUp:
                OnSecondaryPointerUp(ev);
                break;

            case MotionEventActions.Up:
            case MotionEventActions.Cancel:
            {
                if (_activePointerId == InvalidPointer)
                {
                    if (action == MotionEventActions.Up)
                    {
                        Log.Error(LogTag, "Got ACTION_UP event but don't have an active pointer id.");
                    }

                    return(false);
                }

                var pointerIndex0  = ev.FindPointerIndex(_activePointerId);
                var y0             = ev.GetY(pointerIndex0);
                var overscrollTop0 = (y0 - _initialMotionY) * DragRate;
                _isBeingDragged = false;
                if (overscrollTop0 > _totalDragDistance)
                {
                    SetRefreshing(true, true /* notify */);
                }
                else
                {
                    // cancel refresh
                    _refreshing = false;
                    _progress.SetStartEndTrim(0f, 0f);
                    IAnimationListener listener = null;
                    if (!_scale)
                    {
                        listener = new CustomCancelListener(this);
                    }

                    AnimateOffsetToStartPosition(_currentTargetOffsetTop, listener);
                    _progress.ShowArrow(false);
                }

                _activePointerId = InvalidPointer;
                return(false);
            }
            }

            return(true);
        }
示例#17
0
        public override bool OnTouchEvent(MotionEvent ev)
        {
            MotionEventActions action = ev.Action;

            if (mReturningToStart && action == MotionEventActions.Down)
            {
                mReturningToStart = false;
            }

            if (!Enabled || mReturningToStart || canChildScrollUp())
            {
                // Fail fast if we're not in a state where a swipe is possible
                return(false);
            }

            switch (action)
            {
            case MotionEventActions.Down:
                mActivePointerId = ev.GetPointerId(0);
                mIsBeingDragged  = false;
                break;

            case MotionEventActions.Move:
                int pointerIndex = ev.FindPointerIndex(mActivePointerId);
                if (pointerIndex < 0)
                {
                    Log.Error(LOG_TAG, "Got ACTION_MOVE event but have an invalid active pointer id.");
                    return(false);
                }

                float y             = ev.GetY(pointerIndex);
                float overscrollTop = (y - mInitialMotionY) * DRAG_RATE;
                if (mIsBeingDragged)
                {
                    mProgress.showArrow(true);
                    float originalDragPercent = overscrollTop / mTotalDragDistance;
                    if (originalDragPercent < 0)
                    {
                        return(false);
                    }
                    float dragPercent     = System.Math.Min(1f, System.Math.Abs(originalDragPercent));
                    float adjustedPercent = (float)System.Math.Max(dragPercent - .4, 0) * 5 / 3;
                    float extraOS         = System.Math.Abs(overscrollTop) - mTotalDragDistance;
                    float slingshotDist   = mUsingCustomStart ? mSpinnerFinalOffset
                                            - mOriginalOffsetTop : mSpinnerFinalOffset;
                    float tensionSlingshotPercent = System.Math.Max(0,
                                                                    System.Math.Min(extraOS, slingshotDist * 2) / slingshotDist);
                    float tensionPercent = (float)((tensionSlingshotPercent / 4) - System.Math.Pow(
                                                       (tensionSlingshotPercent / 4), 2)) * 2f;
                    float extraMove = (slingshotDist) * tensionPercent * 2;

                    int targetY = mOriginalOffsetTop
                                  + (int)((slingshotDist * dragPercent) + extraMove);
                    // where 1.0f is a full circle
                    if (mCircleView.Visibility != ViewStates.Visible)
                    {
                        mCircleView.Visibility = ViewStates.Visible;
                    }
                    if (!mScale)
                    {
                        ViewCompat.SetScaleX(mCircleView, 1f);
                        ViewCompat.SetScaleY(mCircleView, 1f);
                    }
                    if (overscrollTop < mTotalDragDistance)
                    {
                        if (mScale)
                        {
                            setAnimationProgress(overscrollTop / mTotalDragDistance);
                        }
                        if (mProgress.Alpha > STARTING_PROGRESS_ALPHA &&
                            !isAnimationRunning(mAlphaStartAnimation))
                        {
                            // Animate the alpha
                            startProgressAlphaStartAnimation();
                        }
                        float strokeStart = adjustedPercent * .8f;
                        mProgress.setStartEndTrim(0f, System.Math.Min(MAX_PROGRESS_ANGLE, strokeStart));
                        mProgress.setArrowScale(System.Math.Min(1f, adjustedPercent));
                    }
                    else
                    {
                        if (mProgress.Alpha < MAX_ALPHA &&
                            !isAnimationRunning(mAlphaMaxAnimation))
                        {
                            // Animate the alpha
                            startProgressAlphaMaxAnimation();
                        }
                    }
                    float rotation = (-0.25f + .4f * adjustedPercent + tensionPercent * 2) * .5f;
                    mProgress.setProgressRotation(rotation);
                    setTargetOffsetTopAndBottom(targetY - mCurrentTargetOffsetTop,
                                                true /* requires update */);
                }
                break;

            //case MotionEventActions.Down:
            //    int index = ev.ActionIndex;
            //    mActivePointerId = ev.GetPointerId(index);
            //    break;


            case MotionEventActions.PointerUp:
                onSecondaryPointerUp(ev);
                break;

            case MotionEventActions.Up:
            case MotionEventActions.Cancel:
            {
                if (mActivePointerId == INVALID_POINTER)
                {
                    if (action == MotionEventActions.Up)
                    {
                        Log.Error(LOG_TAG, "Got ACTION_UP event but don't have an active pointer id.");
                    }
                    return(false);
                }
                int   pointerIndex0  = ev.FindPointerIndex(mActivePointerId);
                float y0             = ev.GetY(pointerIndex0);
                float overscrollTop0 = (y0 - mInitialMotionY) * DRAG_RATE;
                mIsBeingDragged = false;
                if (overscrollTop0 > mTotalDragDistance)
                {
                    setRefreshing(true, true /* notify */);
                }
                else
                {
                    // cancel refresh
                    mRefreshing = false;
                    mProgress.setStartEndTrim(0f, 0f);
                    IAnimationListener listener = null;
                    if (!mScale)
                    {
                        listener = new CustomCancelListener(this);
                    }
                    animateOffsetToStartPosition(mCurrentTargetOffsetTop, listener);
                    mProgress.showArrow(false);
                }
                mActivePointerId = INVALID_POINTER;
                return(false);
            }
            }
            return(true);
        }
示例#18
0
 public void SetAnimationListener(IAnimationListener animationListener)
 {
     this.animationListener = animationListener;
 }