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(float left, float top, float right, float bottom, @NonNull Paint paint) 

Source Link

Document

Draw the specified Rect using the specified paint.

Usage

From source file:com.richtodd.android.quiltdesign.block.Theme.java

private void draw(Canvas canvas, int width, int height) {
    canvas.drawColor(Color.WHITE);

    Paint swatchPaint = new Paint();
    swatchPaint.setStyle(Style.FILL);

    int idx = 0;/* ww  w  .  j a va  2 s  .  c o m*/
    int left = 0;
    int right = 0;
    for (Swatch swatch : m_swatches) {
        idx += 1;
        left = right;
        right = width * idx / m_swatches.size();

        swatchPaint.setColor(swatch.getColor());
        canvas.drawRect(left, 0, right, height, swatchPaint);
    }
}

From source file:com.journeyapps.barcodescanner.WXViewfinderView.java

@SuppressLint("DrawAllocation")
@Override//  w w  w  .ja  v a  2  s.  c  o  m
public void onDraw(Canvas canvas) {
    refreshSizes();
    if (framingRect == null || previewFramingRect == null) {
        return;
    }

    Rect frame = framingRect;
    Rect previewFrame = previewFramingRect;

    int width = canvas.getWidth();
    int height = canvas.getHeight();

    maskPaint.setColor(maskColor);
    canvas.drawRect(0, 0, width, frame.top, maskPaint);
    canvas.drawRect(0, frame.top, frame.left, frame.bottom + 1, maskPaint);
    canvas.drawRect(frame.right + 1, frame.top, width, frame.bottom + 1, maskPaint);
    canvas.drawRect(0, frame.bottom + 1, width, height, maskPaint);
    //drawable the border
    canvas.drawRect(frame.left + 1, frame.top + 1, frame.right, frame.bottom, borderPaint);
    int halfWidth = (int) (cornerWidth / 2);
    //draw four corner
    Path corner1 = new Path();
    corner1.moveTo(frame.left, frame.top + cornerLength);
    corner1.lineTo(frame.left, frame.top);
    corner1.lineTo(frame.left + cornerLength, frame.top);
    Matrix translate1 = new Matrix();
    translate1.setTranslate(halfWidth, halfWidth);
    corner1.transform(translate1);
    canvas.drawPath(corner1, cornerPaint);

    Path corner2 = new Path();
    corner2.moveTo(frame.right + 1 - cornerLength, frame.top);
    corner2.lineTo(frame.right + 1, frame.top);
    corner2.lineTo(frame.right + 1, frame.top + cornerLength);
    Matrix translate2 = new Matrix();
    translate2.setTranslate(-halfWidth, halfWidth);
    corner2.transform(translate2);
    canvas.drawPath(corner2, cornerPaint);

    Path corner3 = new Path();
    corner3.moveTo(frame.left, frame.bottom + 1 - cornerLength);
    corner3.lineTo(frame.left, frame.bottom + 1);
    corner3.lineTo(frame.left + cornerLength, frame.bottom + 1);
    Matrix translate3 = new Matrix();
    translate3.setTranslate(halfWidth, -halfWidth);
    corner3.transform(translate3);
    canvas.drawPath(corner3, cornerPaint);

    Path corner4 = new Path();
    corner4.moveTo(frame.right + 1 - cornerLength, frame.bottom + 1);
    corner4.lineTo(frame.right + 1, frame.bottom + 1);
    corner4.lineTo(frame.right + 1, frame.bottom + 1 - cornerLength);
    Matrix translate4 = new Matrix();
    translate4.setTranslate(-halfWidth, -halfWidth);
    corner4.transform(translate4);
    canvas.drawPath(corner4, cornerPaint);

    offset += speed;
    if (offset >= frame.bottom - frame.top) {
        offset = 0;
    }
    Rect rect = new Rect();
    rect.left = frame.left + 1 + laserPadding;
    rect.top = frame.top + 1 + offset;
    rect.right = frame.right - laserPadding;
    rect.bottom = frame.top + 1 + offset + 3;

    Bitmap laserBitmap = ((BitmapDrawable) ResourcesCompat.getDrawable(getResources(), R.drawable.scan_laser,
            null)).getBitmap();
    canvas.drawBitmap(laserBitmap, null, rect, linePaint);

    textPaint.setTextAlign(Paint.Align.CENTER);

    canvas.drawText(statusText, (frame.right + frame.left) / 2,
            frame.bottom + statusTextPadding + statusTextSize, textPaint);

    postInvalidateDelayed(animationDelay, frame.left, frame.top, frame.right, frame.bottom);

}

