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.longle1.facedetection.MainActivity.java

@Override
protected void onDraw(Canvas canvas) {
    Paint paint = new Paint();
    paint.setColor(Color.GREEN);// www. ja v  a  2s  .c om
    paint.setTextSize(20);

    String s = "FacePreview - This side up.";
    float textWidth = paint.measureText(s);
    canvas.drawText(s, (getWidth() - textWidth) / 2, 20, paint);

    if (faces != null) {
        paint.setStrokeWidth(5);
        paint.setStyle(Paint.Style.STROKE);
        float scaleX = (float) getWidth() / grayImage.width();
        float scaleY = (float) getHeight() / grayImage.height();
        int total = faces.total();
        for (int i = 0; i < total; i++) {
            CvRect r = new CvRect(cvGetSeqElem(faces, i));
            int x = r.x(), y = r.y(), w = r.width(), h = r.height();
            canvas.drawRect(x * scaleX, y * scaleY, (x + w) * scaleX, (y + h) * scaleY, paint);
        }
    }
}

From source file:com.halloon.android.util.UnderLinePageIndicator.java

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

    if (mViewPager == null) {
        return;//from ww  w  . j  a  va2 s  . c  om
    }
    final int count = mViewPager.getAdapter().getCount();
    if (count == 0) {
        return;
    }

    if (mCurrentPage >= count) {
        setCurrentItem(count - 1);
        return;
    }

    final int paddingLeft = getPaddingLeft();
    final float pageWidth = (getWidth() - paddingLeft - getPaddingRight()) / (1f * count);
    final float exScale = pageWidth * scale;
    final float left = paddingLeft + pageWidth * (mCurrentPage + mPositionOffset) + exScale;
    final float right = left + pageWidth - (exScale * 2);
    final float bottom = getHeight() - getPaddingBottom();
    final float exTop = bottom - lineHeight;
    canvas.drawRect(left, exTop, right, bottom, mPaint);

    mPaintText.setColor(0xFFFFFFFF);
    mPaintText.setTextSize((float) (getHeight() / 2.4F));
    for (int i = 0; i < count; i++) {
        String til = (String) getTitle(i);
        mPaintText.getTextBounds(til, 0, til.length(), bounds);
        canvas.drawText(til, 0, til.length(),
                paddingLeft + (pageWidth * i) + ((pageWidth - bounds.width()) / 2),
                exTop / 2 + (bounds.height() / 2), mPaintText);
    }
}

From source file:com.viewpagerindicator.as.library.indicator.RecyclerUnderlinePageIndicator.java

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

    if (mRecyclerView == null) {
        return;// w  w  w.  j a va2  s.co m
    }
    final int count = mRecyclerView.getAdapter().getItemCount();
    if (count == 0) {
        return;
    }

    if (mCurrentPage >= count) {
        setCurrentItem(count - 1);
        return;
    }

    final int paddingLeft = getPaddingLeft();
    final float pageWidth = (getWidth() - paddingLeft - getPaddingRight()) / (1f * count);
    final float left = paddingLeft + pageWidth * (mCurrentPage + mPositionOffset);
    final float right = left + pageWidth;
    final float top = getPaddingTop();
    final float bottom = getHeight() - getPaddingBottom();
    mPaint.setColor(defaultSelectedColor);
    canvas.drawRect(left, top, right, bottom, mPaint);
}

From source file:angeloid.dreamnarae.SwipeyTabs.java

