Search in sources :

Example 56 with Rect

use of android.graphics.Rect in project SmartAndroidSource by jaychou2012.

the class TitlePageIndicator method calculateAllBounds.

/**
	 * Calculate views bounds and scroll them according to the current index
	 * 
	 * @param paint
	 * @return
	 */
private ArrayList<Rect> calculateAllBounds(Paint paint) {
    ArrayList<Rect> list = new ArrayList<Rect>();
    // For each views (If no values then add a fake one)
    final int count = mViewPager.getAdapter().getCount();
    final int width = getWidth();
    final int halfWidth = width / 2;
    for (int i = 0; i < count; i++) {
        Rect bounds = calcBounds(i, paint);
        int w = bounds.right - bounds.left;
        int h = bounds.bottom - bounds.top;
        bounds.left = (int) (halfWidth - (w / 2f) + ((i - mCurrentPage - mPageOffset) * width));
        bounds.right = bounds.left + w;
        bounds.top = 0;
        bounds.bottom = h;
        list.add(bounds);
    }
    return list;
}
Also used : Rect(android.graphics.Rect) ArrayList(java.util.ArrayList) Paint(android.graphics.Paint)

Example 57 with Rect

use of android.graphics.Rect in project SmartAndroidSource by jaychou2012.

the class TitlePageIndicator method onDraw.