From source file:com.mn.tiger.widget.viewpager.TGPagerSlidingTabStrip.java

/**
 * /*from   w  ww.  jav  a  2s.  c  om*/
 * @param canvas
 * @param rectPaint
 */
protected void onDrawUnderLine(Canvas canvas, Paint rectPaint) {
    // draw underline
    rectPaint.setColor(underlineColor);
    canvas.drawRect(0, getHeight() - underlineHeight, tabsContainer.getWidth(), getHeight(), rectPaint);
}

From source file:com.ruialonso.multiplegridrecyclerview.decoration.GridItemDividerDecoration.java

@Override
public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
    if (parent.isAnimating())
        return;/*from ww w .j  a  v a  2 s.  c  o m*/

    final int childCount = parent.getChildCount();
    final RecyclerView.LayoutManager lm = parent.getLayoutManager();
    for (int i = 0; i < childCount; i++) {
        final View child = parent.getChildAt(i);
        RecyclerView.ViewHolder viewHolder = parent.getChildViewHolder(child);

        final int right = lm.getDecoratedRight(child);
        final int bottom = lm.getDecoratedBottom(child);
        // draw the bottom divider
        canvas.drawRect(lm.getDecoratedLeft(child), bottom - dividerSize, right, bottom, paint);
        // draw the right edge divider
        canvas.drawRect(right - dividerSize, lm.getDecoratedTop(child), right, bottom - dividerSize, paint);
    }
}

From source file:com.marshalchen.common.uimodule.modifysys.PagerTabStrip.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    final int height = getHeight();
    final int bottom = height;
    final int left = mCurrText.getLeft() - mTabPadding;
    final int right = mCurrText.getRight() + mTabPadding;
    final int top = bottom - mIndicatorHeight;

    mTabPaint.setColor(mTabAlpha << 24 | (mIndicatorColor & 0xFFFFFF));
    canvas.drawRect(left, top, right, bottom, mTabPaint);

    if (mDrawFullUnderline) {
        mTabPaint.setColor(0xFF << 24 | (mIndicatorColor & 0xFFFFFF));
        canvas.drawRect(getPaddingLeft(), height - mFullUnderlineHeight, getWidth() - getPaddingRight(), height,
                mTabPaint);//from w  ww .ja v  a 2  s  .c  o  m
    }
}

From source file:com.cyou.cma.clockscreen.widget.TabPageIndicator.java

@Override
protected void dispatchDraw(Canvas canvas) {
    super.dispatchDraw(canvas);
    // if(mViewPager==null) return;
    int height = getHeight();
    final int count = mViewPager.getAdapter().getCount();
    if (mSelectedTabIndex2 == count - 1) {
        if (drawunder) {
            canvas.drawRect(mTabLayout.getChildAt(mSelectedTabIndex2).getLeft(), height - 6 * mDensity,
                    mTabLayout.getChildAt(mSelectedTabIndex2).getRight(), height, paint);
        }/*from   w  w w.  j  a va  2s  .com*/
        return;
    }
    int left = mTabLayout.getChildAt(mSelectedTabIndex2).getLeft();
    float newLeft = left + mOffset * mTabLayout.getChildAt(mSelectedTabIndex2).getWidth();
    int right = mTabLayout.getChildAt(mSelectedTabIndex2).getRight();

    float newRight = right + mOffset * mTabLayout.getChildAt(mSelectedTabIndex2 + 1).getWidth();
    mRectF.set(newLeft, height - 6 * mDensity, newRight, height);
    if (drawunder)
        canvas.drawRect(mRectF, paint);
}

From source file:com.sinyuk.jianyimaterial.ui.GridItemDividerDecoration.java