@Override
protected void dispatchDraw(Canvas canvas) {
    if (mCurrentPos != -1) {
        // calculate the relative position of the fronted tab to set the
        // alpha channel of the tab indicator
        final int tabSelectedTop = getHeight() - getPaddingBottom() - mBottomBarHeight - mTabIndicatorHeight;
        final View currentTab = getChildAt(mCurrentPos);
        final int centerOfTab = (mCurrentTabPos[mCurrentPos] + currentTab.getMeasuredWidth())
                - (currentTab.getMeasuredWidth() / 2);
        final int center = getWidth() / 2;
        final int centerDiv3 = center / 3;
        final float relativePos = 1.0f
                - Math.min(Math.abs((float) (centerOfTab - center) / (float) (centerDiv3)), 1.0f);

        mCachedTabPaint.setAlpha((int) (255 * relativePos));
        canvas.drawRect(mCurrentTabPos[mCurrentPos], tabSelectedTop,
                mCurrentTabPos[mCurrentPos] + currentTab.getMeasuredWidth(),
                tabSelectedTop + mTabIndicatorHeight, mCachedTabPaint);

        // set the correct text colors on the text views
        final int count = mAdapter.getCount();
        for (int i = 0; i < count; i++) {
            final TextView tab = (TextView) getChildAt(i);
            if (mCurrentPos == i) {
                tab.setTextColor(interpolateColor(mBottomBarColor, mTextColor, 1.0f - relativePos));
            } else {
                tab.setTextColor(mTextColor);
            }/*  ww w . j  av  a 2s.co m*/
        }

    }

    super.dispatchDraw(canvas);
}

From source file:com.miqtech.master.client.view.PagerSlidingTabStrip.java

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

    if (isInEditMode() || tabCount == 0) {
        return;/*ww  w .  ja v a  2s.c o  m*/
    }

    final int height = getHeight();

    // draw indicator line

    rectPaint.setColor(indicatorColor);

    // default: line below current tab
    View currentTab = tabsContainer.getChildAt(currentPosition);
    float lineLeft = currentTab.getLeft();
    float lineRight = currentTab.getRight();

    // if there is an offset, start interpolating left and right coordinates between current and next tab

    // draw underline

    rectPaint.setColor(underlineColor);
    canvas.drawRect(0, height - underlineHeight, tabsContainer.getWidth(), height, rectPaint);

    // draw divider
    rectPaint.setColor(indicatorColor);
    if (currentPositionOffset > 0f && currentPosition < tabCount - 1) {

        View nextTab = tabsContainer.getChildAt(currentPosition + 1);
        final float nextTabLeft = nextTab.getLeft();
        final float nextTabRight = nextTab.getRight();

        lineLeft = (currentPositionOffset * nextTabLeft + (1f - currentPositionOffset) * lineLeft);
        lineRight = (currentPositionOffset * nextTabRight + (1f - currentPositionOffset) * lineRight);
    }

    canvas.drawRect(lineLeft, height - indicatorHeight, lineRight, height, rectPaint);

    dividerPaint.setColor(dividerColor);
    for (int i = 0; i < tabCount - 1; i++) {
        View tab = tabsContainer.getChildAt(i);
        canvas.drawLine(tab.getRight(), dividerPadding, tab.getRight(), height - dividerPadding, dividerPaint);
    }
}

From source file:com.uphyca.recyclerviewcommons.DividerItemDecoration.java

@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
    super.onDraw(c, parent, state);

    final RecyclerView.LayoutManager manager = parent.getLayoutManager();

    final int left = parent.getPaddingLeft();
    final int right = parent.getWidth() - parent.getPaddingRight();

    final int childCount = parent.getChildCount();
    if (childCount < 2) {
        return;/*from   w w w  . ja  v  a  2 s  . c o  m*/
    }

    int lastItemViewType = -1;
    for (int i = 0; i < childCount; i++) {
        final View child = parent.getChildAt(i);
        final RecyclerView.ViewHolder childViewHolder = parent.getChildViewHolder(child);
        final int itemViewType = childViewHolder.getItemViewType();
        if (itemViewType == lastItemViewType) {
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            // ViewCompat.getTranslationY() needs for add/delete animation
            final int top = manager.getDecoratedTop(child) - params.topMargin
                    + Math.round(ViewCompat.getTranslationY(child));
            final int bottom = top + dividerHeight;
            c.drawRect(left + dividerMargin, top, right - dividerMargin, bottom, paint);
        }
        lastItemViewType = itemViewType;
    }
}

