Example usage for android.view View getDrawingRect

List of usage examples for android.view View getDrawingRect

Introduction

In this page you can find the example usage for android.view View getDrawingRect.

Prototype

public void getDrawingRect(Rect outRect) 

Source Link

Document

Return the visible drawing bounds of your view.

Usage

From source file:com.awrtechnologies.carbudgetsales.hlistview.widget.HListView.java

@Override
protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
    super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);

    final ListAdapter adapter = mAdapter;
    int closetChildIndex = -1;
    int closestChildLeft = 0;
    if (adapter != null && gainFocus && previouslyFocusedRect != null) {
        previouslyFocusedRect.offset(getScrollX(), getScrollY());

        // Don't cache the result of getChildCount or mFirstPosition here,
        // it could change in layoutChildren.
        if (adapter.getCount() < getChildCount() + mFirstPosition) {
            mLayoutMode = LAYOUT_NORMAL;
            layoutChildren();// www  .  ja va 2 s. c  om
        }

        // figure out which item should be selected based on previously
        // focused rect
        Rect otherRect = mTempRect;
        int minDistance = Integer.MAX_VALUE;
        final int childCount = getChildCount();
        final int firstPosition = mFirstPosition;

        for (int i = 0; i < childCount; i++) {
            // only consider selectable views
            if (!adapter.isEnabled(firstPosition + i)) {
                continue;
            }

            View other = getChildAt(i);
            other.getDrawingRect(otherRect);
            offsetDescendantRectToMyCoords(other, otherRect);
            int distance = getDistance(previouslyFocusedRect, otherRect, direction);

            if (distance < minDistance) {
                minDistance = distance;
                closetChildIndex = i;
                closestChildLeft = other.getLeft();
            }
        }
    }

    if (closetChildIndex >= 0) {
        setSelectionFromLeft(closetChildIndex + mFirstPosition, closestChildLeft);
    } else {
        requestLayout();
    }
}

From source file:com.appunite.list.ListView.java

@Override
protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
    super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);

    final ListAdapter adapter = mAdapter;
    int closetChildIndex = -1;
    int closestChildTop = 0;
    if (adapter != null && gainFocus && previouslyFocusedRect != null) {
        final int scrollX = getScrollX();
        final int scrollY = getScrollY();
        previouslyFocusedRect.offset(scrollX, scrollY);

        // Don't cache the result of getChildCount or mFirstPosition here,
        // it could change in layoutChildren.
        if (adapter.getCount() < getChildCount() + mFirstPosition) {
            mLayoutMode = LAYOUT_NORMAL;
            layoutChildren();/*  w  w  w. j  av  a2  s  .c o  m*/
        }

        // figure out which item should be selected based on previously
        // focused rect
        Rect otherRect = mTempRect;
        int minDistance = Integer.MAX_VALUE;
        final int childCount = getChildCount();
        final int firstPosition = mFirstPosition;

        for (int i = 0; i < childCount; i++) {
            // only consider selectable views
            if (!adapter.isEnabled(firstPosition + i)) {
                continue;
            }

            View other = getChildAt(i);
            other.getDrawingRect(otherRect);
            offsetDescendantRectToMyCoords(other, otherRect);
            int distance = getDistance(previouslyFocusedRect, otherRect, direction);

            if (distance < minDistance) {
                minDistance = distance;
                closetChildIndex = i;
                closestChildTop = other.getTop();
            }
        }
    }

    if (closetChildIndex >= 0) {
        setSelectionFromTop(closetChildIndex + mFirstPosition, closestChildTop);
    } else {
        requestLayout();
    }
}

From source file:com.awrtechnologies.carbudgetsales.hlistview.widget.HListView.java

/**
 * Determine how much we need to scroll in order to get newFocus in view.
 * //from  w ww  . ja  v  a 2 s . c  o  m
 * @param direction
 *           either {@link android.view.View#FOCUS_UP} or {@link android.view.View#FOCUS_DOWN}.
 * @param newFocus
 *           The view that would take focus.
 * @param positionOfNewFocus
 *           The position of the list item containing newFocus
 * @return The amount to scroll. Note: this is always positive! Direction needs to be taken into account when actually scrolling.
 */
private int amountToScrollToNewFocus(int direction, View newFocus, int positionOfNewFocus) {
    int amountToScroll = 0;
    newFocus.getDrawingRect(mTempRect);
    offsetDescendantRectToMyCoords(newFocus, mTempRect);
    if (direction == View.FOCUS_UP) {
        if (mTempRect.left < mListPadding.left) {
            amountToScroll = mListPadding.left - mTempRect.left;
            if (positionOfNewFocus > 0) {
                amountToScroll += getArrowScrollPreviewLength();
            }
        }
    } else {
        final int listRight = getWidth() - mListPadding.right;
        if (mTempRect.bottom > listRight) {
            amountToScroll = mTempRect.right - listRight;
            if (positionOfNewFocus < mItemCount - 1) {
                amountToScroll += getArrowScrollPreviewLength();
            }
        }
    }
    return amountToScroll;
}

