Example usage for android.graphics Canvas drawRect

List of usage examples for android.graphics Canvas drawRect

Introduction

In this page you can find the example usage for android.graphics Canvas drawRect.

Prototype

public void drawRect(@NonNull Rect r, @NonNull Paint paint) 

Source Link

Document

Draw the specified Rect using the specified Paint.

Usage

From source file:com.android.contacts.common.list.ShortcutIntentBuilder.java

/**
 * Generates a phone number shortcut icon. Adds an overlay describing the type of the phone
 * number, and if there is a photo also adds the call action icon.
 *///  ww w.j ava 2s. c  om
private Bitmap generatePhoneNumberIcon(Drawable photo, int phoneType, String phoneLabel, int actionResId) {
    final Resources r = mContext.getResources();
    final float density = r.getDisplayMetrics().density;

    Bitmap phoneIcon = ((BitmapDrawable) r.getDrawableForDensity(actionResId, mIconDensity)).getBitmap();

    Bitmap icon = generateQuickContactIcon(photo);
    Canvas canvas = new Canvas(icon);

    // Copy in the photo
    Paint photoPaint = new Paint();
    photoPaint.setDither(true);
    photoPaint.setFilterBitmap(true);
    Rect dst = new Rect(0, 0, mIconSize, mIconSize);

    // Create an overlay for the phone number type
    CharSequence overlay = Phone.getTypeLabel(r, phoneType, phoneLabel);

    if (overlay != null) {
        TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);
        textPaint.setTextSize(r.getDimension(R.dimen.shortcut_overlay_text_size));
        textPaint.setColor(r.getColor(R.color.textColorIconOverlay));
        textPaint.setShadowLayer(4f, 0, 2f, r.getColor(R.color.textColorIconOverlayShadow));

        final FontMetricsInt fmi = textPaint.getFontMetricsInt();

        // First fill in a darker background around the text to be drawn
        final Paint workPaint = new Paint();
        workPaint.setColor(mOverlayTextBackgroundColor);
        workPaint.setStyle(Paint.Style.FILL);
        final int textPadding = r.getDimensionPixelOffset(R.dimen.shortcut_overlay_text_background_padding);
        final int textBandHeight = (fmi.descent - fmi.ascent) + textPadding * 2;
        dst.set(0, mIconSize - textBandHeight, mIconSize, mIconSize);
        canvas.drawRect(dst, workPaint);

        overlay = TextUtils.ellipsize(overlay, textPaint, mIconSize, TruncateAt.END);
        final float textWidth = textPaint.measureText(overlay, 0, overlay.length());
        canvas.drawText(overlay, 0, overlay.length(), (mIconSize - textWidth) / 2,
                mIconSize - fmi.descent - textPadding, textPaint);
    }

    // Draw the phone action icon as an overlay
    Rect src = new Rect(0, 0, phoneIcon.getWidth(), phoneIcon.getHeight());
    int iconWidth = icon.getWidth();
    dst.set(iconWidth - ((int) (20 * density)), -1, iconWidth, ((int) (19 * density)));
    canvas.drawBitmap(phoneIcon, src, dst, photoPaint);

    canvas.setBitmap(null);

    return icon;
}

From source file:com.silentcircle.contacts.list.ShortcutIntentBuilder.java

/**
 * Generates a phone number shortcut icon. Adds an overlay describing the type of the phone
 * number, and if there is a photo also adds the call action icon.
 *//*from   ww  w .j a v a  2s.  c  om*/
