Search in sources :

Example 61 with MotionEvent

use of android.view.MotionEvent in project cardslib by gabrielemariotti.

the class SwipeDismissListItemViewTouchListener method onTouch.

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    if (mViewWidth < 2) {
        mViewWidth = mListView.getWidth();
    }
    switch(motionEvent.getActionMasked()) {
        case MotionEvent.ACTION_DOWN:
            {
                if (mPaused) {
                    return false;
                }
                if (mSwiping) {
                    return true;
                }
                // Find the child view that was touched (perform a hit test)
                Rect rect = new Rect();
                int childCount = mListView.getChildCount();
                int headerCount = 0;
                int footerCount = 0;
                int[] listViewCoords = new int[2];
                mListView.getLocationOnScreen(listViewCoords);
                int x = (int) motionEvent.getRawX() - listViewCoords[0];
                int y = (int) motionEvent.getRawY() - listViewCoords[1];
                View child = null;
                for (int i = headerCount; i < (childCount - footerCount); i++) {
                    child = mListView.getChildAt(i);
                    child.getHitRect(rect);
                    if (rect.contains(x, y)) {
                        mDownView = child;
                        break;
                    }
                }
                if (mDownView != null) {
                    mDownX = motionEvent.getRawX();
                    mDownY = motionEvent.getRawY();
                    mDownPosition = mListView.getPositionForView(mDownView);
                    CardWithList.ListObject object = mListView.getAdapter().getItem(mDownPosition);
                    if (mCallbacks.canDismiss(mDownPosition, object.getParentCard(), object)) {
                        mVelocityTracker = VelocityTracker.obtain();
                        mVelocityTracker.addMovement(motionEvent);
                    } else {
                        mDownView = null;
                    }
                }
                view.onTouchEvent(motionEvent);
                return false;
            }
        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);
                    mDownView.getParent().requestDisallowInterceptTouchEvent(true);
                    mListView.requestDisallowInterceptTouchEvent(true);
                    // Cancel ListView's touch (un-highlighting the item)
                    MotionEvent cancelEvent = MotionEvent.obtain(motionEvent);
                    cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (motionEvent.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT));
                    mDownView.onTouchEvent(cancelEvent);
                    cancelEvent.recycle();
                }
                if (mSwiping) {
                    mDownView.setTranslationX(deltaX - mSwipingSlop);
                    /* mDownView.setAlpha(Math.max(0f, Math.min(1f,
                            1f - 2f * Math.abs(deltaX) / mViewWidth)));
                   */
                    return true;
                }
                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 / swipeDistanceDivisor) {
                    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
                    dismiss(mDownView, mDownPosition, dismissRight);
                } else {
                    // cancel
                    mDownView.animate().translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null);
                }
                mVelocityTracker.recycle();
                mVelocityTracker = null;
                mDownX = 0;
                mDownY = 0;
                mDownView = null;
                mDownPosition = ListView.INVALID_POSITION;
                if (mSwiping) {
                    //To prevent onClick event with a fast swipe
                    mSwiping = false;
                    return true;
                }
                mSwiping = false;
                break;
            }
        case MotionEvent.ACTION_CANCEL:
            {
                if (mVelocityTracker == null) {
                    break;
                }
                if (mDownView != null) {
                    // cancel
                    mDownView.animate().translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null);
                }
                mVelocityTracker.recycle();
                mVelocityTracker = null;
                mDownX = 0;
                mDownY = 0;
                mDownView = null;
                mDownPosition = ListView.INVALID_POSITION;
                mSwiping = false;
                break;
            }
    }
    return false;
}
Also used : Rect(android.graphics.Rect) View(android.view.View) ListView(android.widget.ListView) MotionEvent(android.view.MotionEvent)

Example 62 with MotionEvent

use of android.view.MotionEvent in project cardslib by gabrielemariotti.

the class SwipeDismissListViewTouchListener method onTouch.