From source file:com.appunite.list.ListView.java

/**
 * Determine how much we need to scroll in order to get newFocus in view.
 * @param direction either {@link android.view.View#FOCUS_UP} or
 *        {@link android.view.View#FOCUS_DOWN}.
 * @param newFocus The view that would take focus.
 * @param positionOfNewFocus The position of the list item containing newFocus
 * @return The amount to scroll.  Note: this is always positive!  Direction
 *   needs to be taken into account when actually scrolling.
 *//*  w w w.j av  a  2s . c  o m*/
private int amountToScrollToNewFocus(int direction, View newFocus, int positionOfNewFocus) {
    int amountToScroll = 0;
    newFocus.getDrawingRect(mTempRect);
    offsetDescendantRectToMyCoords(newFocus, mTempRect);
    if (direction == View.FOCUS_UP) {
        if (mTempRect.top < mListPadding.top) {
            amountToScroll = mListPadding.top - mTempRect.top;
            if (positionOfNewFocus > 0) {
                amountToScroll += getArrowScrollPreviewLength();
            }
        }
    } else {
        final int listBottom = getHeight() - mListPadding.bottom;
        if (mTempRect.bottom > listBottom) {
            amountToScroll = mTempRect.bottom - listBottom;
            if (positionOfNewFocus < mItemCount - 1) {
                amountToScroll += getArrowScrollPreviewLength();
            }
        }
    }
    return amountToScroll;
}

From source file:app.umitems.greenclock.widget.sgv.StaggeredGridView.java

/**
 *
 * NOTE This method is borrowed from {@link ScrollView}.
 *
 * @return whether the descendant of this scroll view is within delta
 *  pixels of being on the screen./* ww w  .  j  a  v  a  2s  .  c  om*/
 */
private boolean isWithinDeltaOfScreen(View descendant, int delta, int height) {
    descendant.getDrawingRect(mTempRect);
    offsetDescendantRectToMyCoords(descendant, mTempRect);

    return (mTempRect.bottom + delta) >= getScrollY() && (mTempRect.top - delta) <= (getScrollY() + height);
}

From source file:com.android.launcher3.Workspace.java

/**
 * Draw the View v into the given Canvas.
 *
 * @param v the view to draw/*  w ww .  jav a2 s .  c o  m*/
 * @param destCanvas the canvas to draw on
 * @param padding the horizontal and vertical padding to use when drawing
 */
private static void drawDragView(View v, Canvas destCanvas, int padding) {
    final Rect clipRect = sTempRect;
    v.getDrawingRect(clipRect);

    boolean textVisible = false;

    destCanvas.save();
    if (v instanceof TextView) {
        Drawable d = ((TextView) v).getCompoundDrawables()[1];
        Rect bounds = getDrawableBounds(d);
        clipRect.set(0, 0, bounds.width() + padding, bounds.height() + padding);
        destCanvas.translate(padding / 2 - bounds.left, padding / 2 - bounds.top);
        d.draw(destCanvas);
    } else {
        if (v instanceof FolderIcon) {
            // For FolderIcons the text can bleed into the icon area, and so we need to
            // hide the text completely (which can't be achieved by clipping).
            if (((FolderIcon) v).getTextVisible()) {
                ((FolderIcon) v).setTextVisible(false);
                textVisible = true;
            }
        }
        destCanvas.translate(-v.getScrollX() + padding / 2, -v.getScrollY() + padding / 2);
        destCanvas.clipRect(clipRect, Op.REPLACE);
        v.draw(destCanvas);

        // Restore text visibility of FolderIcon if necessary
        if (textVisible) {
            ((FolderIcon) v).setTextVisible(true);
        }
    }
    destCanvas.restore();
}

From source file:app.umitems.greenclock.widget.sgv.StaggeredGridView.java

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);

    if (mOnSizeChangedListener != null) {
        mOnSizeChangedListener.onSizeChanged(w, h, oldw, oldh);
    }/*from  w w  w  . j  a  v  a 2s . co m*/

    // NOTE Below is borrowed from {@link ScrollView}.
    final View currentFocused = findFocus();
    if (null == currentFocused || this == currentFocused) {
        return;
    }

    // If the currently-focused view was visible on the screen when the
    // screen was at the old height, then scroll the screen to make that
    // view visible with the new screen height.
    if (isWithinDeltaOfScreen(currentFocused, 0, oldh)) {
        currentFocused.getDrawingRect(mTempRect);
        offsetDescendantRectToMyCoords(currentFocused, mTempRect);
        scrollBy(0, computeScrollDeltaToGetChildRectOnScreen(mTempRect));
    }
}

From source file:cc.flydev.launcher.Workspace.java

/**
 * Draw the View v into the given Canvas.
 *
 * @param v the view to draw/* ww w. jav  a2 s.  c  o m*/
 * @param destCanvas the canvas to draw on
 * @param padding the horizontal and vertical padding to use when drawing
 */
