Example usage for android.graphics Rect contains

List of usage examples for android.graphics Rect contains

Introduction

In this page you can find the example usage for android.graphics Rect contains.

Prototype

public boolean contains(int x, int y) 

Source Link

Document

Returns true if (x,y) is inside the rectangle.

Usage

From source file:com.icloud.listenbook.base.view.DraggableGridViewPager.java

/**
 * ?//from w ww . ja va2 s  . c om
 * */
private int getTargetByXY(int x, int y) {
    // ?
    final int position = getPositionByXY(x, y);
    if (position < 0) {
        return -1;
    }
    // ?
    final Rect r = getRectByPosition(position);
    final int page = position / mPageSize;
    // ?? ??
    r.inset(r.width() / 4, r.height() / 4);
    // ??
    r.offset(-getWidth() * page, 0);
    // ?? ?
    if (!r.contains(x, y)) {
        return -1;
    }
    return position;
}

From source file:net.simonvt.staggeredgridview.StaggeredGridView.java

private int getPositionAt(int x, int y) {
    Rect frame = new Rect();
    final int count = getChildCount();
    for (int i = count - 1; i >= 0; i--) {
        final View child = getChildAt(i);
        if (child.getVisibility() == View.VISIBLE) {
            child.getHitRect(frame);//from  w  w w. j  av a 2s  .  com
            if (frame.contains(x, y)) {
                return firstPosition + i;
            }
        }
    }

    return INVALID_POSITION;
}

From source file:com.vuze.android.remote.fragment.TorrentListFragment.java

private boolean isViewContains(View view, int rx, int ry) {
    Rect rect = new Rect();
    view.getGlobalVisibleRect(rect);/*from  www. j  a  va2s.  com*/

    return rect.contains(rx, ry);
}

From source file:com.vnidens.clickableedittext.ClickableEditTextHelper.java

private boolean isTouchInside(@NonNull View v, int xTouch, int yTouch, int drawablePosition) {
    Rect iconRect;

    int viewPaddingStart = ViewCompat.getPaddingStart(v);
    int viewPaddingEnd = ViewCompat.getPaddingEnd(v);
    int ltDirection = ViewCompat.getLayoutDirection(v);

    switch (drawablePosition) {
    case DRAWABLE_POSITION_START:
        if (startButtonDrawable == null || onStartButtonClickListener == null) {
            return false;
        }//from   www.jav a2  s .co m

        iconRect = startButtonDrawable.copyBounds();
        break;

    case DRAWABLE_POSITION_END:
        if (endButtonDrawable == null || onEndButtonClickListener == null) {
            return false;
        }

        iconRect = endButtonDrawable.copyBounds();
        break;

    default:
        return false;
    }

    int viewCenterVertical = v.getPaddingTop()
            + ((v.getHeight() - v.getPaddingBottom()) - v.getPaddingTop()) / 2;

    if ((drawablePosition == DRAWABLE_POSITION_START && ltDirection == ViewCompat.LAYOUT_DIRECTION_LTR)
            || (drawablePosition == DRAWABLE_POSITION_END && ltDirection == ViewCompat.LAYOUT_DIRECTION_RTL)) {

        iconRect.offsetTo(viewPaddingStart, viewCenterVertical - iconRect.centerY());

    } else if ((drawablePosition == DRAWABLE_POSITION_START && ltDirection == ViewCompat.LAYOUT_DIRECTION_RTL)
            || (drawablePosition == DRAWABLE_POSITION_END && ltDirection == ViewCompat.LAYOUT_DIRECTION_LTR)) {

        iconRect.offsetTo(v.getWidth() - viewPaddingEnd - iconRect.width(),
                viewCenterVertical - iconRect.centerY());

    } else {
        return false;
    }

    iconRect.left = iconRect.left - extraTouchableAreaPx;
    iconRect.right = iconRect.right + extraTouchableAreaPx;
    iconRect.top = iconRect.top - extraTouchableAreaPx;
    iconRect.bottom = iconRect.bottom + extraTouchableAreaPx;

    return iconRect.contains(xTouch, yTouch);
}

From source file:com.doubleTwist.drawerlib.ADrawerLayout.java

