Search in sources :

Example 71 with ViewParent

use of android.view.ViewParent in project Carbon by ZieIony.

the class RangeSeekBar method onTouchEvent.

@Override
public boolean onTouchEvent(MotionEvent event) {
    float v = (value - min) / (max - min);
    float v2 = (value2 - min) / (max - min);
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        int thumbX = (int) (v * (getWidth() - getPaddingLeft() - getPaddingRight() - thumbRadius * 2) + getPaddingLeft() + thumbRadius);
        int thumbX2 = (int) (v2 * (getWidth() - getPaddingLeft() - getPaddingRight() - thumbRadius2 * 2) + getPaddingLeft() + thumbRadius2);
        if (Math.abs(event.getX() - thumbX) < Math.abs(event.getX() - thumbX2)) {
            draggedThumb = 1;
            if (radiusAnimator != null)
                radiusAnimator.end();
            radiusAnimator = ValueAnimator.ofFloat(thumbRadius, THUMB_RADIUS_DRAGGED);
            radiusAnimator.setDuration(200);
            radiusAnimator.setInterpolator(interpolator);
            radiusAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    thumbRadius = (float) animation.getAnimatedValue();
                    postInvalidate();
                }
            });
            radiusAnimator.addListener(new AnimatorListenerAdapter() {

                @Override
                public void onAnimationEnd(Animator animation) {
                    radiusAnimator = null;
                }
            });
            radiusAnimator.start();
        } else {
            draggedThumb = 2;
            if (radiusAnimator != null)
                radiusAnimator.end();
            radiusAnimator = ValueAnimator.ofFloat(thumbRadius2, THUMB_RADIUS_DRAGGED);
            radiusAnimator.setDuration(200);
            radiusAnimator.setInterpolator(interpolator);
            radiusAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    thumbRadius2 = (float) animation.getAnimatedValue();
                    postInvalidate();
                }
            });
            radiusAnimator.addListener(new AnimatorListenerAdapter() {

                @Override
                public void onAnimationEnd(Animator animation) {
                    radiusAnimator = null;
                }
            });
            radiusAnimator.start();
        }
        ViewParent parent = getParent();
        if (parent != null)
            parent.requestDisallowInterceptTouchEvent(true);
        if (showLabel)
            popup.show(this);
    } else if (event.getAction() == MotionEvent.ACTION_CANCEL || event.getAction() == MotionEvent.ACTION_UP) {
        if (draggedThumb == 1) {
            if (style == Style.Discrete) {
                float val = (float) Math.floor((value - min + step / 2) / step) * step + min;
                if (valueAnimator != null)
                    valueAnimator.cancel();
                valueAnimator = ValueAnimator.ofFloat(value, val);
                valueAnimator.setDuration(200);
                valueAnimator.setInterpolator(interpolator);
                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        value = (float) animation.getAnimatedValue();
                        int thumbX = (int) ((value - min) / (max - min) * (getWidth() - getPaddingLeft() - getPaddingRight()) + getPaddingLeft());
                        int thumbY = getHeight() / 2;
                        int radius = rippleDrawable.getRadius();
                        rippleDrawable.setBounds(thumbX - radius, thumbY - radius, thumbX + radius, thumbY + radius);
                        postInvalidate();
                    }
                });
                valueAnimator.start();
            }
            if (radiusAnimator != null)
                radiusAnimator.end();
            radiusAnimator = ValueAnimator.ofFloat(thumbRadius, THUMB_RADIUS);
            radiusAnimator.setDuration(200);
            radiusAnimator.setInterpolator(interpolator);
            radiusAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    thumbRadius = (float) animation.getAnimatedValue();
                    postInvalidate();
                }
            });
            radiusAnimator.start();
        } else {
            if (style == Style.Discrete) {
                float val2 = (float) Math.floor((value2 - min + step / 2) / step) * step + min;
                if (valueAnimator != null)
                    valueAnimator.cancel();
                valueAnimator = ValueAnimator.ofFloat(value2, val2);
                valueAnimator.setDuration(200);
                valueAnimator.setInterpolator(interpolator);
                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        value2 = (float) animation.getAnimatedValue();
                        int thumbX = (int) ((value2 - min) / (max - min) * (getWidth() - getPaddingLeft() - getPaddingRight()) + getPaddingLeft());
                        int thumbY = getHeight() / 2;
                        int radius = rippleDrawable.getRadius();
                        rippleDrawable.setBounds(thumbX - radius, thumbY - radius, thumbX + radius, thumbY + radius);
                        postInvalidate();
                    }
                });
                valueAnimator.start();
            }
            if (radiusAnimator != null)
                radiusAnimator.end();
            radiusAnimator = ValueAnimator.ofFloat(thumbRadius2, THUMB_RADIUS);
            radiusAnimator.setDuration(200);
            radiusAnimator.setInterpolator(interpolator);
            radiusAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    thumbRadius2 = (float) animation.getAnimatedValue();
                    postInvalidate();
                }
            });
            radiusAnimator.start();
        }
        //draggedThumb = -1;
        ViewParent parent = getParent();
        if (parent != null)
            parent.requestDisallowInterceptTouchEvent(false);
        if (showLabel)
            popup.dismiss();
    }
    if (draggedThumb == 1) {
        v = (event.getX() - getPaddingLeft()) / (getWidth() - getPaddingLeft() - getPaddingRight());
        v = Math.max(0, Math.min(v, 1));
    } else if (draggedThumb == 2) {
        v2 = (event.getX() - getPaddingLeft()) / (getWidth() - getPaddingLeft() - getPaddingRight());
        v2 = Math.max(0, Math.min(v2, 1));
    }
    if (v > v2) {
        draggedThumb = 3 - draggedThumb;
        float t = v;
        v = v2;
        v2 = t;
        t = thumbRadius;
        thumbRadius = thumbRadius2;
        thumbRadius2 = t;
    }
    float newValue = v * (max - min) + min;
    float newValue2 = v2 * (max - min) + min;
    int thumbX = 0;
    if (draggedThumb == 1) {
        thumbX = (int) (v * (getWidth() - getPaddingLeft() - getPaddingRight()) + getPaddingLeft());
    } else if (draggedThumb == 2) {
        thumbX = (int) (v2 * (getWidth() - getPaddingLeft() - getPaddingRight()) + getPaddingLeft());
    }
    int thumbY = getHeight() / 2;
    int radius = rippleDrawable.getRadius();
    if (showLabel && draggedThumb > 0) {
        int[] location = new int[2];
        getLocationInWindow(location);
        popup.setText(String.format(labelFormat, draggedThumb == 1 ? newValue : newValue2));
        popup.update(thumbX + location[0] - popup.getBubbleWidth() / 2, thumbY - radius + location[1] - popup.getHeight());
    }
    if (rippleDrawable != null) {
        rippleDrawable.setHotspot(event.getX(), event.getY());
        rippleDrawable.setBounds(thumbX - radius, thumbY - radius, thumbX + radius, thumbY + radius);
    }
    postInvalidate();
    if ((newValue != value || newValue2 != value2) && onValueChangedListener != null) {
        if (style == Style.Discrete) {
            int sv = stepValue(newValue);
            int sv2 = stepValue(newValue2);
            if (stepValue(value) != sv || stepValue(value2) != sv2)
                onValueChangedListener.onValueChanged(this, sv, sv2);
        } else {
            onValueChangedListener.onValueChanged(this, newValue, newValue2);
        }
    }
    value = newValue;
    value2 = newValue2;
    super.onTouchEvent(event);
    return true;
}
Also used : ValueAnimator(com.nineoldandroids.animation.ValueAnimator) Animator(com.nineoldandroids.animation.Animator) StateAnimator(carbon.animation.StateAnimator) AnimatorListenerAdapter(com.nineoldandroids.animation.AnimatorListenerAdapter) ViewParent(android.view.ViewParent) ValueAnimator(com.nineoldandroids.animation.ValueAnimator) Paint(android.graphics.Paint)

