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.bwf.aiyiqi.ui.view.indicator.UnderlinePageIndicator.java

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

    if (mViewPager == null) {
        return;//  w ww.  j ava2s . c  o  m
    }
    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);
    float left = paddingLeft + pageWidth * (mCurrentPage + mPositionOffset);
    float right = 0;
    if (lineLength == 0) {
        right = left + pageWidth;
    } else {
        left = left + pageWidth / 2 - lineLength / 2;
        right = left + lineLength;
    }

    final float top = getPaddingTop();
    final float bottom = getHeight() - getPaddingBottom();
    canvas.drawRect(left, top, right, bottom, mPaint);
}

From source file:com.forrestguice.suntimeswidget.LightMapView.java

@SuppressWarnings("BooleanMethodIsAlwaysInverted")
private boolean drawRect(SuntimesRiseSetData data, Canvas c, Paint p) {
    Calendar riseTime = data.sunriseCalendarToday();
    Calendar setTime = data.sunsetCalendarToday();
    if (riseTime == null && setTime == null) {
        return false;
    }/*from  w  w w.j ava  2  s. c  o m*/

    int w = c.getWidth();
    int h = c.getHeight();

    int left = 0;
    if (riseTime != null) {
        double riseMinute = riseTime.get(Calendar.HOUR_OF_DAY) * 60 + riseTime.get(Calendar.MINUTE);
        double riseR = riseMinute / MINUTES_IN_DAY;
        left = (int) Math.round(riseR * w);
    }

    int right = w;
    if (setTime != null) {
        double setMinute = setTime.get(Calendar.HOUR_OF_DAY) * 60 + setTime.get(Calendar.MINUTE);
        double setR = setMinute / MINUTES_IN_DAY;
        right = (int) Math.round(setR * w);
    }

    boolean setTimeBeforeRiseTime = (riseTime != null && setTime != null
            && setTime.getTime().before(riseTime.getTime()));
    if (setTimeBeforeRiseTime) {
        c.drawRect(0, 0, right, h, p);
        c.drawRect(left, 0, w, h, p);

    } else {
        c.drawRect(left, 0, right, h, p);
    }
    return true;
}

From source file:com.e.common.widget.viewpager.UnderlinePageIndicator.java

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

    if (mViewPager == null) {
        return;//from ww  w. j av a  2s.c o m
    }
    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 left = paddingLeft + pageWidth * (mCurrentPage + mPositionOffset);
    final float right = left + pageWidth;
    final float top = getPaddingTop();
    final float bottom = getHeight() - getPaddingBottom();
    if (isRound) {
        int radius = getHeight() / 2;
        canvas.drawRoundRect(new RectF(left, top, right, bottom), radius, radius, mPaint);
    } else {
        canvas.drawRect(left, top, right, bottom, mPaint);
    }
}

From source file:com.htc.dotdesign.DrawingView.java

private Bitmap handlePicture() {
    Paint paint = new Paint();
    Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    int[][] mCloneArray = new int[mRowSize][mColSize];
    float left = 0;
    float top = 0;
    float right = sDotPixelWidth;
    float bottom = sDotPixelHeight;
    for (int row = 0; row < mRowSize; ++row) {
        for (int col = 0; col < mColSize; ++col) {
            if (mImgDotMatrix[row][col] == Color.TRANSPARENT) {
                mCloneArray[row][col] = Color.BLACK;
            } else {
                mCloneArray[row][col] = mImgDotMatrix[row][col];
            }//from   w w w  .  java2  s  .c o  m
            paint.setColor(mCloneArray[row][col]);
            canvas.drawRect(left, top, right, bottom, paint);
            left += sDotPixelWidth;
            right += sDotPixelWidth;
        }
        left = 0;
        right = sDotPixelWidth;
        top += sDotPixelHeight;
        bottom += sDotPixelHeight;
    }
    return bitmap;
}