@Override
public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
    final int childCount = parent.getChildCount();
    final RecyclerView.LayoutManager lm = parent.getLayoutManager();
    for (int i = 0; i < childCount; i++) {
        final View child = parent.getChildAt(i);
        RecyclerView.ViewHolder viewHolder = parent.getChildViewHolder(child);

        if (requiresDivider(viewHolder)) {
            final int right = lm.getDecoratedRight(child);
            final int bottom = lm.getDecoratedBottom(child);
            // draw the bottom divider
            canvas.drawRect(lm.getDecoratedLeft(child), bottom - dividerSize, right, bottom, paint);
            // draw the right edge divider
            canvas.drawRect(right - dividerSize, lm.getDecoratedTop(child), right, bottom - dividerSize, paint);
        }//from  w  ww . java2  s  . c o  m

    }
}

From source file:com.geekandroid.sdk.DividerItemDecoration.java

@Override
public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
    if (parent.isAnimating())
        return;/*from  ww w  . j  av a 2  s. c  om*/

    final int childCount = parent.getChildCount();
    final RecyclerView.LayoutManager lm = parent.getLayoutManager();

    for (int i = 0; i < childCount; i++) {

        final View child = parent.getChildAt(i);
        RecyclerView.ViewHolder viewHolder = parent.getChildViewHolder(child);
        final int right = lm.getDecoratedRight(child);
        final int bottom = lm.getDecoratedBottom(child);

        // draw the bottom divider
        canvas.drawRect(lm.getDecoratedLeft(child), bottom - dividerSize, right, bottom, paint);

        if (dividerType == DividerItemDecoration.GRIDE_VIEW) {
            // draw the right edge divider
            canvas.drawRect(right - dividerSize, lm.getDecoratedTop(child), right, bottom - dividerSize, paint);
        }
    }
}

From source file:com.mn.tiger.widget.viewpager.TGPagerSlidingTabStrip.java

/**
 * /*from www .j  ava 2  s.  c  o  m*/
 * @param canvas
 */
protected void onDrawIndicator(Canvas canvas, Paint rectPaint, float lineLeft, float lineRight) {
    // draw indicator line
    rectPaint.setColor(indicatorColor);
    canvas.drawRect(lineLeft, getHeight() - indicatorHeight, lineRight, getHeight(), rectPaint);
}

From source file:com.hippo.widget.Slider.java

@Override
protected void onDraw(@NonNull Canvas canvas) {
    int width = getWidth();
    int height = getHeight();
    if (width < LayoutUtils.dp2pix(mContext, 24)) {
        canvas.drawRect(0, 0, width, getHeight(), mPaint);
    } else {//from www  .j a v a 2  s.com
        int paddingLeft = getPaddingLeft();
        int paddingTop = getPaddingTop();
        int paddingRight = getPaddingRight();
        int paddingBottom = getPaddingBottom();
        float thickness = mThickness;
        float radius = mRadius;
        float halfThickness = thickness / 2;

        int saved = canvas.save();

        canvas.translate(0, paddingTop + ((height - paddingTop - paddingBottom) / 2));

        float currentX = paddingLeft + radius + (width - radius - radius - paddingLeft - paddingRight)
                * (mReverse ? (1.0f - mDrawPercent) : mDrawPercent);

        mLeftRectF.set(paddingLeft + radius, -halfThickness, currentX, halfThickness);
        mRightRectF.set(currentX, -halfThickness, width - paddingRight - radius, halfThickness);

        // Draw bar
        if (mReverse) {
            canvas.drawRect(mRightRectF, mPaint);
            canvas.drawRect(mLeftRectF, mBgPaint);
        } else {
            canvas.drawRect(mLeftRectF, mPaint);
            canvas.drawRect(mRightRectF, mBgPaint);
        }

        // Draw controller
        float scale = 1.0f - mDrawBubbleScale;
        if (scale != 0.0f) {
            canvas.scale(scale, scale, currentX, 0);
            canvas.drawCircle(currentX, 0, radius, mPaint);
        }

        canvas.restoreToCount(saved);
    }
}