/**
     * Returns an {@link AbsListView.OnScrollListener} to be added to the {@link
     * ListView} using {@link ListView#setOnScrollListener(AbsListView.OnScrollListener)}.
     * If a scroll listener is already assigned, the caller should still pass scroll changes through
     * to this listener. This will ensure that this {@link SwipeDismissListViewTouchListener} is
     * paused during list view scrolling.</p>
     *
     * @see SwipeDismissListViewTouchListener
     */
/*
    public AbsListView.OnScrollListener makeScrollListener() {
        return new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int scrollState) {
                setEnabled(scrollState != AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);
            }

            @Override
            public void onScroll(AbsListView absListView, int i, int i1, int i2) {
            }
        };
    }*/
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    if (mViewWidth < 2) {
        mViewWidth = mListView.getWidth();
    }
    switch(motionEvent.getActionMasked()) {
        case MotionEvent.ACTION_DOWN:
            {
                if (mPaused) {
                    return false;
                }
                if (mSwiping) {
                    return true;
                }
                // 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 headerCount = mListView.getHeaderViewsCount();
                int footerCount = mListView.getFooterViewsCount();
                int[] listViewCoords = new int[2];
                mListView.getLocationOnScreen(listViewCoords);
                int x = (int) motionEvent.getRawX() - listViewCoords[0];
                int y = (int) motionEvent.getRawY() - listViewCoords[1];
                View child = null;
                for (int i = headerCount; i < (childCount - footerCount); i++) {
                    child = mListView.getChildAt(i);
                    child.getHitRect(rect);
                    if (rect.contains(x, y)) {
                        mDownView = child;
                        break;
                    }
                }
                if (mDownView != null) {
                    mDownX = motionEvent.getRawX();
                    mDownY = motionEvent.getRawY();
                    mDownPosition = mListView.getPositionForView(mDownView);
                    if (mDownPosition != ListView.INVALID_POSITION && mDownPosition < mListView.getAdapter().getCount()) {
                        if (mListView.getAdapter().getItem(mDownPosition) instanceof Card) {
                            if (mCallbacks.canDismiss(mDownPosition, (Card) mListView.getAdapter().getItem(mDownPosition))) {
                                mVelocityTracker = VelocityTracker.obtain();
                                mVelocityTracker.addMovement(motionEvent);
                            } else {
                                mDownView = null;
                            }
                        } else {
                            mDownView = null;
                        }
                    } else {
                        mDownView = null;
                    }
                }
                view.onTouchEvent(motionEvent);
                return true;
            //return false;
            }
        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 / swipeDistanceDivisor && 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
                    dismiss(mDownView, mDownPosition - mListView.getHeaderViewsCount(), dismissRight);
                } else {
                    // cancel
                    mDownView.animate().translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null);
                }
                mVelocityTracker.recycle();
                mVelocityTracker = null;
                mDownX = 0;
                mDownY = 0;
                mDownView = null;
                mDownPosition = ListView.INVALID_POSITION;
                if (mSwiping) {
                    //To prevent onClick event with a fast swipe
                    mSwiping = false;
                    return true;
                }
                mSwiping = false;
                break;
            }
        case MotionEvent.ACTION_CANCEL:
            {
                if (mVelocityTracker == null) {
                    break;
                }
                if (mDownView != null) {
                    // cancel
                    mDownView.animate().translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null);
                }
                mVelocityTracker.recycle();
                mVelocityTracker = null;
                mDownX = 0;
                mDownY = 0;
                mDownView = 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;
                boolean movementAllowed = isSwipeMovementAllowed(deltaX);
                if (Math.abs(deltaX) > mSlop && Math.abs(deltaY) < Math.abs(deltaX) / 2 && movementAllowed) {
                    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 | (motionEvent.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT));
                    mListView.onTouchEvent(cancelEvent);
                    view.onTouchEvent(cancelEvent);
                    cancelEvent.recycle();
                }
                if (mSwiping) {
                    mDownView.setTranslationX(deltaX - mSwipingSlop);
                    mDownView.setAlpha(Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth)));
                    return true;
                }
                break;
            }
    }
    return false;
}
Also used : Rect(android.graphics.Rect) AbsListView(android.widget.AbsListView) View(android.view.View) ListView(android.widget.ListView) Card(it.gmariotti.cardslib.library.internal.Card) MotionEvent(android.view.MotionEvent)

