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.huewu.pla.lib.internal.PLAListView.java

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

    if (DEBUG) {/*from  ww  w  .  j  a v  a 2s  .  c  o m*/
        Log.v("PLA_ListView", "onFocusChanged");
    }

    int closetChildIndex = -1;
    if (gainFocus && previouslyFocusedRect != null) {
        // previouslyFocusedRect.offset(mScrollX, mScrollY);
        previouslyFocusedRect.offset(getScrollX(), getScrollY());

        final ListAdapter adapter = mAdapter;
        // Don't cache the result of getChildCount or mFirstPosition here,
        // it could change in layoutChildren.
        if (adapter.getCount() < getChildCount() + mFirstPosition) {
            mLayoutMode = LAYOUT_NORMAL;
            layoutChildren();
        }

        // figure out which item should be selected based on previously
        // focused rect
        final 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;
            }

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

            if (distance < minDistance) {
                minDistance = distance;
                closetChildIndex = i;
            }
        }
    }

    if (closetChildIndex >= 0) {
        setSelection(closetChildIndex + mFirstPosition);
    } else {
        requestLayout();
    }
}

From source file:com.huewu.pla.lib.internal.PLA_ListView.java

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

    if (DEBUG)//w ww.  j  a va  2s . com
        Log.v("PLA_ListView", "onFocusChanged");

    int closetChildIndex = -1;
    if (gainFocus && previouslyFocusedRect != null) {
        // previouslyFocusedRect.offset(mScrollX, mScrollY);
        previouslyFocusedRect.offset(getScrollX(), getScrollY());

        final ListAdapter adapter = mAdapter;
        // Don't cache the result of getChildCount or mFirstPosition here,
        // it could change in layoutChildren.
        if (adapter.getCount() < getChildCount() + mFirstPosition) {
            mLayoutMode = LAYOUT_NORMAL;
            layoutChildren();
        }

        // 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;
            }
        }
    }

    if (closetChildIndex >= 0) {
        setSelection(closetChildIndex + mFirstPosition);
    } else {
        requestLayout();
    }
}

From source file:org.bangbang.support.v4.widget.HListView.java

/**
     * Determine the distance to the nearest edge of a view in a particular
     * direciton./*from  w  ww .j ava  2s .  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) {
        int distance = 0;
        descendant.getDrawingRect(mTempRect);
        offsetDescendantRectToMyCoords(descendant, mTempRect);
        final int listRight = getRight() - getLeft() - mListPadding.right;
        if (mTempRect.right < mListPadding.left) {
            distance = mListPadding.left - mTempRect.right;
        } else if (mTempRect.left > listRight) {
            distance = mTempRect.left - listRight;
        }
        return distance;
    }

From source file:org.bangbang.support.v4.widget.HListView.java

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

        int closetChildIndex = -1;
        if (gainFocus && previouslyFocusedRect != null) {
            previouslyFocusedRect.offset(getScrollX(), getScrollY());

            // figure out which item should be selected based on previously
            // focused rect
            Rect otherRect = mTempRect;//from   w ww . j  a  va2  s.  com
            int minDistance = Integer.MAX_VALUE;
            final int childCount = getChildCount();
            final int firstPosition = mFirstPosition;
            final ListAdapter adapter = mAdapter;

            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;
                }
            }
        }

        if (closetChildIndex >= 0) {
            setSelection(closetChildIndex + mFirstPosition);
        } else {
            requestLayout();
        }
    }

From source file:org.bangbang.support.v4.widget.HListView.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.
     *///  www. j  ava  2  s .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:com.appunite.list.HorizontalListView.java

/**
 * Determine the distance to the nearest edge of a view in a particular
 * direction./*from  ww w  .ja  v  a 2s  . 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) {
    int distance = 0;
    descendant.getDrawingRect(mTempRect);
    offsetDescendantRectToMyCoords(descendant, mTempRect);
    final int listRight = getRight() - getLeft() - mListPadding.right;
    if (mTempRect.right < mListPadding.left) {
        distance = mListPadding.left - mTempRect.right;
    } else if (mTempRect.left > listRight) {
        distance = mTempRect.left - listRight;
    }
    return distance;
}

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

/**
 * Determine the distance to the nearest edge of a view in a particular
 * direction./*from  w  w w.  ja  va2  s  . c om*/
 * 
 * @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) {
    int distance = 0;
    descendant.getDrawingRect(mTempRect);
    offsetDescendantRectToMyCoords(descendant, mTempRect);
    final int listBottom = getBottom() - getTop() - mListPadding.bottom;
    if (mTempRect.bottom < mListPadding.top) {
        distance = mListPadding.top - mTempRect.bottom;
    } else if (mTempRect.top > listBottom) {
        distance = mTempRect.top - listBottom;
    }
    return distance;
}

From source file:com.appunite.list.HorizontalListView.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) {
        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 a2s .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;
                closestChildLeft = other.getLeft();
            }
        }
    }

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

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

/**
 * Draw the View v into the given Canvas.
 *
 * @param v the view to draw//from www .j a  va2 s  . co 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 = getTextViewIcon((TextView) v);
        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:com.appunite.list.HorizontalListView.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.
 *///from   w w w. j  a  v  a 2  s .  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_LEFT) {
        if (mTempRect.left < mListPadding.left) {
            amountToScroll = mListPadding.left - mTempRect.left;
            if (positionOfNewFocus > 0) {
                amountToScroll += getArrowScrollPreviewLength();
            }
        }
    } else {
        final int listRight = getWidth() - mListPadding.right;
        if (mTempRect.right > listRight) {
            amountToScroll = mTempRect.right - listRight;
            if (positionOfNewFocus < mItemCount - 1) {
                amountToScroll += getArrowScrollPreviewLength();
            }
        }
    }
    return amountToScroll;
}