Example 72 with ViewParent

use of android.view.ViewParent in project Carbon by ZieIony.

the class RecyclerView method dispatchTouchEvent.

@Override
public boolean dispatchTouchEvent(@NonNull MotionEvent ev) {
    if (header != null && (getChildCount() == 0 || getChildAt(0).getTop() + getScrollY() > ev.getY()))
        if (header.dispatchTouchEvent(ev))
            return true;
    switch(ev.getAction()) {
        case MotionEvent.ACTION_MOVE:
            float deltaY = prevY - ev.getY();
            if (!drag && Math.abs(deltaY) > mTouchSlop) {
                final ViewParent parent = getParent();
                if (parent != null) {
                    parent.requestDisallowInterceptTouchEvent(true);
                }
                drag = true;
                if (deltaY > 0) {
                    deltaY -= mTouchSlop;
                } else {
                    deltaY += mTouchSlop;
                }
            }
            if (drag) {
                final int oldY = computeVerticalScrollOffset();
                int range = computeVerticalScrollRange() - getHeight();
                if (header != null)
                    range += header.getHeight();
                boolean canOverscroll = overscrollMode == ViewCompat.OVER_SCROLL_ALWAYS || (overscrollMode == ViewCompat.OVER_SCROLL_IF_CONTENT_SCROLLS && range > 0);
                if (canOverscroll) {
                    float pulledToY = oldY + deltaY;
                    if (pulledToY < 0) {
                        topGlow.onPull(deltaY / getHeight(), ev.getX() / getWidth());
                        if (!bottomGlow.isFinished())
                            bottomGlow.onRelease();
                    } else if (pulledToY > range) {
                        bottomGlow.onPull(deltaY / getHeight(), 1.f - ev.getX() / getWidth());
                        if (!topGlow.isFinished())
                            topGlow.onRelease();
                    }
                    if (topGlow != null && (!topGlow.isFinished() || !bottomGlow.isFinished()))
                        postInvalidate();
                }
            }
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            if (drag) {
                drag = false;
                if (topGlow != null) {
                    topGlow.onRelease();
                    bottomGlow.onRelease();
                }
            }
            break;
    }
    prevY = ev.getY();
    return super.dispatchTouchEvent(ev);
}
Also used : ViewParent(android.view.ViewParent) Paint(android.graphics.Paint)