private Bitmap generatePhoneNumberIcon(Drawable photo, int phoneType, String phoneLabel, int actionResId) {
    final Resources r = mContext.getResources();
    final float density = r.getDisplayMetrics().density;

    Bitmap phoneIcon = ((BitmapDrawable) r.getDrawableForDensity(actionResId, mIconDensity)).getBitmap();

    Bitmap icon = generateQuickContactIcon(photo);
    Canvas canvas = new Canvas(icon);

    // Copy in the photo
    Paint photoPaint = new Paint();
    photoPaint.setDither(true);
    photoPaint.setFilterBitmap(true);
    Rect dst = new Rect(0, 0, mIconSize, mIconSize);

    // Create an overlay for the phone number type
    CharSequence overlay = Phone.getTypeLabel(r, phoneType, phoneLabel);

    if (overlay != null) {
        TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);
        textPaint.setTextSize(r.getDimension(R.dimen.shortcut_overlay_text_size));
        textPaint.setColor(ContextCompat.getColor(mContext, R.color.textColorIconOverlay));
        textPaint.setShadowLayer(4f, 0, 2f,
                ContextCompat.getColor(mContext, R.color.textColorIconOverlayShadow));

        final FontMetricsInt fmi = textPaint.getFontMetricsInt();

        // First fill in a darker background around the text to be drawn
        final Paint workPaint = new Paint();
        workPaint.setColor(mOverlayTextBackgroundColor);
        workPaint.setStyle(Paint.Style.FILL);
        final int textPadding = r.getDimensionPixelOffset(R.dimen.shortcut_overlay_text_background_padding);
        final int textBandHeight = (fmi.descent - fmi.ascent) + textPadding * 2;
        dst.set(0, mIconSize - textBandHeight, mIconSize, mIconSize);
        canvas.drawRect(dst, workPaint);

        overlay = TextUtils.ellipsize(overlay, textPaint, mIconSize, TruncateAt.END);
        final float textWidth = textPaint.measureText(overlay, 0, overlay.length());
        canvas.drawText(overlay, 0, overlay.length(), (mIconSize - textWidth) / 2,
                mIconSize - fmi.descent - textPadding, textPaint);
    }

    // Draw the phone action icon as an overlay
    Rect src = new Rect(0, 0, phoneIcon.getWidth(), phoneIcon.getHeight());
    int iconWidth = icon.getWidth();
    dst.set(iconWidth - ((int) (20 * density)), -1, iconWidth, ((int) (19 * density)));
    canvas.drawBitmap(phoneIcon, src, dst, photoPaint);

    canvas.setBitmap(null);

    return icon;
}

From source file:com.hanbing.library.android.view.CustomTabLayout23.java

protected void onDrawIndicator(Canvas canvas, Paint paint, LinearLayout parent, Rect rect) {
    canvas.drawRect(rect, paint);
}

From source file:com.android.contacts.ShortcutIntentBuilder.java

/**
 * Generates a phone number shortcut icon. Adds an overlay describing the type of the phone
 * number, and if there is a photo also adds the call action icon.
 *//*from w  w w  .j  a  v a  2  s .com*/
private Bitmap generatePhoneNumberIcon(Drawable photo, int phoneType, String phoneLabel, int actionResId) {
    final Resources r = mContext.getResources();
    final float density = r.getDisplayMetrics().density;

    final Drawable phoneDrawable = r.getDrawableForDensity(actionResId, mIconDensity);
    // These icons have the same height and width so either is fine for the size.
    final Bitmap phoneIcon = BitmapUtil.drawableToBitmap(phoneDrawable, phoneDrawable.getIntrinsicHeight());

    Bitmap icon = generateQuickContactIcon(photo);
    Canvas canvas = new Canvas(icon);

    // Copy in the photo
    Paint photoPaint = new Paint();
    photoPaint.setDither(true);
    photoPaint.setFilterBitmap(true);
    Rect dst = new Rect(0, 0, mIconSize, mIconSize);

    // Create an overlay for the phone number type if we're pre-O. O created shortcuts have the
    // app badge which overlaps the type overlay.
    CharSequence overlay = Phone.getTypeLabel(r, phoneType, phoneLabel);
    if (!BuildCompat.isAtLeastO() && overlay != null) {
        TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);
        textPaint.setTextSize(r.getDimension(R.dimen.shortcut_overlay_text_size));
        textPaint.setColor(r.getColor(R.color.textColorIconOverlay));
        textPaint.setShadowLayer(4f, 0, 2f, r.getColor(R.color.textColorIconOverlayShadow));

        final FontMetricsInt fmi = textPaint.getFontMetricsInt();

        // First fill in a darker background around the text to be drawn
        final Paint workPaint = new Paint();
        workPaint.setColor(mOverlayTextBackgroundColor);
        workPaint.setStyle(Paint.Style.FILL);
        final int textPadding = r.getDimensionPixelOffset(R.dimen.shortcut_overlay_text_background_padding);
        final int textBandHeight = (fmi.descent - fmi.ascent) + textPadding * 2;
        dst.set(0, mIconSize - textBandHeight, mIconSize, mIconSize);
        canvas.drawRect(dst, workPaint);

        overlay = TextUtils.ellipsize(overlay, textPaint, mIconSize, TruncateAt.END);
        final float textWidth = textPaint.measureText(overlay, 0, overlay.length());
        canvas.drawText(overlay, 0, overlay.length(), (mIconSize - textWidth) / 2,
                mIconSize - fmi.descent - textPadding, textPaint);
    }

    // Draw the phone action icon as an overlay
    int iconWidth = icon.getWidth();
    if (BuildCompat.isAtLeastO()) {
        // On O we need to calculate where the phone icon goes slightly differently. The whole
        // canvas area is 108dp, a centered circle with a diameter of 66dp is the "safe zone".
        // So we start the drawing the phone icon at
        // 108dp - 21 dp (distance from right edge of safe zone to the edge of the canvas)
        // - 24 dp (size of the phone icon) on the x axis (left)
        // The y axis is simply 21dp for the distance to the safe zone (top).
        // See go/o-icons-eng for more details and a handy picture.
        final int left = (int) (mIconSize - (45 * density));
        final int top = (int) (21 * density);
        canvas.drawBitmap(phoneIcon, left, top, photoPaint);
    } else {
        dst.set(iconWidth - ((int) (20 * density)), -1, iconWidth, ((int) (19 * density)));
        canvas.drawBitmap(phoneIcon, null, dst, photoPaint);
    }

    canvas.setBitmap(null);
    return icon;
}

