Search in sources :

Example 56 with TransitionDrawable

use of android.graphics.drawable.TransitionDrawable in project platform_frameworks_base by android.

the class AbsListView method keyPressed.

/**
     * Sets the selector state to "pressed" and posts a CheckForKeyLongPress to see if
     * this is a long press.
     */
void keyPressed() {
    if (!isEnabled() || !isClickable()) {
        return;
    }
    Drawable selector = mSelector;
    Rect selectorRect = mSelectorRect;
    if (selector != null && (isFocused() || touchModeDrawsInPressedState()) && !selectorRect.isEmpty()) {
        final View v = getChildAt(mSelectedPosition - mFirstPosition);
        if (v != null) {
            if (v.hasFocusable())
                return;
            v.setPressed(true);
        }
        setPressed(true);
        final boolean longClickable = isLongClickable();
        Drawable d = selector.getCurrent();
        if (d != null && d instanceof TransitionDrawable) {
            if (longClickable) {
                ((TransitionDrawable) d).startTransition(ViewConfiguration.getLongPressTimeout());
            } else {
                ((TransitionDrawable) d).resetTransition();
            }
        }
        if (longClickable && !mDataChanged) {
            if (mPendingCheckForKeyLongPress == null) {
                mPendingCheckForKeyLongPress = new CheckForKeyLongPress();
            }
            mPendingCheckForKeyLongPress.rememberWindowAttachCount();
            postDelayed(mPendingCheckForKeyLongPress, ViewConfiguration.getLongPressTimeout());
        }
    }
}
Also used : Rect(android.graphics.Rect) TransitionDrawable(android.graphics.drawable.TransitionDrawable) Drawable(android.graphics.drawable.Drawable) TransitionDrawable(android.graphics.drawable.TransitionDrawable) View(android.view.View)

Example 57 with TransitionDrawable

use of android.graphics.drawable.TransitionDrawable in project plaid by nickbutcher.

the class FeedAdapter method createDribbbleShotHolder.

@NonNull
private DribbbleShotHolder createDribbbleShotHolder(ViewGroup parent) {
    final DribbbleShotHolder holder = new DribbbleShotHolder(layoutInflater.inflate(R.layout.dribbble_shot_item, parent, false));
    holder.image.setBadgeColor(initialGifBadgeColor);
    holder.image.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            Intent intent = new Intent();
            intent.setClass(host, DribbbleShot.class);
            intent.putExtra(DribbbleShot.EXTRA_SHOT, (Shot) getItem(holder.getAdapterPosition()));
            setGridItemContentTransitions(holder.image);
            ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(host, Pair.create(view, host.getString(R.string.transition_shot)), Pair.create(view, host.getString(R.string.transition_shot_background)));
            host.startActivityForResult(intent, REQUEST_CODE_VIEW_SHOT, options.toBundle());
        }
    });
    // play animated GIFs whilst touched
    holder.image.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // check if it's an event we care about, else bail fast
            final int action = event.getAction();
            if (!(action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL))
                return false;
            // get the image and check if it's an animated GIF
            final Drawable drawable = holder.image.getDrawable();
            if (drawable == null)
                return false;
            GifDrawable gif = null;
            if (drawable instanceof GifDrawable) {
                gif = (GifDrawable) drawable;
            } else if (drawable instanceof TransitionDrawable) {
                // we fade in images on load which uses a TransitionDrawable; check its layers
                TransitionDrawable fadingIn = (TransitionDrawable) drawable;
                for (int i = 0; i < fadingIn.getNumberOfLayers(); i++) {
                    if (fadingIn.getDrawable(i) instanceof GifDrawable) {
                        gif = (GifDrawable) fadingIn.getDrawable(i);
                        break;
                    }
                }
            }
            if (gif == null)
                return false;
            // GIF found, start/stop it on press/lift
            switch(action) {
                case MotionEvent.ACTION_DOWN:
                    gif.start();
                    break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    gif.stop();
                    break;
            }
            return false;
        }
    });
    return holder;
}
Also used : TransitionDrawable(android.graphics.drawable.TransitionDrawable) ColorDrawable(android.graphics.drawable.ColorDrawable) Drawable(android.graphics.drawable.Drawable) TransitionDrawable(android.graphics.drawable.TransitionDrawable) GlideDrawable(com.bumptech.glide.load.resource.drawable.GlideDrawable) GifDrawable(com.bumptech.glide.load.resource.gif.GifDrawable) CustomTabsIntent(android.support.customtabs.CustomTabsIntent) Intent(android.content.Intent) GifDrawable(com.bumptech.glide.load.resource.gif.GifDrawable) BindView(butterknife.BindView) View(android.view.View) TextView(android.widget.TextView) BadgedFourThreeImageView(io.plaidapp.ui.widget.BadgedFourThreeImageView) RecyclerView(android.support.v7.widget.RecyclerView) MotionEvent(android.view.MotionEvent) Shot(io.plaidapp.data.api.dribbble.model.Shot) ActivityOptions(android.app.ActivityOptions) NonNull(android.support.annotation.NonNull)