Example 63 with MotionEvent

use of android.view.MotionEvent in project cardslib by gabrielemariotti.

the class SwipeDismissListViewTouchListener method performDismiss.

private void performDismiss(final View dismissView, final int dismissPosition) {
    // Animate the dismissed list item to zero-height and fire the dismiss callback when
    // all dismissed list item animations have completed. This triggers layout on each animation
    // frame; in the future we may want to do something smarter and more performant.
    final ViewGroup.LayoutParams lp = dismissView.getLayoutParams();
    final int originalHeight = dismissView.getHeight();
    ValueAnimator animator = ValueAnimator.ofInt(originalHeight, 1).setDuration(mAnimationTime);
    animator.addListener(new AnimatorListenerAdapter() {

        @Override
        public void onAnimationEnd(Animator animation) {
            --mDismissAnimationRefCount;
            if (mDismissAnimationRefCount == 0) {
                // No active animations, process all pending dismisses.
                // Sort by descending position
                Collections.sort(mPendingDismisses);
                int[] dismissPositions = new int[mPendingDismisses.size()];
                for (int i = mPendingDismisses.size() - 1; i >= 0; i--) {
                    dismissPositions[i] = mPendingDismisses.get(i).position;
                }
                mCallbacks.onDismiss(mListView, dismissPositions);
                // Reset mDownPosition to avoid MotionEvent.ACTION_UP trying to start a dismiss
                // animation with a stale position
                mDownPosition = ListView.INVALID_POSITION;
                ViewGroup.LayoutParams lp;
                for (PendingDismissData pendingDismiss : mPendingDismisses) {
                    // Reset view presentation
                    pendingDismiss.view.setAlpha(1f);
                    pendingDismiss.view.setTranslationX(0);
                    lp = pendingDismiss.view.getLayoutParams();
                    lp.height = 0;
                    pendingDismiss.view.setLayoutParams(lp);
                }
                // Send a cancel event
                long time = SystemClock.uptimeMillis();
                MotionEvent cancelEvent = MotionEvent.obtain(time, time, MotionEvent.ACTION_CANCEL, 0, 0, 0);
                mListView.dispatchTouchEvent(cancelEvent);
                mPendingDismisses.clear();
            }
        }
    });
    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

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

Example 64 with MotionEvent

use of android.view.MotionEvent in project MusicDNA by harjot-oberai.

the class PlaylistTrackAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(final PlaylistTrackAdapter.MyViewHolder holder, int position) {
    UnifiedTrack ut = songList.get(position);
    if (ut.getType()) {
        LocalTrack lt = ut.getLocalTrack();
        imgLoader.DisplayImage(lt.getPath(), holder.art);
        holder.title.setText(lt.getTitle());
        holder.artist.setText(lt.getArtist());
    } else {
        Track t = ut.getStreamTrack();
        Picasso.with(ctx).load(t.getArtworkURL()).resize(100, 100).error(R.drawable.ic_default).placeholder(R.drawable.ic_default).into(holder.art);
        holder.title.setText(t.getTitle());
        holder.artist.setText("");
    }
    holder.holderImg.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
                mDragStartListener.onDragStarted(holder);
            }
            return false;
        }
    });
}
Also used : UnifiedTrack(com.sdsmdg.harjot.MusicDNA.models.UnifiedTrack) LocalTrack(com.sdsmdg.harjot.MusicDNA.models.LocalTrack) ImageView(android.widget.ImageView) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) View(android.view.View) LocalTrack(com.sdsmdg.harjot.MusicDNA.models.LocalTrack) Track(com.sdsmdg.harjot.MusicDNA.models.Track) UnifiedTrack(com.sdsmdg.harjot.MusicDNA.models.UnifiedTrack) MotionEvent(android.view.MotionEvent)