From source file:com.android.yijiang.kzx.widget.betterpickers.widget.UnderlinePageIndicatorPicker.java

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

    final int count = mViewPager.getAdapter().getCount();

    if (isInEditMode() || count == 0) {
        return;/*from  w  w w  . jav  a 2  s .c  om*/
    }

    if (mTitleView != null) {
        View currentTab = mTitleView.getViewAt(mCurrentPage);
        float lineLeft = currentTab.getLeft();
        float lineRight = currentTab.getRight();

        // if there is an offset, start interpolating left and right
        // coordinates
        // between current and next tab
        if (mPositionOffset > 0f && mCurrentPage < count - 1) {

            View nextTab = mTitleView.getViewAt(mCurrentPage + 1);
            final float nextTabLeft = nextTab.getLeft();
            final float nextTabRight = nextTab.getRight();

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

        canvas.drawRect(lineLeft, getPaddingBottom(), lineRight, getHeight() - getPaddingBottom(), mPaint);
    }
}

From source file:com.example.anumbrella.viewpager.UnderlinePagerIndicator.java

/**
 * /*from  w ww .ja va 2 s. c o  m*/
 */
@Override
protected void onDraw(Canvas canvas) {

    super.onDraw(canvas);

    if (mViewPager == null) {
        return;
    }

    final int count = mViewPager.getAdapter().getCount();
    if (count == 0) {
        return;
    }

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

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

From source file:com.netease.hearttouch.htrefreshrecyclerview.base.HTBaseRecyclerView.java

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (mHTViewHolderTracker.isOverRefreshViewSize() && mHTViewHolder.getRefreshViewBackgroundResId() > 0) {
        int drawOffset = Math
                .abs(mHTViewHolderTracker.getCurrentPos() - mHTViewHolderTracker.getRefreshViewSize());
        mRefreshBgPaint/*  w  w w.j  av  a2 s  . c  om*/
                .setColor(ContextCompat.getColor(getContext(), mHTViewHolder.getRefreshViewBackgroundResId()));
        switch (mHTOrientation) {
        case VERTICAL_DOWN:
            canvas.drawRect(0, 0, getWidth(), drawOffset, mRefreshBgPaint);
            break;
        case HTOrientation.VERTICAL_UP:
            canvas.drawRect(0, getHeight() - drawOffset, getWidth(), getHeight(), mRefreshBgPaint);
            break;
        case HTOrientation.HORIZONTAL_RIGHT:
            canvas.drawRect(0, 0, drawOffset, getHeight(), mRefreshBgPaint);
            break;
        case HTOrientation.HORIZONTAL_LEFT:
            canvas.drawRect(getWidth() - drawOffset, 0, getWidth(), getHeight(), mRefreshBgPaint);
            break;
        }

    }
}

From source file:android.support.designox.widget.CollapsingTextHelper.java

public void draw(Canvas canvas) {
    final int saveCount = canvas.save();

    if (mTextToDraw != null && mDrawTitle) {
        float x = mCurrentDrawX;
        float y = mCurrentDrawY;

        final boolean drawTexture = mUseTexture && mExpandedTitleTexture != null;

        final float ascent;
        final float descent;
        if (drawTexture) {
            ascent = mTextureAscent * mScale;
            descent = mTextureDescent * mScale;
        } else {/*w w w.  jav a  2s. co m*/
            ascent = mTextPaint.ascent() * mScale;
            descent = mTextPaint.descent() * mScale;
        }

        if (DEBUG_DRAW) {
            // Just a debug tool, which drawn a Magneta rect in the text bounds
            canvas.drawRect(mCurrentBounds.left, y + ascent, mCurrentBounds.right, y + descent,
                    DEBUG_DRAW_PAINT);
        }

        if (drawTexture) {
            y += ascent;
        }

        if (mScale != 1f) {
            canvas.scale(mScale, mScale, x, y);
        }

        if (drawTexture) {
            // If we should use a texture, draw it instead of text
            canvas.drawBitmap(mExpandedTitleTexture, x, y, mTexturePaint);
        } else {
            canvas.drawText(mTextToDraw, 0, mTextToDraw.length(), x, y, mTextPaint);
        }
    }

    canvas.restoreToCount(saveCount);
}