From source file:com.velocityviewpagerindicator.VelocityUnderlinePageIndicator.java

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

    if (mVelocityViewPager == null) {
        return;/*from w  w  w.j  a  v a  2  s .c  o m*/
    }
    final int count = mVelocityViewPager.getAdapter().getCount();
    if (count == 0) {
        return;
    }

    if (mCurrentPage >= count) {
        setCurrentItem(count - 1);
        return;
    }

    final int paddingLeft = getPaddingLeft();
    final float pageWidth = (getWidth() - paddingLeft - getPaddingRight()) / (1f * count);
    final float left = paddingLeft + pageWidth * (mCurrentPage + mPositionOffset);
    final float right = left + pageWidth;
    final float top = getPaddingTop();
    final float bottom = getHeight() - getPaddingBottom();

    Log.e("DEBUG", ">>>LEFT:" + left + " >>>RIGHT: " + right + " >>>>TOP: " + top + " >>>>BOTTOM: " + bottom);

    canvas.drawRect(left, top, right, bottom, mPaint);
}

From source file:com.chess.genesis.view.SwipeTabs.java

@Override
protected void dispatchDraw(final Canvas canvas) {
    if (mCurrentPos != -1) {
        // calculate the relative position of the fronted tab to set the
        // alpha channel of the tab indicator
        final int tabSelectedTop = getHeight() - getPaddingBottom() - mBottomBarHeight - mTabIndicatorHeight;
        final View currentTab = getChildAt(mCurrentPos);
        final int centerOfTab = (mCurrentTabPos[mCurrentPos] + currentTab.getMeasuredWidth())
                - (currentTab.getMeasuredWidth() / 2);
        final int center = getWidth() / 2;
        final int centerDiv3 = center / 3;
        final float relativePos = 1.0f
                - Math.min(Math.abs((float) (centerOfTab - center) / (float) (centerDiv3)), 1.0f);

        mCachedTabPaint.setAlpha((int) (255 * relativePos));
        canvas.drawRect(mCurrentTabPos[mCurrentPos], tabSelectedTop,
                mCurrentTabPos[mCurrentPos] + currentTab.getMeasuredWidth(),
                tabSelectedTop + mTabIndicatorHeight, mCachedTabPaint);

        // set the correct text colors on the text views
        final int count = mAdapter.getCount();
        for (int i = 0; i < count; i++) {
            final TextView tab = (TextView) getChildAt(i);
            if (mCurrentPos == i)
                tab.setTextColor(interpolateColor(mBottomBarColor, mTextColor, 1.0f - relativePos));
            else//  w ww  .  j av a  2s .co m
                tab.setTextColor(mTextColor);
        }

    }
    super.dispatchDraw(canvas);
}

From source file:com.cnh.library.materialdrawer.view.BezelImageView.java