Example 73 with ViewParent

use of android.view.ViewParent in project Carbon by ZieIony.

the class ViewPager method dispatchTouchEvent.

@Override
public boolean dispatchTouchEvent(@NonNull MotionEvent ev) {
    switch(ev.getAction()) {
        case MotionEvent.ACTION_MOVE:
            float deltaX = prevX - ev.getX();
            if (!drag && Math.abs(deltaX) > mTouchSlop) {
                final ViewParent parent = getParent();
                if (parent != null) {
                    parent.requestDisallowInterceptTouchEvent(true);
                }
                drag = true;
                if (deltaX > 0) {
                    deltaX -= mTouchSlop;
                } else {
                    deltaX += mTouchSlop;
                }
            }
            if (drag) {
                final int oldX = getScrollX();
                final int range = getScrollRange();
                boolean canOverscroll = overscrollMode == OVER_SCROLL_ALWAYS || (overscrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && range > 0);
                if (canOverscroll) {
                    float pulledToX = oldX + deltaX;
                    if (pulledToX < 0) {
                        leftGlow.onPull(deltaX / getWidth(), 1.f - ev.getY() / getHeight());
                        if (!rightGlow.isFinished())
                            rightGlow.onRelease();
                    } else if (pulledToX > range) {
                        rightGlow.onPull(deltaX / getWidth(), ev.getY() / getHeight());
                        if (!leftGlow.isFinished())
                            leftGlow.onRelease();
                    }
                    if (leftGlow != null && (!leftGlow.isFinished() || !rightGlow.isFinished()))
                        postInvalidate();
                }
            }
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            if (drag) {
                drag = false;
                if (leftGlow != null) {
                    leftGlow.onRelease();
                    rightGlow.onRelease();
                }
            }
            break;
    }
    prevX = ev.getX();
    return super.dispatchTouchEvent(ev);
}
Also used : ViewParent(android.view.ViewParent)

Example 74 with ViewParent

use of android.view.ViewParent in project Carbon by ZieIony.

the class FlagsScrollView method onTouchEvent.