From source file:com.amitupadhyay.aboutexample.util.CollapsingTextHelper.java

public void draw(Canvas canvas) {
    final int saveCount = canvas.save();
    if (mTextToDraw != null && mDrawTitle) {
        float x = mCurrentDrawX;
        float y = mCurrentDrawY;
        final boolean drawTexture = mUseTexture && mExpandedTitleTexture != null;
        final float ascent;
        final float descent;
        // Update the TextPaint to the current text size
        mTextPaint.setTextSize(mCurrentTextSize);
        if (drawTexture) {
            ascent = mTextureAscent * mScale;
            descent = mTextureDescent * mScale;
        } else {/*from   w  ww  . j  a  v a 2  s . c o  m*/
            ascent = mTextPaint.ascent() * mScale;
            descent = mTextPaint.descent() * mScale;
        }
        if (DEBUG_DRAW) {
            // Just a debug tool, which drawn a Magneta rect in the text bounds
            canvas.drawRect(mCurrentBounds.left, y + ascent, mCurrentBounds.right, y + descent,
                    DEBUG_DRAW_PAINT);
        }
        if (drawTexture) {
            y += ascent;
        }
        if (mScale != 1f) {
            canvas.scale(mScale, mScale, x, y);
        }
        if (drawTexture) {
            // If we should use a texture, draw it instead of text
            canvas.drawBitmap(mExpandedTitleTexture, x, y, mTexturePaint);
        } else {
            canvas.drawText(mTextToDraw, 0, mTextToDraw.length(), x, y, mTextPaint);
        }
    }
    canvas.restoreToCount(saveCount);
}

From source file:com.zitech.framework.widget.SlidingTabs.java

/**
 * draw the sliding tabs//from   w w w .  j  av  a 2  s .c om
 *
 * @param canvas canvas
 */
@Override
protected void onDraw(Canvas canvas) {
    // OPPO device may throw nullPointerException here!!!
    try {
        super.onDraw(canvas);
    } catch (NullPointerException e) {
        e.printStackTrace();
    }

    if (isInEditMode() || this.tabCount == 0) {
        return;
    }
    final int height = getHeight();

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

    // draw indicator line
    this.rectPaint.setColor(this.indicatorColor);

    // default: line below current tab
    View currentTab = this.tabsContainer.getChildAt(this.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
    if (this.currentPositionOffset > 0f && this.currentPosition < this.tabCount - 1) {
        View nextTab = this.tabsContainer.getChildAt(this.currentPosition + 1);
        final float nextTabLeft = nextTab.getLeft();
        final float nextTabRight = nextTab.getRight();
        lineLeft = (this.currentPositionOffset * nextTabLeft + (1f - this.currentPositionOffset) * lineLeft);
        lineRight = (this.currentPositionOffset * nextTabRight + (1f - this.currentPositionOffset) * lineRight);
    }
    if (this.indicatorDrawable != null) {
        int indicatorDrawableWidth = this.indicatorDrawable.getIntrinsicWidth();
        this.indicatorDrawable.setBounds((int) (lineLeft + this.tabWidth / 2 - indicatorDrawableWidth / 2),
                height - this.indicatorDrawable.getIntrinsicHeight(),
                (int) (lineRight - tabWidth / 2 + indicatorDrawableWidth / 2), height);
        this.indicatorDrawable.draw(canvas);
    } else {
        canvas.drawRect(lineLeft + this.mMyUnderlinePadding, height - this.indicatorHeight,
                lineRight - this.mMyUnderlinePadding, height, this.rectPaint);
    }

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