From source file:com.wanikani.androidnotifier.graph.HistogramPlot.java

protected void drawBar(Canvas canvas, Samples bar, float left, float right) {
    long base, height;
    float top, tbl;
    Paint lpaint;/*from   ww w.jav  a2  s .  co  m*/
    Paint paint;
    Path path;
    RectF rect;

    top = vp.getY(vp.yMax);
    base = 0;
    for (Sample sample : bar.samples) {
        if (sample.value > 0) {
            height = sample.value;

            if (base > vp.yMax)
                ;
            else if (base + height > vp.yMax) {
                path = new Path();
                path.moveTo(left, vp.getY(base));
                path.lineTo(left, top);
                path.lineTo(left + (right - left) / 3, top - 10);
                path.lineTo(left + (right - left) * 2 / 3, top + 5);
                path.lineTo(right, top);
                path.lineTo(right, vp.getY(base));
                path.close();
                canvas.drawPath(path, pas.series.get(sample.series));
            } else {
                rect = new RectF(left, vp.getY(base + height), right, vp.getY(base));
                rect.intersect(meas.plotArea);
                paint = pas.series.get(sample.series);
                paint.setStyle(Style.FILL);
                canvas.drawRect(rect, paint);
                paint.setStyle(Style.STROKE);
                canvas.drawRect(rect, paint);
            }
            base += height;
        }
    }

    if (base <= vp.yMax) {
        lpaint = pas.levelupPaint;
        tbl = vp.getY(base) - meas.headroom / 2;
    } else {
        lpaint = pas.levelupPaintInside;
        tbl = vp.getY(vp.yMax) + meas.margin;
    }

    if (base > 0 && drawTotal)
        canvas.drawText(Long.toString(base), (left + right) / 2, tbl, lpaint);
}

From source file:com.yanzhenjie.durban.view.OverlayView.java

/**
 * This method draws crop bounds (empty rectangle)
 * and crop guidelines (vertical and horizontal lines inside the crop bounds) if needed.
 *
 * @param canvas - valid canvas object//  www.  j a  v a2s.  co m
 */
protected void drawCropGrid(@NonNull Canvas canvas) {
    if (mShowCropGrid) {
        if (mGridPoints == null && !mCropViewRect.isEmpty()) {

            mGridPoints = new float[(mCropGridRowCount) * 4 + (mCropGridColumnCount) * 4];

            int index = 0;
            for (int i = 0; i < mCropGridRowCount; i++) {
                mGridPoints[index++] = mCropViewRect.left;
                mGridPoints[index++] = (mCropViewRect.height()
                        * (((float) i + 1.0f) / (float) (mCropGridRowCount + 1))) + mCropViewRect.top;
                mGridPoints[index++] = mCropViewRect.right;
                mGridPoints[index++] = (mCropViewRect.height()
                        * (((float) i + 1.0f) / (float) (mCropGridRowCount + 1))) + mCropViewRect.top;
            }

            for (int i = 0; i < mCropGridColumnCount; i++) {
                mGridPoints[index++] = (mCropViewRect.width()
                        * (((float) i + 1.0f) / (float) (mCropGridColumnCount + 1))) + mCropViewRect.left;
                mGridPoints[index++] = mCropViewRect.top;
                mGridPoints[index++] = (mCropViewRect.width()
                        * (((float) i + 1.0f) / (float) (mCropGridColumnCount + 1))) + mCropViewRect.left;
                mGridPoints[index++] = mCropViewRect.bottom;
            }
        }

        if (mGridPoints != null)
            canvas.drawLines(mGridPoints, mCropGridPaint);
    }

    if (mShowCropFrame)
        canvas.drawRect(mCropViewRect, mCropFramePaint);

    if (mFreestyleCropMode != FREESTYLE_CROP_MODE_DISABLE) {
        canvas.save();

        mTempRect.set(mCropViewRect);
        mTempRect.inset(mCropRectCornerTouchAreaLineLength, -mCropRectCornerTouchAreaLineLength);
        canvas.clipRect(mTempRect, Region.Op.DIFFERENCE);

        mTempRect.set(mCropViewRect);
        mTempRect.inset(-mCropRectCornerTouchAreaLineLength, mCropRectCornerTouchAreaLineLength);
        canvas.clipRect(mTempRect, Region.Op.DIFFERENCE);

        canvas.drawRect(mCropViewRect, mCropFrameCornersPaint);

        canvas.restore();
    }
}