@Override
public boolean onTouchEvent(MotionEvent ev) {
    initVelocityTrackerIfNotExists();
    mVelocityTracker.addMovement(ev);
    final int action = ev.getAction();
    switch(action & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            {
                if (getChildCount() == 0) {
                    return false;
                }
                if ((mIsBeingDragged = !mScroller.isFinished())) {
                    final ViewParent parent = getParent();
                    if (parent != null) {
                        parent.requestDisallowInterceptTouchEvent(true);
                    }
                }
                /*
                 * If being flinged and user touches, stop the fling. isFinished
                 * will be false if being flinged.
                 */
                if (!mScroller.isFinished()) {
                    mScroller.abortAnimation();
                }
                // Remember where the motion event started
                mLastMotionY = (int) ev.getY();
                mActivePointerId = ev.getPointerId(0);
                break;
            }
        case MotionEvent.ACTION_MOVE:
            final int activePointerIndex = ev.findPointerIndex(mActivePointerId);
            if (activePointerIndex == -1) {
                Log.e(TAG, "Invalid pointerId=" + mActivePointerId + " in onTouchEvent");
                break;
            }
            final int y = (int) ev.getY(activePointerIndex);
            int deltaY = mLastMotionY - y;
            if (!mIsBeingDragged && Math.abs(deltaY) > mTouchSlop) {
                final ViewParent parent = getParent();
                if (parent != null) {
                    parent.requestDisallowInterceptTouchEvent(true);
                }
                mIsBeingDragged = true;
                if (deltaY > 0) {
                    deltaY -= mTouchSlop;
                } else {
                    deltaY += mTouchSlop;
                }
            }
            if (mIsBeingDragged) {
                // Scroll to follow the motion event
                mLastMotionY = y;
                final int oldX = getScrollX();
                final int oldY = getScrollY();
                final int range = getScrollRange();
                final int overscrollMode = getOverScrollMode();
                final boolean canOverscroll = overscrollMode == OVER_SCROLL_ALWAYS || (overscrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && range > 0);
                // calls onScrollChanged if applicable.
                if (overScrollBy(0, deltaY, 0, getScrollY(), 0, range, 0, mOverscrollDistance, true)) {
                    // Break our velocity if we hit a scroll barrier.
                    mVelocityTracker.clear();
                }
                if (canOverscroll) {
                    final int pulledToY = oldY + deltaY;
                    if (pulledToY < 0) {
                        mEdgeGlowTop.onPull((float) deltaY / getHeight());
                        if (!mEdgeGlowBottom.isFinished()) {
                            mEdgeGlowBottom.onRelease();
                        }
                    } else if (pulledToY > range) {
                        mEdgeGlowBottom.onPull((float) deltaY / getHeight());
                        if (!mEdgeGlowTop.isFinished()) {
                            mEdgeGlowTop.onRelease();
                        }
                    }
                    if (mEdgeGlowTop != null && (!mEdgeGlowTop.isFinished() || !mEdgeGlowBottom.isFinished())) {
                        ViewCompat.postInvalidateOnAnimation(this);
                    }
                }
            }
            break;
        case MotionEvent.ACTION_UP:
            if (mIsBeingDragged) {
                final VelocityTracker velocityTracker = mVelocityTracker;
                velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
                int initialVelocity = (int) velocityTracker.getYVelocity(mActivePointerId);
                if (getChildCount() > 0) {
                    if ((Math.abs(initialVelocity) > mMinimumVelocity)) {
                        fling(-initialVelocity);
                    } else {
                        if (mScroller.springBack(getScrollX(), getScrollY(), 0, 0, 0, getScrollRange())) {
                            ViewCompat.postInvalidateOnAnimation(this);
                        }
                    }
                }
                mActivePointerId = INVALID_POINTER;
                endDrag();
            }
            break;
        case MotionEvent.ACTION_CANCEL:
            if (mIsBeingDragged && getChildCount() > 0) {
                if (mScroller.springBack(getScrollX(), getScrollY(), 0, 0, 0, getScrollRange())) {
                    ViewCompat.postInvalidateOnAnimation(this);
                }
                mActivePointerId = INVALID_POINTER;
                endDrag();
            }
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
            {
                final int index = ev.getActionIndex();
                mLastMotionY = (int) ev.getY(index);
                mActivePointerId = ev.getPointerId(index);
                break;
            }
        case MotionEvent.ACTION_POINTER_UP:
            onSecondaryPointerUp(ev);
            mLastMotionY = (int) ev.getY(ev.findPointerIndex(mActivePointerId));
            break;
    }
    return true;
}
Also used : VelocityTracker(android.view.VelocityTracker) ViewParent(android.view.ViewParent)

Example 75 with ViewParent

use of android.view.ViewParent in project actor-platform by actorapp.