Example 58 with TransitionDrawable

use of android.graphics.drawable.TransitionDrawable in project android_frameworks_base by ParanoidAndroid.

the class AbsListView method keyPressed.

/**
     * Sets the selector state to "pressed" and posts a CheckForKeyLongPress to see if
     * this is a long press.
     */
void keyPressed() {
    if (!isEnabled() || !isClickable()) {
        return;
    }
    Drawable selector = mSelector;
    Rect selectorRect = mSelectorRect;
    if (selector != null && (isFocused() || touchModeDrawsInPressedState()) && !selectorRect.isEmpty()) {
        final View v = getChildAt(mSelectedPosition - mFirstPosition);
        if (v != null) {
            if (v.hasFocusable())
                return;
            v.setPressed(true);
        }
        setPressed(true);
        final boolean longClickable = isLongClickable();
        Drawable d = selector.getCurrent();
        if (d != null && d instanceof TransitionDrawable) {
            if (longClickable) {
                ((TransitionDrawable) d).startTransition(ViewConfiguration.getLongPressTimeout());
            } else {
                ((TransitionDrawable) d).resetTransition();
            }
        }
        if (longClickable && !mDataChanged) {
            if (mPendingCheckForKeyLongPress == null) {
                mPendingCheckForKeyLongPress = new CheckForKeyLongPress();
            }
            mPendingCheckForKeyLongPress.rememberWindowAttachCount();
            postDelayed(mPendingCheckForKeyLongPress, ViewConfiguration.getLongPressTimeout());
        }
    }
}
Also used : Rect(android.graphics.Rect) TransitionDrawable(android.graphics.drawable.TransitionDrawable) Drawable(android.graphics.drawable.Drawable) TransitionDrawable(android.graphics.drawable.TransitionDrawable) View(android.view.View)

Example 59 with TransitionDrawable

use of android.graphics.drawable.TransitionDrawable in project XobotOS by xamarin.

the class AbsListView method keyPressed.

/**
     * Sets the selector state to "pressed" and posts a CheckForKeyLongPress to see if
     * this is a long press.
     */
void keyPressed() {
    if (!isEnabled() || !isClickable()) {
        return;
    }
    Drawable selector = mSelector;
    Rect selectorRect = mSelectorRect;
    if (selector != null && (isFocused() || touchModeDrawsInPressedState()) && !selectorRect.isEmpty()) {
        final View v = getChildAt(mSelectedPosition - mFirstPosition);
        if (v != null) {
            if (v.hasFocusable())
                return;
            v.setPressed(true);
        }
        setPressed(true);
        final boolean longClickable = isLongClickable();
        Drawable d = selector.getCurrent();
        if (d != null && d instanceof TransitionDrawable) {
            if (longClickable) {
                ((TransitionDrawable) d).startTransition(ViewConfiguration.getLongPressTimeout());
            } else {
                ((TransitionDrawable) d).resetTransition();
            }
        }
        if (longClickable && !mDataChanged) {
            if (mPendingCheckForKeyLongPress == null) {
                mPendingCheckForKeyLongPress = new CheckForKeyLongPress();
            }
            mPendingCheckForKeyLongPress.rememberWindowAttachCount();
            postDelayed(mPendingCheckForKeyLongPress, ViewConfiguration.getLongPressTimeout());
        }
    }
}
Also used : Rect(android.graphics.Rect) TransitionDrawable(android.graphics.drawable.TransitionDrawable) Drawable(android.graphics.drawable.Drawable) TransitionDrawable(android.graphics.drawable.TransitionDrawable) View(android.view.View)

Example 60 with TransitionDrawable

use of android.graphics.drawable.TransitionDrawable in project XobotOS by xamarin.