private boolean touchIsInDrawerArea(int drawerId, float x, float y) {
    Rect drawerBounds = null;
    switch (drawerId) {
    case LEFT_DRAWER:
        drawerBounds = mLayoutBoundsWithoutPeek.get(mLeft);
        break;/*from  w  ww. ja v a  2 s.  c  o  m*/
    case RIGHT_DRAWER:
        drawerBounds = mLayoutBoundsWithoutPeek.get(mRight);
        break;
    case TOP_DRAWER:
        drawerBounds = mLayoutBoundsWithoutPeek.get(mTop);
        break;
    case BOTTOM_DRAWER:
        drawerBounds = mLayoutBoundsWithoutPeek.get(mBottom);
        break;
    }

    return drawerBounds == null ? false : drawerBounds.contains((int) x, (int) y);
}

From source file:com.android.camera.HighlightView.java

public int getHit(float x, float y) {
    Rect r = computeLayout();
    final float hysteresis = 20F;
    int retval = GROW_NONE;

    if (mCircle) {
        float distX = x - r.centerX();
        float distY = y - r.centerY();
        int distanceFromCenter = (int) Math.sqrt(distX * distX + distY * distY);
        int radius = mDrawRect.width() / 2;
        int delta = distanceFromCenter - radius;
        if (Math.abs(delta) <= hysteresis) {
            if (Math.abs(distY) > Math.abs(distX)) {
                if (distY < 0) {
                    retval = GROW_TOP_EDGE;
                } else {
                    retval = GROW_BOTTOM_EDGE;
                }//from  ww w.  j a  v  a2  s  . c o  m
            } else {
                if (distX < 0) {
                    retval = GROW_LEFT_EDGE;
                } else {
                    retval = GROW_RIGHT_EDGE;
                }
            }
        } else if (distanceFromCenter < radius) {
            retval = MOVE;
        } else {
            retval = GROW_NONE;
        }
    } else {
        // verticalCheck makes sure the position is between the top and
        // the bottom edge (with some tolerance). Similar for horizCheck.
        boolean verticalCheck = (y >= r.top - hysteresis) && (y < r.bottom + hysteresis);
        boolean horizCheck = (x >= r.left - hysteresis) && (x < r.right + hysteresis);

        // Check whether the position is near some edge(s).
        if ((Math.abs(r.left - x) < hysteresis) && verticalCheck) {
            retval |= GROW_LEFT_EDGE;
        }
        if ((Math.abs(r.right - x) < hysteresis) && verticalCheck) {
            retval |= GROW_RIGHT_EDGE;
        }
        if ((Math.abs(r.top - y) < hysteresis) && horizCheck) {
            retval |= GROW_TOP_EDGE;
        }
        if ((Math.abs(r.bottom - y) < hysteresis) && horizCheck) {
            retval |= GROW_BOTTOM_EDGE;
        }

        // Not near any edge but inside the rectangle: move.
        if (retval == GROW_NONE && r.contains((int) x, (int) y)) {
            retval = MOVE;
        }
    }
    return retval;
}

From source file:com.example.gatsu.theevent.HighlightView.java

public int getHit(float x, float y) {

    Rect r = computeLayout();
    final float hysteresis = 20F;
    int retval = GROW_NONE;

    if (mCircle) {
        float distX = x - r.centerX();
        float distY = y - r.centerY();
        int distanceFromCenter = (int) Math.sqrt(distX * distX + distY * distY);
        int radius = mDrawRect.width() / 2;
        int delta = distanceFromCenter - radius;
        if (Math.abs(delta) <= hysteresis) {
            if (Math.abs(distY) > Math.abs(distX)) {
                if (distY < 0) {
                    retval = GROW_TOP_EDGE;
                } else {
                    retval = GROW_BOTTOM_EDGE;
                }// www.j a v a2  s  .  c o  m
            } else {
                if (distX < 0) {
                    retval = GROW_LEFT_EDGE;
                } else {
                    retval = GROW_RIGHT_EDGE;
                }
            }
        } else if (distanceFromCenter < radius) {
            retval = MOVE;
        } else {
            retval = GROW_NONE;
        }
    } else {
        // verticalCheck makes sure the position is between the top and
        // the bottom edge (with some tolerance). Similar for horizCheck.
        boolean verticalCheck = (y >= r.top - hysteresis) && (y < r.bottom + hysteresis);
        boolean horizCheck = (x >= r.left - hysteresis) && (x < r.right + hysteresis);

        // Check whether the position is near some edge(s).
        if ((Math.abs(r.left - x) < hysteresis) && verticalCheck) {
            retval |= GROW_LEFT_EDGE;
        }
        if ((Math.abs(r.right - x) < hysteresis) && verticalCheck) {
            retval |= GROW_RIGHT_EDGE;
        }
        if ((Math.abs(r.top - y) < hysteresis) && horizCheck) {
            retval |= GROW_TOP_EDGE;
        }
        if ((Math.abs(r.bottom - y) < hysteresis) && horizCheck) {
            retval |= GROW_BOTTOM_EDGE;
        }

        // Not near any edge but inside the rectangle: move.
        if (retval == GROW_NONE && r.contains((int) x, (int) y)) {
            retval = MOVE;
        }
    }
    return retval;
}