/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View#onDraw(android.graphics.Canvas)
	 */
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (mViewPager == null) {
        return;
    }
    final int count = mViewPager.getAdapter().getCount();
    if (count == 0) {
        return;
    }
    // so, retrieve the correct index from viewpager.
    if (mCurrentPage == -1 && mViewPager != null) {
        mCurrentPage = mViewPager.getCurrentItem();
    }
    // Calculate views bounds
    ArrayList<Rect> bounds = calculateAllBounds(mPaintText);
    final int boundsSize = bounds.size();
    // Make sure we're on a page that still exists
    if (mCurrentPage >= boundsSize) {
        setCurrentItem(boundsSize - 1);
        return;
    }
    final int countMinusOne = count - 1;
    final float halfWidth = getWidth() / 2f;
    final int left = getLeft();
    final float leftClip = left + mClipPadding;
    final int width = getWidth();
    int height = getHeight();
    final int right = left + width;
    final float rightClip = right - mClipPadding;
    int page = mCurrentPage;
    float offsetPercent;
    if (mPageOffset <= 0.5) {
        offsetPercent = mPageOffset;
    } else {
        page += 1;
        offsetPercent = 1 - mPageOffset;
    }
    final boolean currentSelected = (offsetPercent <= SELECTION_FADE_PERCENTAGE);
    final boolean currentBold = (offsetPercent <= BOLD_FADE_PERCENTAGE);
    final float selectedPercent = (SELECTION_FADE_PERCENTAGE - offsetPercent) / SELECTION_FADE_PERCENTAGE;
    // Verify if the current view must be clipped to the screen
    Rect curPageBound = bounds.get(mCurrentPage);
    float curPageWidth = curPageBound.right - curPageBound.left;
    if (curPageBound.left < leftClip) {
        // Try to clip to the screen (left side)
        clipViewOnTheLeft(curPageBound, curPageWidth, left);
    }
    if (curPageBound.right > rightClip) {
        // Try to clip to the screen (right side)
        clipViewOnTheRight(curPageBound, curPageWidth, right);
    }
    // Left views starting from the current position
    if (mCurrentPage > 0) {
        for (int i = mCurrentPage - 1; i >= 0; i--) {
            Rect bound = bounds.get(i);
            // Is left side is outside the screen
            if (bound.left < leftClip) {
                int w = bound.right - bound.left;
                // Try to clip to the screen (left side)
                clipViewOnTheLeft(bound, w, left);
                // Except if there's an intersection with the right view
                Rect rightBound = bounds.get(i + 1);
                // Intersection
                if (bound.right + mTitlePadding > rightBound.left) {
                    bound.left = (int) (rightBound.left - w - mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
        }
    }
    // Right views starting from the current position
    if (mCurrentPage < countMinusOne) {
        for (int i = mCurrentPage + 1; i < count; i++) {
            Rect bound = bounds.get(i);
            // If right side is outside the screen
            if (bound.right > rightClip) {
                int w = bound.right - bound.left;
                // Try to clip to the screen (right side)
                clipViewOnTheRight(bound, w, right);
                // Except if there's an intersection with the left view
                Rect leftBound = bounds.get(i - 1);
                // Intersection
                if (bound.left - mTitlePadding < leftBound.right) {
                    bound.left = (int) (leftBound.right + mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
        }
    }
    // Now draw views
    int colorTextAlpha = mColorText >>> 24;
    for (int i = 0; i < count; i++) {
        // Get the title
        Rect bound = bounds.get(i);
        // Only if one side is visible
        if ((bound.left > left && bound.left < right) || (bound.right > left && bound.right < right)) {
            final boolean currentPage = (i == page);
            final CharSequence pageTitle = getTitle(i);
            // Only set bold if we are within bounds
            mPaintText.setFakeBoldText(currentPage && currentBold && mBoldText);
            // Draw text as unselected
            mPaintText.setColor(mColorText);
            if (currentPage && currentSelected) {
                // Fade out/in unselected text as the selected text fades
                // in/out
                mPaintText.setAlpha(colorTextAlpha - (int) (colorTextAlpha * selectedPercent));
            }
            // Except if there's an intersection with the right view
            if (i < boundsSize - 1) {
                Rect rightBound = bounds.get(i + 1);
                // Intersection
                if (bound.right + mTitlePadding > rightBound.left) {
                    int w = bound.right - bound.left;
                    bound.left = (int) (rightBound.left - w - mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
            canvas.drawText(pageTitle, 0, pageTitle.length(), bound.left, bound.bottom + mTopPadding, mPaintText);
            // If we are within the selected bounds draw the selected text
            if (currentPage && currentSelected) {
                mPaintText.setColor(mColorSelected);
                mPaintText.setAlpha((int) ((mColorSelected >>> 24) * selectedPercent));
                canvas.drawText(pageTitle, 0, pageTitle.length(), bound.left, bound.bottom + mTopPadding, mPaintText);
            }
        }
    }
    // If we want the line on the top change height to zero and invert the
    // line height to trick the drawing code
    float footerLineHeight = mFooterLineHeight;
    float footerIndicatorLineHeight = mFooterIndicatorHeight;
    if (mLinePosition == LinePosition.Top) {
        height = 0;
        footerLineHeight = -footerLineHeight;
        footerIndicatorLineHeight = -footerIndicatorLineHeight;
    }
    // Draw the footer line
    mPath.reset();
    mPath.moveTo(0, height - footerLineHeight / 2f);
    mPath.lineTo(width, height - footerLineHeight / 2f);
    mPath.close();
    canvas.drawPath(mPath, mPaintFooterLine);
    float heightMinusLine = height - footerLineHeight;
    switch(mFooterIndicatorStyle) {
        case Triangle:
            mPath.reset();
            mPath.moveTo(halfWidth, heightMinusLine - footerIndicatorLineHeight);
            mPath.lineTo(halfWidth + footerIndicatorLineHeight, heightMinusLine);
            mPath.lineTo(halfWidth - footerIndicatorLineHeight, heightMinusLine);
            mPath.close();
            canvas.drawPath(mPath, mPaintFooterIndicator);
            break;
        case Underline:
            if (!currentSelected || page >= boundsSize) {
                break;
            }
            Rect underlineBounds = bounds.get(page);
            final float rightPlusPadding = underlineBounds.right + mFooterIndicatorUnderlinePadding;
            final float leftMinusPadding = underlineBounds.left - mFooterIndicatorUnderlinePadding;
            final float heightMinusLineMinusIndicator = heightMinusLine - footerIndicatorLineHeight;
            mPath.reset();
            mPath.moveTo(leftMinusPadding, heightMinusLine);
            mPath.lineTo(rightPlusPadding, heightMinusLine);
            mPath.lineTo(rightPlusPadding, heightMinusLineMinusIndicator);
            mPath.lineTo(leftMinusPadding, heightMinusLineMinusIndicator);
            mPath.close();
            mPaintFooterIndicator.setAlpha((int) (0xFF * selectedPercent));
            canvas.drawPath(mPath, mPaintFooterIndicator);
            mPaintFooterIndicator.setAlpha(0xFF);
            break;
    }
}
Also used : Rect(android.graphics.Rect) Paint(android.graphics.Paint)

Example 58 with Rect

use of android.graphics.Rect in project SmartAndroidSource by jaychou2012.

the class PopoverView method getRectForArrowLeft.

/**
	 * Calculates the rect for showing the view with Arrow Left
	 * 
	 * @param originRect
	 *            The origin rect
	 * @return The calculated rect to show the view
	 */
private Rect getRectForArrowLeft(Rect originRect) {
    // Get available space
    int xAvailable = popoverLayoutRect.width() - (originRect.right - popoverLayoutRect.left);
    if (xAvailable < 0)
        xAvailable = 0;
    int yAvailable = popoverLayoutRect.height();
    if (yAvailable < 0)
        yAvailable = 0;
    // Get final width and height
    int finalX = xAvailable;
    if ((realContentSize.x > 0) && (realContentSize.x < finalX))
        finalX = realContentSize.x;
    int finalY = yAvailable;
    if ((realContentSize.y > 0) && (realContentSize.y < finalY))
        finalY = realContentSize.y;
    // Get final origin X and Y
    int originX = (originRect.right - popoverLayoutRect.left);
    int originY = (originRect.centerY() - popoverLayoutRect.top) - (finalY / 2);
    if (originY < 0)
        originY = 0;
    else if (originY + finalY > popoverLayoutRect.height())
        originY = popoverLayoutRect.height() - finalY;
    // Create rect
    Rect finalRect = new Rect(originX, originY, originX + finalX, originY + finalY);
    // And return
    return finalRect;
}
Also used : Rect(android.graphics.Rect) Point(android.graphics.Point)

Example 59 with Rect

use of android.graphics.Rect in project SmartAndroidSource by jaychou2012.

the class PopoverView method getBestRect.

/**
	 * Get the best available rect (bigger area)
	 * 
	 * @return The Integer key to get the Rect from posibleRects
	 *         (PopoverArrowDirectionUp
	 *         ,PopoverArrowDirectionDown,PopoverArrowDirectionRight or
	 *         PopoverArrowDirectionLeft)
	 */
private Integer getBestRect() {
    // Get the best one (bigger area)
    Integer best = null;
    for (Integer arrowDir : possibleRects.keySet()) {
        if (best == null) {
            best = arrowDir;
        } else {
            Rect bestRect = possibleRects.get(best);
            Rect checkRect = possibleRects.get(arrowDir);
            if ((bestRect.width() * bestRect.height()) < (checkRect.width() * checkRect.height()))
                best = arrowDir;
        }
    }
    return best;
}
Also used : Rect(android.graphics.Rect)

Example 60 with Rect

use of android.graphics.Rect in project k-9 by k9mail.

the class ContactPictureLoader method calculateFallbackBitmap.

/**
     * Calculates a bitmap with a color and a capital letter for contacts without picture.
     */
private Bitmap calculateFallbackBitmap(Address address) {
    Bitmap result = Bitmap.createBitmap(mPictureSizeInPx, mPictureSizeInPx, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(result);
    int rgb = calcUnknownContactColor(address);
    result.eraseColor(rgb);
    String letter = calcUnknownContactLetter(address);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setStyle(Paint.Style.FILL);
    paint.setARGB(255, 255, 255, 255);
    // just scale this down a bit
    paint.setTextSize(mPictureSizeInPx * 3 / 4);
    Rect rect = new Rect();
    paint.getTextBounds(letter, 0, 1, rect);
    float width = paint.measureText(letter);
    canvas.drawText(letter, (mPictureSizeInPx / 2f) - (width / 2f), (mPictureSizeInPx / 2f) + (rect.height() / 2f), paint);
    return result;
}
Also used : Bitmap(android.graphics.Bitmap) Rect(android.graphics.Rect) Canvas(android.graphics.Canvas) Paint(android.graphics.Paint) Paint(android.graphics.Paint)

Aggregations

Rect (android.graphics.Rect)4805 Paint (android.graphics.Paint)1052 View (android.view.View)687 Point (android.graphics.Point)563 Bitmap (android.graphics.Bitmap)467 Canvas (android.graphics.Canvas)372 RectF (android.graphics.RectF)266 Drawable (android.graphics.drawable.Drawable)241 Matrix (android.graphics.Matrix)125 ViewGroup (android.view.ViewGroup)121 ArrayList (java.util.ArrayList)121 TextView (android.widget.TextView)119 SuppressLint (android.annotation.SuppressLint)116 Resources (android.content.res.Resources)112 TextPaint (android.text.TextPaint)110 PorterDuffXfermode (android.graphics.PorterDuffXfermode)105 ImageView (android.widget.ImageView)97 BitmapDrawable (android.graphics.drawable.BitmapDrawable)95 SmallTest (android.test.suitebuilder.annotation.SmallTest)94 RemoteException (android.os.RemoteException)93