the class AbsListView method onTouchEvent.

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (!isEnabled()) {
        // events, it just doesn't respond to them.
        return isClickable() || isLongClickable();
    }
    if (mFastScroller != null) {
        boolean intercepted = mFastScroller.onTouchEvent(ev);
        if (intercepted) {
            return true;
        }
    }
    final int action = ev.getAction();
    View v;
    initVelocityTrackerIfNotExists();
    mVelocityTracker.addMovement(ev);
    switch(action & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            {
                switch(mTouchMode) {
                    case TOUCH_MODE_OVERFLING:
                        {
                            mFlingRunnable.endFling();
                            if (mPositionScroller != null) {
                                mPositionScroller.stop();
                            }
                            mTouchMode = TOUCH_MODE_OVERSCROLL;
                            mMotionX = (int) ev.getX();
                            mMotionY = mLastY = (int) ev.getY();
                            mMotionCorrection = 0;
                            mActivePointerId = ev.getPointerId(0);
                            mDirection = 0;
                            break;
                        }
                    default:
                        {
                            mActivePointerId = ev.getPointerId(0);
                            final int x = (int) ev.getX();
                            final int y = (int) ev.getY();
                            int motionPosition = pointToPosition(x, y);
                            if (!mDataChanged) {
                                if ((mTouchMode != TOUCH_MODE_FLING) && (motionPosition >= 0) && (getAdapter().isEnabled(motionPosition))) {
                                    // User clicked on an actual view (and was not stopping a fling).
                                    // It might be a click or a scroll. Assume it is a click until
                                    // proven otherwise
                                    mTouchMode = TOUCH_MODE_DOWN;
                                    // FIXME Debounce
                                    if (mPendingCheckForTap == null) {
                                        mPendingCheckForTap = new CheckForTap();
                                    }
                                    postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
                                } else {
                                    if (mTouchMode == TOUCH_MODE_FLING) {
                                        // Stopped a fling. It is a scroll.
                                        createScrollingCache();
                                        mTouchMode = TOUCH_MODE_SCROLL;
                                        mMotionCorrection = 0;
                                        motionPosition = findMotionRow(y);
                                        mFlingRunnable.flywheelTouch();
                                    }
                                }
                            }
                            if (motionPosition >= 0) {
                                // Remember where the motion event started
                                v = getChildAt(motionPosition - mFirstPosition);
                                mMotionViewOriginalTop = v.getTop();
                            }
                            mMotionX = x;
                            mMotionY = y;
                            mMotionPosition = motionPosition;
                            mLastY = Integer.MIN_VALUE;
                            break;
                        }
                }
                if (performButtonActionOnTouchDown(ev)) {
                    if (mTouchMode == TOUCH_MODE_DOWN) {
                        removeCallbacks(mPendingCheckForTap);
                    }
                }
                break;
            }
        case MotionEvent.ACTION_MOVE:
            {
                int pointerIndex = ev.findPointerIndex(mActivePointerId);
                if (pointerIndex == -1) {
                    pointerIndex = 0;
                    mActivePointerId = ev.getPointerId(pointerIndex);
                }
                final int y = (int) ev.getY(pointerIndex);
                switch(mTouchMode) {
                    case TOUCH_MODE_DOWN:
                    case TOUCH_MODE_TAP:
                    case TOUCH_MODE_DONE_WAITING:
                        // Check if we have moved far enough that it looks more like a
                        // scroll than a tap
                        startScrollIfNeeded(y);
                        break;
                    case TOUCH_MODE_SCROLL:
                    case TOUCH_MODE_OVERSCROLL:
                        scrollIfNeeded(y);
                        break;
                }
                break;
            }
        case MotionEvent.ACTION_UP:
            {
                switch(mTouchMode) {
                    case TOUCH_MODE_DOWN:
                    case TOUCH_MODE_TAP:
                    case TOUCH_MODE_DONE_WAITING:
                        final int motionPosition = mMotionPosition;
                        final View child = getChildAt(motionPosition - mFirstPosition);
                        final float x = ev.getX();
                        final boolean inList = x > mListPadding.left && x < getWidth() - mListPadding.right;
                        if (child != null && !child.hasFocusable() && inList) {
                            if (mTouchMode != TOUCH_MODE_DOWN) {
                                child.setPressed(false);
                            }
                            if (mPerformClick == null) {
                                mPerformClick = new PerformClick();
                            }
                            final AbsListView.PerformClick performClick = mPerformClick;
                            performClick.mClickMotionPosition = motionPosition;
                            performClick.rememberWindowAttachCount();
                            mResurrectToPosition = motionPosition;
                            if (mTouchMode == TOUCH_MODE_DOWN || mTouchMode == TOUCH_MODE_TAP) {
                                final Handler handler = getHandler();
                                if (handler != null) {
                                    handler.removeCallbacks(mTouchMode == TOUCH_MODE_DOWN ? mPendingCheckForTap : mPendingCheckForLongPress);
                                }
                                mLayoutMode = LAYOUT_NORMAL;
                                if (!mDataChanged && mAdapter.isEnabled(motionPosition)) {
                                    mTouchMode = TOUCH_MODE_TAP;
                                    setSelectedPositionInt(mMotionPosition);
                                    layoutChildren();
                                    child.setPressed(true);
                                    positionSelector(mMotionPosition, child);
                                    setPressed(true);
                                    if (mSelector != null) {
                                        Drawable d = mSelector.getCurrent();
                                        if (d != null && d instanceof TransitionDrawable) {
                                            ((TransitionDrawable) d).resetTransition();
                                        }
                                    }
                                    if (mTouchModeReset != null) {
                                        removeCallbacks(mTouchModeReset);
                                    }
                                    mTouchModeReset = new Runnable() {

                                        @Override
                                        public void run() {
                                            mTouchMode = TOUCH_MODE_REST;
                                            child.setPressed(false);
                                            setPressed(false);
                                            if (!mDataChanged) {
                                                performClick.run();
                                            }
                                        }
                                    };
                                    postDelayed(mTouchModeReset, ViewConfiguration.getPressedStateDuration());
                                } else {
                                    mTouchMode = TOUCH_MODE_REST;
                                    updateSelectorState();
                                }
                                return true;
                            } else if (!mDataChanged && mAdapter.isEnabled(motionPosition)) {
                                performClick.run();
                            }
                        }
                        mTouchMode = TOUCH_MODE_REST;
                        updateSelectorState();
                        break;
                    case TOUCH_MODE_SCROLL:
                        final int childCount = getChildCount();
                        if (childCount > 0) {
                            final int firstChildTop = getChildAt(0).getTop();
                            final int lastChildBottom = getChildAt(childCount - 1).getBottom();
                            final int contentTop = mListPadding.top;
                            final int contentBottom = getHeight() - mListPadding.bottom;
                            if (mFirstPosition == 0 && firstChildTop >= contentTop && mFirstPosition + childCount < mItemCount && lastChildBottom <= getHeight() - contentBottom) {
                                mTouchMode = TOUCH_MODE_REST;
                                reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
                            } else {
                                final VelocityTracker velocityTracker = mVelocityTracker;
                                velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
                                final int initialVelocity = (int) (velocityTracker.getYVelocity(mActivePointerId) * mVelocityScale);
                                // fling further.
                                if (Math.abs(initialVelocity) > mMinimumVelocity && !((mFirstPosition == 0 && firstChildTop == contentTop - mOverscrollDistance) || (mFirstPosition + childCount == mItemCount && lastChildBottom == contentBottom + mOverscrollDistance))) {
                                    if (mFlingRunnable == null) {
                                        mFlingRunnable = new FlingRunnable();
                                    }
                                    reportScrollStateChange(OnScrollListener.SCROLL_STATE_FLING);
                                    mFlingRunnable.start(-initialVelocity);
                                } else {
                                    mTouchMode = TOUCH_MODE_REST;
                                    reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
                                    if (mFlingRunnable != null) {
                                        mFlingRunnable.endFling();
                                    }
                                    if (mPositionScroller != null) {
                                        mPositionScroller.stop();
                                    }
                                }
                            }
                        } else {
                            mTouchMode = TOUCH_MODE_REST;
                            reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
                        }
                        break;
                    case TOUCH_MODE_OVERSCROLL:
                        if (mFlingRunnable == null) {
                            mFlingRunnable = new FlingRunnable();
                        }
                        final VelocityTracker velocityTracker = mVelocityTracker;
                        velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
                        final int initialVelocity = (int) velocityTracker.getYVelocity(mActivePointerId);
                        reportScrollStateChange(OnScrollListener.SCROLL_STATE_FLING);
                        if (Math.abs(initialVelocity) > mMinimumVelocity) {
                            mFlingRunnable.startOverfling(-initialVelocity);
                        } else {
                            mFlingRunnable.startSpringback();
                        }
                        break;
                }
                setPressed(false);
                if (mEdgeGlowTop != null) {
                    mEdgeGlowTop.onRelease();
                    mEdgeGlowBottom.onRelease();
                }
                // Need to redraw since we probably aren't drawing the selector anymore
                invalidate();
                final Handler handler = getHandler();
                if (handler != null) {
                    handler.removeCallbacks(mPendingCheckForLongPress);
                }
                recycleVelocityTracker();
                mActivePointerId = INVALID_POINTER;
                if (PROFILE_SCROLLING) {
                    if (mScrollProfilingStarted) {
                        Debug.stopMethodTracing();
                        mScrollProfilingStarted = false;
                    }
                }
                if (mScrollStrictSpan != null) {
                    mScrollStrictSpan.finish();
                    mScrollStrictSpan = null;
                }
                break;
            }
        case MotionEvent.ACTION_CANCEL:
            {
                switch(mTouchMode) {
                    case TOUCH_MODE_OVERSCROLL:
                        if (mFlingRunnable == null) {
                            mFlingRunnable = new FlingRunnable();
                        }
                        mFlingRunnable.startSpringback();
                        break;
                    case TOUCH_MODE_OVERFLING:
                        // Do nothing - let it play out.
                        break;
                    default:
                        mTouchMode = TOUCH_MODE_REST;
                        setPressed(false);
                        View motionView = this.getChildAt(mMotionPosition - mFirstPosition);
                        if (motionView != null) {
                            motionView.setPressed(false);
                        }
                        clearScrollingCache();
                        final Handler handler = getHandler();
                        if (handler != null) {
                            handler.removeCallbacks(mPendingCheckForLongPress);
                        }
                        recycleVelocityTracker();
                }
                if (mEdgeGlowTop != null) {
                    mEdgeGlowTop.onRelease();
                    mEdgeGlowBottom.onRelease();
                }
                mActivePointerId = INVALID_POINTER;
                break;
            }
        case MotionEvent.ACTION_POINTER_UP:
            {
                onSecondaryPointerUp(ev);
                final int x = mMotionX;
                final int y = mMotionY;
                final int motionPosition = pointToPosition(x, y);
                if (motionPosition >= 0) {
                    // Remember where the motion event started
                    v = getChildAt(motionPosition - mFirstPosition);
                    mMotionViewOriginalTop = v.getTop();
                    mMotionPosition = motionPosition;
                }
                mLastY = y;
                break;
            }
        case MotionEvent.ACTION_POINTER_DOWN:
            {
                // New pointers take over dragging duties
                final int index = ev.getActionIndex();
                final int id = ev.getPointerId(index);
                final int x = (int) ev.getX(index);
                final int y = (int) ev.getY(index);
                mMotionCorrection = 0;
                mActivePointerId = id;
                mMotionX = x;
                mMotionY = y;
                final int motionPosition = pointToPosition(x, y);
                if (motionPosition >= 0) {
                    // Remember where the motion event started
                    v = getChildAt(motionPosition - mFirstPosition);
                    mMotionViewOriginalTop = v.getTop();
                    mMotionPosition = motionPosition;
                }
                mLastY = y;
                break;
            }
    }
    return true;
}
Also used : TransitionDrawable(android.graphics.drawable.TransitionDrawable) VelocityTracker(android.view.VelocityTracker) Drawable(android.graphics.drawable.Drawable) TransitionDrawable(android.graphics.drawable.TransitionDrawable) Handler(android.os.Handler) View(android.view.View)

Aggregations

TransitionDrawable (android.graphics.drawable.TransitionDrawable)96 Drawable (android.graphics.drawable.Drawable)61 ColorDrawable (android.graphics.drawable.ColorDrawable)35 BitmapDrawable (android.graphics.drawable.BitmapDrawable)31 View (android.view.View)28 VelocityTracker (android.view.VelocityTracker)11 ImageView (android.widget.ImageView)10 Rect (android.graphics.Rect)9 Handler (android.os.Handler)8 Test (org.junit.Test)6 Bitmap (android.graphics.Bitmap)5 LayerDrawable (android.graphics.drawable.LayerDrawable)5 GradientDrawable (android.graphics.drawable.GradientDrawable)3 RippleDrawable (android.graphics.drawable.RippleDrawable)3 SuppressLint (android.annotation.SuppressLint)2 Intent (android.content.Intent)2 ColorStateList (android.content.res.ColorStateList)2 Resources (android.content.res.Resources)2 ContentObserver (android.database.ContentObserver)2 Canvas (android.graphics.Canvas)2