From source file:com.haibison.android.anhuu.FragmentFiles.java

/**
 * This should be called after the owner activity has been created
 * successfully.//from ww w. j  ava  2s.  c om
 */
private void initGestureDetector() {
    mListviewFilesGestureDetector = new GestureDetector(getActivity(),
            new GestureDetector.SimpleOnGestureListener() {

                private Object getData(float x, float y) {
                    int i = getSubViewId(x, y);
                    if (i >= 0)
                        return mViewFiles.getItemAtPosition(mViewFiles.getFirstVisiblePosition() + i);
                    return null;
                }// getSubView()

                private int getSubViewId(float x, float y) {
                    Rect r = new Rect();
                    for (int i = 0; i < mViewFiles.getChildCount(); i++) {
                        mViewFiles.getChildAt(i).getHitRect(r);
                        if (r.contains((int) x, (int) y))
                            return i;
                    }

                    return -1;
                }// getSubViewId()

                /**
                 * Gets {@link Cursor} from {@code e}.
                 * 
                 * @param e
                 *            {@link MotionEvent}.
                 * @return the cursor, or {@code null} if not available.
                 */
                private Cursor getData(MotionEvent e) {
                    Object o = getData(e.getX(), e.getY());
                    return o instanceof Cursor ? (Cursor) o : null;
                }// getDataModel()

                @Override
                public void onLongPress(MotionEvent e) {
                    /*
                     * Do nothing.
                     */
                }// onLongPress()

                @Override
                public boolean onSingleTapConfirmed(MotionEvent e) {
                    /*
                     * Do nothing.
                     */
                    return false;
                }// onSingleTapConfirmed()

                @Override
                public boolean onDoubleTap(MotionEvent e) {
                    if (mDoubleTapToChooseFiles) {
                        if (mIsMultiSelection)
                            return false;

                        Cursor cursor = getData(e);
                        if (cursor == null)
                            return false;

                        if (BaseFileProviderUtils.isDirectory(cursor)
                                && BaseFile.FILTER_FILES_ONLY == mFilterMode)
                            return false;

                        /*
                         * If mFilterMode == FILTER_DIRECTORIES_ONLY, files
                         * won't be shown.
                         */

                        if (mIsSaveDialog) {
                            if (BaseFileProviderUtils.isFile(cursor)) {
                                mTextSaveas.setText(BaseFileProviderUtils.getFileName(cursor));
                                /*
                                 * Always set tag after setting text, or tag
                                 * will be reset to null.
                                 */
                                mTextSaveas.setTag(BaseFileProviderUtils.getUri(cursor));
                                checkSaveasFilenameAndFinish();
                            } else
                                return false;
                        } else
                            finish(BaseFileProviderUtils.getUri(cursor));
                    } // double tap to choose files
                    else {
                        /*
                         * Do nothing.
                         */
                        return false;
                    } // single tap to choose files

                    return true;
                }// onDoubleTap()

                @Override
                public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                    /*
                     * Sometimes e1 or e2 can be null. This came from users'
                     * experiences.
                     */
                    if (e1 == null || e2 == null)
                        return false;

                    final int max_y_distance = 19;// 10 is too short :-D
                    final int min_x_distance = 80;
                    final int min_x_velocity = 200;
                    if (Math.abs(e1.getY() - e2.getY()) < max_y_distance
                            && Math.abs(e1.getX() - e2.getX()) > min_x_distance
                            && Math.abs(velocityX) > min_x_velocity) {
                        int pos = getSubViewId(e1.getX(), e1.getY());
                        if (pos >= 0) {
                            /*
                             * Don't let this event to be recognized as a
                             * single tap.
                             */
                            MotionEvent cancelEvent = MotionEvent.obtain(e1);
                            cancelEvent.setAction(MotionEvent.ACTION_CANCEL);
                            mViewFiles.onTouchEvent(cancelEvent);

                            deleteFile(mViewFiles.getFirstVisiblePosition() + pos);
                        }
                    }

                    /*
                     * Always return false to let the default handler draw
                     * the item properly.
                     */
                    return false;
                }// onFling()
            });// mListviewFilesGestureDetector
}