From source file:com.hippo.recyclerview.addons.LinearDividerItemDecoration.java

@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
    RecyclerView.Adapter adapter = parent.getAdapter();
    if (adapter == null) {
        return;/*from  w  w  w.j a  v a  2 s  .  co  m*/
    }

    int itemCount = adapter.getItemCount();

    if (orientation == VERTICAL) {
        final boolean isRtl = ViewCompat.getLayoutDirection(parent) == ViewCompat.LAYOUT_DIRECTION_RTL;
        int paddingLeft;
        int paddingRight;
        if (isRtl) {
            paddingLeft = paddingEnd;
            paddingRight = paddingStart;
        } else {
            paddingLeft = paddingStart;
            paddingRight = paddingEnd;
        }

        final int left = parent.getPaddingLeft() + paddingLeft;
        final int right = parent.getWidth() - parent.getPaddingRight() - paddingRight;
        final int childCount = parent.getChildCount();

        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int position = parent.getChildLayoutPosition(child);

            boolean show;
            if (showDividerHelper != null) {
                show = showDividerHelper.showDivider(position + 1);
            } else {
                show = (position != itemCount - 1) || showLastDivider;
            }
            if (show) {
                int top = child.getBottom() + lp.bottomMargin;
                if (overlap) {
                    top -= thickness;
                }
                final int bottom = top + thickness;
                rect.set(left, top, right, bottom);
                c.drawRect(rect, paint);
            }

            if (position == 0) {
                if (showDividerHelper != null) {
                    show = showDividerHelper.showDivider(0);
                } else {
                    show = showFirstDivider;
                }
                if (show) {
                    int bottom = child.getTop() + lp.topMargin;
                    if (overlap) {
                        bottom += thickness;
                    }
                    final int top = bottom - thickness;
                    rect.set(left, top, right, bottom);
                    c.drawRect(rect, paint);
                }
            }
        }
    } else {
        final int top = parent.getPaddingTop() + paddingStart;
        final int bottom = parent.getHeight() - parent.getPaddingBottom() - paddingEnd;
        final int childCount = parent.getChildCount();

        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int position = parent.getChildLayoutPosition(child);

            boolean show;
            if (showDividerHelper != null) {
                show = showDividerHelper.showDivider(position + 1);
            } else {
                show = (position != itemCount - 1) || showLastDivider;
            }
            if (show) {
                int left = child.getRight() + lp.rightMargin;
                if (overlap) {
                    left -= thickness;
                }
                final int right = left + thickness;
                rect.set(left, top, right, bottom);
                c.drawRect(rect, paint);
            }

            if (position == 0) {
                if (showDividerHelper != null) {
                    show = showDividerHelper.showDivider(0);
                } else {
                    show = showFirstDivider;
                }
                if (show) {
                    int right = child.getLeft() + lp.leftMargin;
                    if (overlap) {
                        right += thickness;
                    }
                    final int left = right - thickness;
                    rect.set(left, top, right, bottom);
                    c.drawRect(rect, paint);
                }
            }
        }
    }
}

From source file:com.hippo.widget.recyclerview.LinearDividerItemDecoration.java