Example 65 with MotionEvent

use of android.view.MotionEvent in project coursera-android by aporter.

the class IndicateTouchLocationActivity method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mFrame = (FrameLayout) findViewById(R.id.frame);
    // Initialize pool of View.
    initViews();
    // Create and set on touch listener
    mFrame.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch(event.getActionMasked()) {
                case MotionEvent.ACTION_DOWN:
                case MotionEvent.ACTION_POINTER_DOWN:
                    {
                        int pointerIndex = event.getActionIndex();
                        int pointerID = event.getPointerId(pointerIndex);
                        MarkerView marker = mInactiveMarkers.remove();
                        if (null != marker) {
                            mActiveMarkers.put(pointerID, marker);
                            marker.setXLoc(event.getX(pointerIndex));
                            marker.setYLoc(event.getY(pointerIndex));
                            updateTouches(mActiveMarkers.size());
                            mFrame.addView(marker);
                        }
                        break;
                    }
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_POINTER_UP:
                    {
                        int pointerIndex = event.getActionIndex();
                        int pointerID = event.getPointerId(pointerIndex);
                        MarkerView marker = mActiveMarkers.remove(pointerID);
                        if (null != marker) {
                            mInactiveMarkers.add(marker);
                            updateTouches(mActiveMarkers.size());
                            mFrame.removeView(marker);
                        }
                        break;
                    }
                case MotionEvent.ACTION_MOVE:
                    {
                        for (int idx = 0; idx < event.getPointerCount(); idx++) {
                            int ID = event.getPointerId(idx);
                            MarkerView marker = mActiveMarkers.get(ID);
                            if (null != marker) {
                                // Redraw only if finger has travel ed a minimum distance   
                                if (Math.abs(marker.getXLoc() - event.getX(idx)) > MIN_DXDY || Math.abs(marker.getYLoc() - event.getY(idx)) > MIN_DXDY) {
                                    // Set new location
                                    marker.setXLoc(event.getX(idx));
                                    marker.setYLoc(event.getY(idx));
                                    // Request re-draw
                                    marker.invalidate();
                                }
                            }
                        }
                        break;
                    }
                default:
                    Log.i(TAG, "unhandled action");
            }
            return true;
        }

        // update number of touches on each active MarkerView
        private void updateTouches(int numActive) {
            for (MarkerView marker : mActiveMarkers.values()) {
                marker.setTouches(numActive);
            }
        }
    });
}
Also used : OnTouchListener(android.view.View.OnTouchListener) View(android.view.View) SuppressLint(android.annotation.SuppressLint) Paint(android.graphics.Paint) MotionEvent(android.view.MotionEvent)

Aggregations

MotionEvent (android.view.MotionEvent)717 View (android.view.View)255 TextView (android.widget.TextView)113 ImageView (android.widget.ImageView)91 Point (android.graphics.Point)63 OnTouchListener (android.view.View.OnTouchListener)48 GestureDetector (android.view.GestureDetector)47 Paint (android.graphics.Paint)40 ViewGroup (android.view.ViewGroup)36 Test (org.junit.Test)35 AdapterView (android.widget.AdapterView)34 RecyclerView (android.support.v7.widget.RecyclerView)33 ListView (android.widget.ListView)33 SuppressLint (android.annotation.SuppressLint)31 Intent (android.content.Intent)30 Instrumentation (android.app.Instrumentation)27 AbsListView (android.widget.AbsListView)27 OnClickListener (android.view.View.OnClickListener)24 Rect (android.graphics.Rect)22 Handler (android.os.Handler)20