private void drawDragView(View v, Canvas destCanvas, int padding, boolean pruneToDrawable) {
    final Rect clipRect = mTempRect;
    v.getDrawingRect(clipRect);

    boolean textVisible = false;

    destCanvas.save();
    if (v instanceof TextView && pruneToDrawable) {
        Drawable d = ((TextView) v).getCompoundDrawables()[1];
        clipRect.set(0, 0, d.getIntrinsicWidth() + padding, d.getIntrinsicHeight() + padding);
        destCanvas.translate(padding / 2, padding / 2);
        d.draw(destCanvas);
    } else {
        if (v instanceof FolderIcon) {
            // For FolderIcons the text can bleed into the icon area, and so we need to
            // hide the text completely (which can't be achieved by clipping).
            if (((FolderIcon) v).getTextVisible()) {
                ((FolderIcon) v).setTextVisible(false);
                textVisible = true;
            }
        } else if (v instanceof BubbleTextView) {
            final BubbleTextView tv = (BubbleTextView) v;
            clipRect.bottom = tv.getExtendedPaddingTop() - (int) BubbleTextView.PADDING_V
                    + tv.getLayout().getLineTop(0);
        } else if (v instanceof TextView) {
            final TextView tv = (TextView) v;
            clipRect.bottom = tv.getExtendedPaddingTop() - tv.getCompoundDrawablePadding()
                    + tv.getLayout().getLineTop(0);
        }
        destCanvas.translate(-v.getScrollX() + padding / 2, -v.getScrollY() + padding / 2);
        destCanvas.clipRect(clipRect, Op.REPLACE);
        v.draw(destCanvas);

        // Restore text visibility of FolderIcon if necessary
        if (textVisible) {
            ((FolderIcon) v).setTextVisible(true);
        }
    }
    destCanvas.restore();
}

From source file:com.aidy.launcher3.ui.workspace.Workspace.java

/**
 * Draw the View v into the given Canvas.
 * /*from   www .  ja  v a 2 s  . c  o  m*/
 * @param v
 *            the view to draw
 * @param destCanvas
 *            the canvas to draw on
 * @param padding
 *            the horizontal and vertical padding to use when drawing
 */
private void drawDragView(View v, Canvas destCanvas, int padding, boolean pruneToDrawable) {
    final Rect clipRect = mTempRect;
    v.getDrawingRect(clipRect);

    boolean textVisible = false;

    destCanvas.save();
    if (v instanceof TextView && pruneToDrawable) {
        Drawable d = ((TextView) v).getCompoundDrawables()[1];
        clipRect.set(0, 0, d.getIntrinsicWidth() + padding, d.getIntrinsicHeight() + padding);
        destCanvas.translate(padding / 2, padding / 2);
        d.draw(destCanvas);
    } else {
        if (v instanceof FolderIcon) {
            // For FolderIcons the text can bleed into the icon area, and so
            // we need to
            // hide the text completely (which can't be achieved by
            // clipping).
            if (((FolderIcon) v).getTextVisible()) {
                ((FolderIcon) v).setTextVisible(false);
                textVisible = true;
            }
        } else if (v instanceof BubbleTextView) {
            final BubbleTextView tv = (BubbleTextView) v;
            clipRect.bottom = tv.getExtendedPaddingTop() - (int) BubbleTextView.PADDING_V
                    + tv.getLayout().getLineTop(0);
        } else if (v instanceof TextView) {
            final TextView tv = (TextView) v;
            clipRect.bottom = tv.getExtendedPaddingTop() - tv.getCompoundDrawablePadding()
                    + tv.getLayout().getLineTop(0);
        }
        destCanvas.translate(-v.getScrollX() + padding / 2, -v.getScrollY() + padding / 2);
        destCanvas.clipRect(clipRect, Op.REPLACE);
        v.draw(destCanvas);

        // Restore text visibility of FolderIcon if necessary
        if (textVisible) {
            ((FolderIcon) v).setTextVisible(true);
        }
    }
    destCanvas.restore();
}

From source file:com.aliasapps.seq.scroller.TwoWayView.java

/**
 * Determine the distance to the nearest edge of a view in a particular
 * direction.//  w w  w.j  a v  a2 s .  c  o m
 *
 * @param descendant A descendant of this list.
 * @return The distance, or 0 if the nearest edge is already on screen.
 */
private int distanceToView(View descendant) {
    descendant.getDrawingRect(mTempRect);
    offsetDescendantRectToMyCoords(descendant, mTempRect);

    final int start = (mIsVertical ? getPaddingTop() : getPaddingLeft());
    final int end = (mIsVertical ? getHeight() - getPaddingBottom() : getWidth() - getPaddingRight());

    final int viewStart = (mIsVertical ? mTempRect.top : mTempRect.left);
    final int viewEnd = (mIsVertical ? mTempRect.bottom : mTempRect.right);

    int distance = 0;
    if (viewEnd < start) {
        distance = start - viewEnd;
    } else if (viewStart > end) {
        distance = viewStart - end;
    }

    return distance;
}