the class VerticalViewPager method onTouchEvent.

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (mFakeDragging) {
        // (It is likely that the user is multi-touching the screen.)
        return true;
    }
    if (ev.getAction() == MotionEvent.ACTION_DOWN && ev.getEdgeFlags() != 0) {
        // descendants.
        return false;
    }
    if (mAdapter == null || mAdapter.getCount() == 0) {
        // Nothing to present or scroll; nothing to touch.
        return false;
    }
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(ev);
    final int action = ev.getAction();
    boolean needsInvalidate = false;
    switch(action & MotionEventCompat.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            {
                mScroller.abortAnimation();
                mPopulatePending = false;
                populate();
                // Remember where the motion event started
                mLastMotionX = mInitialMotionX = ev.getX();
                mLastMotionY = mInitialMotionY = ev.getY();
                mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
                break;
            }
        case MotionEvent.ACTION_MOVE:
            if (!mIsBeingDragged) {
                final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
                final float y = MotionEventCompat.getY(ev, pointerIndex);
                final float yDiff = Math.abs(y - mLastMotionY);
                final float x = MotionEventCompat.getX(ev, pointerIndex);
                final float xDiff = Math.abs(x - mLastMotionX);
                if (DEBUG)
                    Log.v(TAG, "Moved x to " + x + "," + y + " diff=" + xDiff + "," + yDiff);
                if (yDiff > mTouchSlop && yDiff > xDiff) {
                    if (DEBUG)
                        Log.v(TAG, "Starting drag!");
                    mIsBeingDragged = true;
                    requestParentDisallowInterceptTouchEvent(true);
                    mLastMotionY = y - mInitialMotionY > 0 ? mInitialMotionY + mTouchSlop : mInitialMotionY - mTouchSlop;
                    mLastMotionX = x;
                    setScrollState(SCROLL_STATE_DRAGGING);
                    setScrollingCacheEnabled(true);
                    // Disallow Parent Intercept, just in case
                    ViewParent parent = getParent();
                    if (parent != null) {
                        parent.requestDisallowInterceptTouchEvent(true);
                    }
                }
            }
            // Not else! Note that mIsBeingDragged can be set above.
            if (mIsBeingDragged) {
                // Scroll to follow the motion event
                final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
                final float y = MotionEventCompat.getY(ev, activePointerIndex);
                needsInvalidate |= performDrag(y);
            }
            break;
        case MotionEvent.ACTION_UP:
            if (mIsBeingDragged) {
                final VelocityTracker velocityTracker = mVelocityTracker;
                velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
                int initialVelocity = (int) VelocityTrackerCompat.getYVelocity(velocityTracker, mActivePointerId);
                mPopulatePending = true;
                final int height = getClientHeight();
                final int scrollY = getScrollY();
                final ItemInfo ii = infoForCurrentScrollPosition();
                final int currentPage = ii.position;
                final float pageOffset = (((float) scrollY / height) - ii.offset) / ii.heightFactor;
                final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
                final float y = MotionEventCompat.getY(ev, activePointerIndex);
                final int totalDelta = (int) (y - mInitialMotionY);
                int nextPage = determineTargetPage(currentPage, pageOffset, initialVelocity, totalDelta);
                setCurrentItemInternal(nextPage, true, true, initialVelocity);
                mActivePointerId = INVALID_POINTER;
                endDrag();
                needsInvalidate = mTopEdge.onRelease() | mBottomEdge.onRelease();
            }
            break;
        case MotionEvent.ACTION_CANCEL:
            if (mIsBeingDragged) {
                scrollToItem(mCurItem, true, 0, false);
                mActivePointerId = INVALID_POINTER;
                endDrag();
                needsInvalidate = mTopEdge.onRelease() | mBottomEdge.onRelease();
            }
            break;
        case MotionEventCompat.ACTION_POINTER_DOWN:
            {
                final int index = MotionEventCompat.getActionIndex(ev);
                final float y = MotionEventCompat.getY(ev, index);
                mLastMotionY = y;
                mActivePointerId = MotionEventCompat.getPointerId(ev, index);
                break;
            }
        case MotionEventCompat.ACTION_POINTER_UP:
            onSecondaryPointerUp(ev);
            mLastMotionY = MotionEventCompat.getY(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
            break;
    }
    if (needsInvalidate) {
        ViewCompat.postInvalidateOnAnimation(this);
    }
    return true;
}
Also used : VelocityTracker(android.view.VelocityTracker) ViewParent(android.view.ViewParent)

Aggregations

ViewParent (android.view.ViewParent)409 ViewGroup (android.view.ViewGroup)185 View (android.view.View)156 ImageView (android.widget.ImageView)36 Rect (android.graphics.Rect)32 VelocityTracker (android.view.VelocityTracker)32 TextView (android.widget.TextView)24 FrameLayout (android.widget.FrameLayout)17 SuppressLint (android.annotation.SuppressLint)16 Paint (android.graphics.Paint)15 AdapterView (android.widget.AdapterView)14 AccessibilityEvent (android.view.accessibility.AccessibilityEvent)13 ViewTreeObserver (android.view.ViewTreeObserver)12 LayoutParams (android.view.WindowManager.LayoutParams)11 Description (org.hamcrest.Description)11 ExpandableNotificationRow (com.android.systemui.statusbar.ExpandableNotificationRow)10 SignalClusterView (com.android.systemui.statusbar.SignalClusterView)10 TypeSafeMatcher (org.hamcrest.TypeSafeMatcher)10 MotionEvent (android.view.MotionEvent)9 EditText (android.widget.EditText)9