@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
    RecyclerView.Adapter adapter = parent.getAdapter();
    if (adapter == null) {
        return;//  w ww. j a va2 s  .  co  m
    }

    int itemCount = adapter.getItemCount();
    boolean overlap = mOverlap;

    if (mOrientation == VERTICAL) {
        final boolean isRtl = ViewCompat.getLayoutDirection(parent) == ViewCompat.LAYOUT_DIRECTION_RTL;
        int mPaddingLeft;
        int mPaddingRight;
        if (isRtl) {
            mPaddingLeft = mPaddingEnd;
            mPaddingRight = mPaddingStart;
        } else {
            mPaddingLeft = mPaddingStart;
            mPaddingRight = mPaddingEnd;
        }

        final int left = parent.getPaddingLeft() + mPaddingLeft;
        final int right = parent.getWidth() - parent.getPaddingRight() - mPaddingRight;
        final int childCount = parent.getChildCount();

        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int position = parent.getChildLayoutPosition(child);

            boolean show;
            if (mShowDividerHelper != null) {
                show = mShowDividerHelper.showDivider(position + 1);
            } else {
                show = position != itemCount - 1 || mShowLastDivider;
            }
            if (show) {
                int top = child.getBottom() + lp.bottomMargin;
                if (overlap) {
                    top -= mThickness;
                }
                final int bottom = top + mThickness;
                mRect.set(left, top, right, bottom);
                c.drawRect(mRect, mPaint);
            }

            if (position == 0) {
                if (mShowDividerHelper != null) {
                    show = mShowDividerHelper.showDivider(0);
                } else {
                    show = mShowFirstDivider;
                }
                if (show) {
                    int bottom = child.getTop() + lp.topMargin;
                    if (overlap) {
                        bottom += mThickness;
                    }
                    final int top = bottom - mThickness;
                    mRect.set(left, top, right, bottom);
                    c.drawRect(mRect, mPaint);
                }
            }
        }
    } else {
        final int top = parent.getPaddingTop() + mPaddingStart;
        final int bottom = parent.getHeight() - parent.getPaddingBottom() - mPaddingEnd;
        final int childCount = parent.getChildCount();

        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int position = parent.getChildLayoutPosition(child);

            boolean show;
            if (mShowDividerHelper != null) {
                show = mShowDividerHelper.showDivider(position + 1);
            } else {
                show = position != itemCount - 1 || mShowLastDivider;
            }
            if (show) {
                int left = child.getRight() + lp.rightMargin;
                if (overlap) {
                    left -= mThickness;
                }
                final int right = left + mThickness;
                mRect.set(left, top, right, bottom);
                c.drawRect(mRect, mPaint);
            }

            if (position == 0) {
                if (mShowDividerHelper != null) {
                    show = mShowDividerHelper.showDivider(0);
                } else {
                    show = mShowFirstDivider;
                }
                if (show) {
                    int right = child.getLeft() + lp.leftMargin;
                    if (overlap) {
                        right += mThickness;
                    }
                    final int left = right - mThickness;
                    mRect.set(left, top, right, bottom);
                    c.drawRect(mRect, mPaint);
                }
            }
        }
    }
}

From source file:ca.mymenuapp.ui.widgets.SlidingUpPanelLayout.java

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    final LayoutParams lp = (LayoutParams) child.getLayoutParams();
    boolean result;
    final int save = canvas.save(Canvas.CLIP_SAVE_FLAG);

    boolean drawScrim = false;

    if (mCanSlide && !lp.slideable && mSlideableView != null) {
        // Clip against the slider; no sense drawing what will immediately be covered.
        canvas.getClipBounds(mTmpRect);//from  w w  w  .  j av  a2  s  . c o m
        if (mIsSlidingUp) {
            mTmpRect.bottom = Math.min(mTmpRect.bottom, mSlideableView.getTop());
        } else {
            mTmpRect.top = Math.max(mTmpRect.top, mSlideableView.getBottom());
        }
        canvas.clipRect(mTmpRect);
        if (mSlideOffset < 1) {
            drawScrim = true;
        }
    }

    result = super.drawChild(canvas, child, drawingTime);
    canvas.restoreToCount(save);

    if (drawScrim) {
        final int baseAlpha = (mCoveredFadeColor & 0xff000000) >>> 24;
        final int imag = (int) (baseAlpha * (1 - mSlideOffset));
        final int color = imag << 24 | (mCoveredFadeColor & 0xffffff);
        mCoveredFadePaint.setColor(color);
        canvas.drawRect(mTmpRect, mCoveredFadePaint);
    }

    return result;
}