From source file:android.support.designox.widget.CoordinatorLayout.java

/**
 * Check if a given point in the CoordinatorLayout's coordinates are within the view bounds
 * of the given direct child view./*w ww.  ja  va  2s. c  om*/
 *
 * @param child child view to test
 * @param x X coordinate to test, in the CoordinatorLayout's coordinate system
 * @param y Y coordinate to test, in the CoordinatorLayout's coordinate system
 * @return true if the point is within the child view's bounds, false otherwise
 */
public boolean isPointInChildBounds(View child, int x, int y) {
    final Rect r = mTempRect1;
    getDescendantRect(child, r);
    return r.contains(x, y);
}

From source file:com.google.android.apps.santatracker.rocketsleigh.RocketSleighActivity.java

private void processFrame() {
    long newTime = System.currentTimeMillis();
    long time = newTime - mLastTime;

    boolean end = false;

    if (time > 60) {
        Log.e("LONG", "Frame time took too long! Time: " + time + " Last process frame: " + mLastFrameTime
                + " Count: " + mBackgroundCount + " Level: " + mLevel);
    }//from  ww  w  .  j av a  2 s.  co  m

    // We don't want to jump too far so, if real time is > 60 treat it as 33.  On screen will seem to slow
    // down instaead of "jump"
    if (time > 60) {
        time = 33;
    }

    // Score is based on time + presents.  Right now 100 point per second played.  No presents yet
    if (mLevel < 6) {
        mScore += time;
    }

    if (mIsTv) {
        mScoreText.setText(mScoreLabel + ": " + NumberFormat.getNumberInstance().format((mScore / 10)));
    } else {
        mScoreText.setText(NumberFormat.getNumberInstance().format((mScore / 10)));
    }

    float scroll = mElfVelX * time;

    // Do collision detection first...
    // The elf can't collide if it is within 2 seconds of colliding previously.
    if (mElfIsHit) {
        if ((newTime - mElfHitTime) > 2000) {
            // Move to next state.
            if (mElfState < 4) {
                mElfState++;
                AnalyticsManager.sendEvent(getString(R.string.analytics_screen_rocket),
                        getString(R.string.analytics_action_rocket_hit), null, mElfState);
                if (mElfState == 4) {
                    mSoundPool.play(mGameOverSound, 1.0f, 1.0f, 2, 0, 1.0f);
                    // No more control...
                    mControlView.setOnTouchListener(null);
                    mElfAccelY = 0.0f;
                    if (mJetThrustStream != 0) {
                        mSoundPool.stop(mJetThrustStream);
                    }
                }
            }
            updateElf(false);
            mElfIsHit = false;
        }
    } else if (mElfState == 4) {
        // Don't do any collision detection for parachute elf.  Just let him fall...
    } else {
        // Find the obstacle(s) we might be colliding with.  It can only be one of the first 3 obstacles.
        for (int i = 0; i < 3; i++) {
            View view = mObstacleLayout.getChildAt(i);
            if (view == null) {
                // No more obstacles...
                break;
            }

            int[] tmp = new int[2];
            view.getLocationOnScreen(tmp);

            // If the start of this view is past the center of the elf, we are done
            if (tmp[0] > mElfPosX) {
                break;
            }

            if (RelativeLayout.class.isInstance(view)) {
                // this is an obstacle layout.
                View topView = view.findViewById(R.id.top_view);
                View bottomView = view.findViewById(R.id.bottom_view);
                if ((topView != null) && topView.getVisibility() == View.VISIBLE) {
                    topView.getLocationOnScreen(tmp);
                    Rect obsRect = new Rect(tmp[0], tmp[1], tmp[0] + topView.getWidth(),
                            tmp[1] + topView.getHeight());
                    if (obsRect.contains((int) mElfPosX, (int) mElfPosY + mElfBitmap.getHeight() / 2)) {
                        handleCollision();
                    }
                }
                if (!mElfIsHit) {
                    if ((bottomView != null) && bottomView.getVisibility() == View.VISIBLE) {
                        bottomView.getLocationOnScreen(tmp);
                        Rect obsRect = new Rect(tmp[0], tmp[1], tmp[0] + bottomView.getWidth(),
                                tmp[1] + bottomView.getHeight());
                        if (obsRect.contains((int) mElfPosX, (int) mElfPosY + mElfBitmap.getHeight() / 2)) {
                            // Special case for the mammoth obstacle...
                            if (bottomView.getTag() != null) {
                                if (((mElfPosX - tmp[0]) / (float) bottomView.getWidth()) > 0.25f) {
                                    // We are over the mammoth not the spike.  lower the top of the rect and test again.
                                    obsRect.top = (int) (tmp[1] + ((float) bottomView.getHeight() * 0.18f));
                                    if (obsRect.contains((int) mElfPosX,
                                            (int) mElfPosY + mElfBitmap.getHeight() / 2)) {
                                        handleCollision();
                                    }
                                }
                            } else {
                                handleCollision();
                            }
                        }
                    }
                }
            } else if (FrameLayout.class.isInstance(view)) {
                // Present view
                FrameLayout frame = (FrameLayout) view;
                if (frame.getChildCount() > 0) {
                    ImageView presentView = (ImageView) frame.getChildAt(0);
                    presentView.getLocationOnScreen(tmp);
                    Rect presentRect = new Rect(tmp[0], tmp[1], tmp[0] + presentView.getWidth(),
                            tmp[1] + presentView.getHeight());
                    mElfLayout.getLocationOnScreen(tmp);
                    Rect elfRect = new Rect(tmp[0], tmp[1], tmp[0] + mElfLayout.getWidth(),
                            tmp[1] + mElfLayout.getHeight());
                    if (elfRect.intersect(presentRect)) {
                        // We got a present!
                        mPresentCount++;
                        if (mPresentCount < 4) {
                            mSoundPool.play(mScoreSmallSound, 1.0f, 1.0f, 2, 0, 1.0f);
                            mScore += 1000; // 100 points.  Score is 10x displayed score.
                            mPlus100.setVisibility(View.VISIBLE);
                            if (mElfPosY > (mScreenHeight / 2)) {
                                mPlus100.setY(mElfPosY - (mElfLayout.getHeight() + mPlus100.getHeight()));
                            } else {
                                mPlus100.setY(mElfPosY + mElfLayout.getHeight());
                            }
                            mPlus100.setX(mElfPosX);
                            if (m100Anim.hasStarted()) {
                                m100Anim.reset();
                            }
                            mPlus100.startAnimation(m100Anim);
                        } else {
                            mSoundPool.play(mScoreBigSound, 1.0f, 1.0f, 2, 0, 1.0f);
                            mScore += 5000; // 500 points.  Score is 10x displayed score.
                            if (!mRainingPresents) {
                                mPresentCount = 0;
                            }
                            mPlus500.setVisibility(View.VISIBLE);
                            if (mElfPosY > (mScreenHeight / 2)) {
                                mPlus500.setY(mElfPosY - (mElfLayout.getHeight() + mPlus100.getHeight()));
                            } else {
                                mPlus500.setY(mElfPosY + mElfLayout.getHeight());
                            }
                            mPlus500.setX(mElfPosX);
                            if (m500Anim.hasStarted()) {
                                m500Anim.reset();
                            }
                            mPlus500.startAnimation(m500Anim);
                            mPresentBonus = true;
                        }
                        frame.removeView(presentView);
                    } else if (elfRect.left > presentRect.right) {
                        mPresentCount = 0;
                    }
                }
            }
        }
    }

    if (mForegroundLayout.getChildCount() > 0) {
        int currentX = mForegroundScroll.getScrollX();
        View view = mForegroundLayout.getChildAt(0);
        int newX = currentX + (int) scroll;
        if (newX > view.getWidth()) {
            newX -= view.getWidth();
            mForegroundLayout.removeViewAt(0);
        }
        mForegroundScroll.setScrollX(newX);
    }

    // Scroll obstacle views
    if (mObstacleLayout.getChildCount() > 0) {
        int currentX = mObstacleScroll.getScrollX();
        View view = mObstacleLayout.getChildAt(0);
        int newX = currentX + (int) scroll;
        if (newX > view.getWidth()) {
            newX -= view.getWidth();
            mObstacleLayout.removeViewAt(0);
        }
        mObstacleScroll.setScrollX(newX);
    }

    // Scroll the background and foreground
    if (mBackgroundLayout.getChildCount() > 0) {
        int currentX = mBackgroundScroll.getScrollX();
        View view = mBackgroundLayout.getChildAt(0);
        int newX = currentX + (int) scroll;
        if (newX > view.getWidth()) {
            newX -= view.getWidth();
            mBackgroundLayout.removeViewAt(0);
            if (view.getTag() != null) {
                Pair<Integer, Integer> pair = (Pair<Integer, Integer>) view.getTag();
                int type = pair.first;
                int level = pair.second;
                if (type == 0) {
                    if (mBackgrounds[level] != null) {
                        mBackgrounds[level].recycle();
                        mBackgrounds[level] = null;
                    } else if (mBackgrounds2[level] != null) {
                        mBackgrounds2[level].recycle();
                        mBackgrounds2[level] = null;
                    }
                } else if (type == 1) {
                    if (mExitTransitions[level] != null) {
                        mExitTransitions[level].recycle();
                        mExitTransitions[level] = null;
                    }
                } else if (type == 2) {
                    if (mEntryTransitions[level] != null) {
                        mEntryTransitions[level].recycle();
                        mEntryTransitions[level] = null;
                    }
                }
            }
            if (mBackgroundCount == 5) {
                if (mLevel < 6) {
                    // Pre-fetch next levels backgrounds
                    // end level uses the index 1 background...
                    int level = (mLevel == 5) ? 1 : (mLevel + 1);
                    BackgroundLoadTask task = new BackgroundLoadTask(getResources(), mLevel + 1,
                            BACKGROUNDS[level], EXIT_TRANSITIONS[mLevel],
                            // Exit transitions are for the current level...
                            ENTRY_TRANSITIONS[level], mScaleX, mScaleY, mBackgrounds, mBackgrounds2,
                            mExitTransitions, mEntryTransitions, mScreenWidth, mScreenHeight);
                    task.execute();
                    addNextImages(mLevel, true);
                    addNextObstacles(mLevel, 2);
                }
                // Fetch first set of obstacles if the next level changes from woods to cave or cave to factory
                if (mLevel == 1) {
                    // Next level will be caves.  Get bitmaps for the first 20 obstacles.
                    ObstacleLoadTask task = new ObstacleLoadTask(getResources(), CAVE_OBSTACLES, mCaveObstacles,
                            mCaveObstacleList, 0, 2, mScaleX, mScaleY);
                    task.execute();
                } else if (mLevel == 3) {
                    // Next level will be factory.  Get bitmaps for the first 20 obstacles.
                    ObstacleLoadTask task = new ObstacleLoadTask(getResources(), FACTORY_OBSTACLES,
                            mFactoryObstacles, mFactoryObstacleList, 0, 2, mScaleX, mScaleY);
                    task.execute();
                }
                mBackgroundCount++;
            } else if (mBackgroundCount == 7) {
                // Add transitions and/or next level
                if (mLevel < 5) {
                    addNextTransitionImages(mLevel + 1);
                    if (mTransitionImagesCount > 0) {
                        addNextObstacleSpacer(mTransitionImagesCount);
                    }
                    addNextImages(mLevel + 1);
                    // First screen of each new level has no obstacles
                    if ((mLevel % 2) == 1) {
                        addNextObstacleSpacer(1);
                        addNextObstacles(mLevel + 1, 1);
                    } else {
                        addNextObstacles(mLevel + 1, 2);
                    }
                } else if (mLevel == 5) {
                    addNextTransitionImages(mLevel + 1);
                    if (mTransitionImagesCount > 0) {
                        addNextObstacleSpacer(mTransitionImagesCount);
                    }
                    addFinalImages();
                }
                mBackgroundCount++;
            } else if (mBackgroundCount == 9) {
                // Either the transition or the next level is showing
                if (this.mTransitionImagesCount > 0) {
                    mTransitionImagesCount--;
                } else {
                    if (mLevel == 1) {
                        // Destroy the wood obstacle bitmaps
                        Thread thread = new Thread(new Runnable() {
                            @Override
                            public void run() {
                                synchronized (mWoodObstacles) {
                                    for (Bitmap bmp : mWoodObstacles.values()) {
                                        bmp.recycle();
                                    }
                                    mWoodObstacles.clear();
                                }
                            }
                        });
                        thread.start();
                    } else if (mLevel == 3) {
                        // Destroy the cave obstacle bitmaps
                        Thread thread = new Thread(new Runnable() {
                            @Override
                            public void run() {
                                synchronized (mCaveObstacles) {
                                    for (Bitmap bmp : mCaveObstacles.values()) {
                                        bmp.recycle();
                                    }
                                    mCaveObstacles.clear();
                                }
                            }
                        });
                        thread.start();
                    } else if (mLevel == 5) {
                        // Destroy the factory obstacle bitmaps
                        Thread thread = new Thread(new Runnable() {
                            @Override
                            public void run() {
                                synchronized (mFactoryObstacles) {
                                    for (Bitmap bmp : mFactoryObstacles.values()) {
                                        bmp.recycle();
                                    }
                                    mFactoryObstacles.clear();
                                }
                            }
                        });
                        thread.start();
                    }
                    mLevel++;

                    // Add an event for clearing this level - note we don't increment mLevel as
                    // it's 0-based and we're tracking the previous level.
                    AnalyticsManager.sendEvent(getString(R.string.analytics_screen_rocket),
                            getString(R.string.analytics_action_rocket_level), null, mLevel);

                    // Achievements
                    if (!mHitLevel) {
                        mCleanLevel = true;
                    }
                    mHitLevel = false;
                    if (mLevel == 5) {
                        mPlus100.setSelected(true);
                        mPlus500.setSelected(true);
                    } else if (mLevel == 6) {
                        mPlus100.setSelected(false);
                        mPlus500.setSelected(false);
                    }
                    if (mLevel < 6) {
                        mSoundPool.play(mLevelUpSound, 1.0f, 1.0f, 2, 0, 1.0f);
                        addNextImages(mLevel);
                        addNextObstacles(mLevel, 2);
                    }
                    mBackgroundCount = 0;
                }
            } else {
                if ((mBackgroundCount % 2) == 1) {
                    if (mLevel < 6) {
                        addNextImages(mLevel);
                        addNextObstacles(mLevel, 2);
                    }
                }
                mBackgroundCount++;
            }
        }
        int current = mBackgroundScroll.getScrollX();
        mBackgroundScroll.setScrollX(newX);
        if ((mLevel == 6) && (mBackgroundScroll.getScrollX() == current)) {
            end = true;
        }
    }

    // Check on the elf
    boolean hitBottom = false;
    boolean hitTop = false;

    float deltaY = mElfVelY * time;
    mElfPosY = mElfLayout.getY() + deltaY;
    if (mElfPosY < 0.0f) {
        mElfPosY = 0.0f;
        mElfVelY = 0.0f;
        hitTop = true;
    } else if (mElfPosY > (mScreenHeight - mElfLayout.getHeight())) {
        mElfPosY = mScreenHeight - mElfLayout.getHeight();
        mElfVelY = 0.0f;
        hitBottom = true;
    } else {
        // Remember -Y is up!
        mElfVelY += (mGravityAccelY * time - mElfAccelY * time);
    }
    mElfLayout.setY(mElfPosY);

    // Rotate the elf to indicate thrust, dive.
    float rot = (float) (Math.atan(mElfVelY / mElfVelX) * 120.0 / Math.PI);
    mElfLayout.setRotation(rot);

    mElf.invalidate();

    // Update the time and spawn the next call to processFrame.
    mLastTime = newTime;
    mLastFrameTime = System.currentTimeMillis() - newTime;
    if (!end) {
        if ((mElfState < 4) || !hitBottom) {
            if (mLastFrameTime < 16) {
                mHandler.postDelayed(mGameLoop, 16 - mLastFrameTime);
            } else {
                mHandler.post(mGameLoop);
            }
        } else {
            endGame();
        }
    } else {
        // Whatever the final stuff is, do it here.
        mPlayPauseButton.setEnabled(false);
        mPlayPauseButton.setVisibility(View.INVISIBLE);
        endGame();
    }
}