@Override
protected void onDraw(Canvas canvas) {
    if (mBounds == null) {
        return;/*from  w w w  . j av a  2  s .  c o  m*/
    }

    int width = mBounds.width();
    int height = mBounds.height();

    if (width == 0 || height == 0) {
        return;
    }

    if (!mCacheValid || width != mCachedWidth || height != mCachedHeight || isSelected != isPressed) {
        // Need to redraw the cache
        if (width == mCachedWidth && height == mCachedHeight) {
            // Have a correct-sized bitmap cache already allocated. Just erase it.
            mCacheBitmap.eraseColor(0);
        } else {
            // Allocate a new bitmap with the correct dimensions.
            mCacheBitmap.recycle();
            //noinspection AndroidLintDrawAllocation
            mCacheBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            mCachedWidth = width;
            mCachedHeight = height;
        }

        Canvas cacheCanvas = new Canvas(mCacheBitmap);
        if (mMaskDrawable != null) {
            int sc = cacheCanvas.save();
            mMaskDrawable.draw(cacheCanvas);
            if (isSelected) {
                if (mSelectorFilter != null) {
                    mMaskedPaint.setColorFilter(mSelectorFilter);
                } else {
                    mMaskedPaint.setColorFilter(mDesaturateColorFilter);

                }
            } else {
                mMaskedPaint.setColorFilter(null);
            }
            cacheCanvas.saveLayer(mBoundsF, mMaskedPaint,
                    Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG);
            super.onDraw(cacheCanvas);
            cacheCanvas.restoreToCount(sc);
        } else if (isSelected) {
            int sc = cacheCanvas.save();
            cacheCanvas.drawRect(0, 0, mCachedWidth, mCachedHeight, mBlackPaint);
            if (mSelectorFilter != null) {
                mMaskedPaint.setColorFilter(mSelectorFilter);
            } else {
                mMaskedPaint.setColorFilter(mDesaturateColorFilter);
            }
            cacheCanvas.saveLayer(mBoundsF, mMaskedPaint,
                    Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG);
            super.onDraw(cacheCanvas);
            cacheCanvas.restoreToCount(sc);
        } else {
            super.onDraw(cacheCanvas);
        }
    }

    // Draw from cache
    canvas.drawBitmap(mCacheBitmap, mBounds.left, mBounds.top, null);

    //remember the previous press state
    isPressed = isPressed();
}

From source file:com.raghu.test.widgets.BezelImageView.java

@SuppressLint("WrongConstant")
@Override//from  ww w .j a  v a 2  s.com
protected void onDraw(Canvas canvas) {
    if (mBounds == null) {
        return;
    }

    int width = mBounds.width();
    int height = mBounds.height();

    if (width == 0 || height == 0) {
        return;
    }

    if (!mCacheValid || width != mCachedWidth || height != mCachedHeight || isSelected != isPressed) {
        // Need to redraw the cache
        if (width == mCachedWidth && height == mCachedHeight) {
            // Have a correct-sized bitmap cache already allocated. Just erase it.
            mCacheBitmap.eraseColor(0);
        } else {
            // Allocate a new bitmap with the correct dimensions.
            mCacheBitmap.recycle();
            //noinspection AndroidLintDrawAllocation
            mCacheBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            mCachedWidth = width;
            mCachedHeight = height;
        }

        Canvas cacheCanvas = new Canvas(mCacheBitmap);
        if (mMaskDrawable != null) {
            int sc = cacheCanvas.save();
            mMaskDrawable.draw(cacheCanvas);
            if (isSelected) {
                if (mSelectorFilter != null) {
                    mMaskedPaint.setColorFilter(mSelectorFilter);
                } else {
                    mMaskedPaint.setColorFilter(mDesaturateColorFilter);

                }
            } else {
                mMaskedPaint.setColorFilter(null);
            }
            cacheCanvas.saveLayer(mBoundsF, mMaskedPaint,
                    Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG);
            super.onDraw(cacheCanvas);
            cacheCanvas.restoreToCount(sc);
        } else if (isSelected) {
            int sc = cacheCanvas.save();
            cacheCanvas.drawRect(0, 0, mCachedWidth, mCachedHeight, mBlackPaint);
            if (mSelectorFilter != null) {
                mMaskedPaint.setColorFilter(mSelectorFilter);
            } else {
                mMaskedPaint.setColorFilter(mDesaturateColorFilter);
            }
            cacheCanvas.saveLayer(mBoundsF, mMaskedPaint,
                    Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG);
            super.onDraw(cacheCanvas);
            cacheCanvas.restoreToCount(sc);
        } else {
            super.onDraw(cacheCanvas);
        }
    }

    // Draw from cache
    canvas.drawBitmap(mCacheBitmap, mBounds.left, mBounds.top, null);

    //remember the previous press state
    isPressed = isPressed();
}