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:cn.chuangblog.simplebanner1.indicator.UnderlinePageIndicator.java

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

    if (mViewPager == null) {
        return;//from  w  ww  .j a  v  a2s  .  c o m
    }
    final int count = InfiniteViewPager.FakePositionHelper.getAdapterSize(mViewPager);
    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();
    canvas.drawRect(left, top, right, bottom, mPaint);
}

From source file:com.cocosw.accessory.views.layout.CollapsingTitleLayout.java

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

    final int toolbarHeight = mToolbar.getHeight();
    canvas.clipRect(0, 0, canvas.getWidth(), interpolate(canvas.getHeight(), toolbarHeight, mScrollOffset));

    // Now call super and let it draw the background, etc
    super.draw(canvas);

    if (mTitleToDraw != null) {
        float x = mTextLeft;
        float y = mTextTop;

        if (!mUseTexture) {
            // If we're not drawing a texture, we need to properly offset the text
            x -= mDrawnTextBounds.left;/*from   w w  w.  j av a  2 s. co  m*/
            y -= mDrawnTextBounds.top;
        }

        if (DEBUG_DRAW) {
            // Just a debug tool, which drawn a Magneta rect in the text bounds
            canvas.drawRect(mTextLeft, mTextTop, mTextRight, mTextTop + mDrawnTextBounds.height(),
                    DEBUG_DRAW_PAINT);
        }

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

        if (mUseTexture && mExpandedTitleTexture != null) {
            // If we should use a texture, draw it instead of text
            canvas.drawBitmap(mExpandedTitleTexture, x, y, mTexturePaint);
        } else {
            canvas.drawText(mTitleToDraw, x, y, mTextPaint);
        }
    }

    canvas.restoreToCount(saveCount);
}

From source file:com.example.nitish.welcomapp.widgetpt.PeriodicTableView.java

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawRect(0, 0, getRight(), getBottom(), mBgPaint);
    mRect.top = (int) (mBlockSize * 1.3) + mContentRect.top + mContentOffset.y;
    mRect.left = mBlockSize * 4 + mContentRect.left + mContentOffset.x;
    mRect.bottom = mRect.top + mBlockSize * 2;
    mRect.right = mRect.left + mBlockSize * 8;
    mLegend.drawLegend(canvas, mRect);//from w  w  w.ja  va 2  s  .  co  m

    writeHeaders(canvas);
    writeTitle(canvas);

    for (PeriodicTableBlock block : mPeriodicTableBlocks) {
        findBlockPosition(block);

        if (!isBlockVisible(mRect)) {
            continue;
        }

        mBlockPaint.setColor(block.color);

        canvas.drawRect(mRect, mBlockPaint);

        canvas.drawText(block.element.symbol, mRect.left + mBlockSize / 2,
                mRect.bottom - (int) (mBlockSize / 2.8), mSymbolPaint);

        canvas.drawText(String.valueOf(block.element.number), mRect.left + mBlockSize / 20,
                mRect.top + mNumberPaint.getTextSize(), mNumberPaint);

        canvas.drawText(block.subtext, mRect.left + mBlockSize / 2, mRect.bottom - mBlockSize / 20,
                mSmallTextPaint);
    }

    if (mBlockSelected != null) {
        mSelectedPaint.setStrokeWidth(mBlockSize / 10);
        findBlockPosition(mBlockSelected);
        canvas.drawRect(mRect, mSelectedPaint);
    }

    drawEdgeEffects(canvas);
}

From source file:com.ksyun.customservice.custom.viewpagerindicator.UnderlinePageIndicator.java

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

    if (mViewPager == null) {
        return;//from  www . j  a  v a  2 s.  co m
    }
    final int count = mViewPager.getAdapter().getCount();
    if (count == 0) {
        return;
    }

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

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

From source file:com.dwdesign.tweetings.fragment.UserProfileFragment.java

public static Bitmap createAlphaGradientBitmap(Bitmap orig) {
    final int width = orig.getWidth(), height = orig.getHeight();
    final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(bitmap);
    final Paint paint = new Paint();
    final LinearGradient shader = new LinearGradient(width / 2, 0, width / 2, height, 0xffffffff, 0x00ffffff,
            Shader.TileMode.CLAMP);//from   ww w  .j av  a2s  .co  m
    paint.setShader(shader);
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
    canvas.drawBitmap(orig, 0, 0, null);
    canvas.drawRect(0, 0, width, height, paint);
    return bitmap;
}

From source file:com.ultramegatech.ey.widget.PeriodicTableView.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawRect(0, 0, getRight(), getBottom(), mBgPaint);
    mRect.top = (int) (mBlockSize * 1.3) + mContentRect.top + mContentOffset.y;
    mRect.left = mBlockSize * 3 + mContentRect.left + mContentOffset.x;
    mRect.bottom = mRect.top + mBlockSize * 2;
    mRect.right = mRect.left + mBlockSize * 9;
    mLegend.drawLegend(canvas, mRect);/*ww  w  .  j a  va 2  s.c  om*/

    writeHeaders(canvas);
    writeTitle(canvas);

    for (PeriodicTableBlock block : mPeriodicTableBlocks) {
        findBlockPosition(block);

        if (!isBlockVisible(mRect)) {
            continue;
        }

        mBlockPaint.setColor(block.color);

        canvas.drawRect(mRect, mBlockPaint);

        canvas.drawText(block.element.symbol, mRect.left + mBlockSize / 2,
                mRect.bottom - (int) (mBlockSize / 2.8), mSymbolPaint);

        canvas.drawText(String.valueOf(block.element.number), mRect.left + mBlockSize / 20,
                mRect.top + mNumberPaint.getTextSize(), mNumberPaint);

        canvas.drawText(block.subtext, mRect.left + mBlockSize / 2, mRect.bottom - mBlockSize / 20,
                mSmallTextPaint);
    }

    if (mBlockSelected != null) {
        mSelectedPaint.setStrokeWidth(mBlockSize / 10);
        findBlockPosition(mBlockSelected);
        canvas.drawRect(mRect, mSelectedPaint);
    }

    drawEdgeEffects(canvas);
}

From source file:net.droidsolutions.droidcharts.core.title.LegendTitle.java

/**
 * Draws the block within the specified area.
 * //  ww w .  j a v a 2 s .co m
 * @param g2
 *            the graphics device.
 * @param area
 *            the area.
 * @param params
 *            ignored (<code>null</code> permitted).
 * 
 * @return An {@link org.jfree.chart.block.EntityBlockResult} or
 *         <code>null</code>.
 */
public Object draw(Canvas g2, Rectangle2D area, Object params) {
    Rectangle2D target = (Rectangle2D) area.clone();
    Rectangle2D hotspot = (Rectangle2D) area.clone();
    StandardEntityCollection sec = null;
    if (params instanceof EntityBlockParams && ((EntityBlockParams) params).getGenerateEntities()) {
        sec = new StandardEntityCollection();
        sec.add(new TitleEntity(hotspot, this));
    }
    target = trimMargin(target);
    if (this.backgroundPaint != null) {
        backgroundPaint.setStyle(Paint.Style.FILL);
        // g2.setPaint(this.backgroundPaint);
        g2.drawRect((float) target.getMinX(), (float) target.getMinY(), (float) target.getMaxX(),
                (float) target.getMaxY(), backgroundPaint);
    }
    BlockFrame border = getFrame();
    border.draw(g2, target);
    border.getInsets().trim(target);
    BlockContainer container = this.wrapper;
    if (container == null) {
        container = this.items;
    }
    target = trimPadding(target);
    Object val = container.draw(g2, target, params);
    if (val instanceof BlockResult) {
        EntityCollection ec = ((BlockResult) val).getEntityCollection();
        if (ec != null && sec != null) {
            sec.addAll(ec);
            ((BlockResult) val).setEntityCollection(sec);
        }
    }
    return val;
}

From source file:com.chenl.widgets.flippablestackview.indicator.OrientedPagerSlidingTabLayout.java

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

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

    if (underlineHeight > 0) {
        final int height = getHeight();

        // draw divider

        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);
        }

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

    if (indicatorHeight > 0) {
        final int height = getHeight();
        // draw indicator line
        rectPaint.setColor(indicatorColor);
        // default: line below current tab
        View currentTab = tabsContainer.getChildAt(mCurrentPosition);
        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 (currentPositionOffset > 0f && mCurrentPosition < tabCount - 1) {

            View nextTab = tabsContainer.getChildAt(mCurrentPosition + 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);
    }
}

From source file:com.am.pagergradienttab.view.PagerGradientTabStrip.java

/**
 * /*  ww  w  . jav a2s  .c om*/
 * 
 * @param canvas
 */
private void drawIndicator(Canvas canvas) {

    canvas.save();
    canvas.translate(
            getPaddingLeft() + currectPager * (itemWidth + intervalWidth) + indicatorPadding
                    + indicatorOffset * (itemWidth + intervalWidth),
            getHeight() - underLineHeight - getPaddingBottom() - indicatorHeight);
    if (showIndicator) {
        mTextPaint.setColor(indicatorColor);
        if (tabs.size() > 1)
            canvas.drawRect(0, 0, itemWidth - 2 * indicatorPadding, indicatorHeight, mTextPaint);
        else if (tabs.size() > 0)
            canvas.drawRect(itemWidth / 4, 0, itemWidth - itemWidth / 4 - 2 * indicatorPadding, indicatorHeight,
                    mTextPaint);
        else
            canvas.drawRect(itemWidth / 4, 0, itemWidth - itemWidth / 4 - 1 * indicatorPadding, indicatorHeight,
                    mTextPaint);
    }
    canvas.restore();
}

From source file:au.com.zacher.popularmovies.activity.layout.CollapsingTitleLayout.java

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

    final int toolbarHeight = this.mToolbar.getHeight();
    canvas.clipRect(0, 0, canvas.getWidth(),
            interpolate(canvas.getHeight(), toolbarHeight, this.mScrollOffset));

    // Now call super and let it draw the background, etc
    super.draw(canvas);

    if (this.mTitleToDraw != null) {
        float x = this.mTextLeft;
        float y = this.mTextTop;

        final float ascent = this.mTextPaint.ascent() * this.mScale;
        final float descent = this.mTextPaint.descent() * this.mScale;
        final float h = descent - ascent;

        if (DEBUG_DRAW) {
            // Just a debug tool, which drawn a Magneta rect in the text bounds
            canvas.drawRect(this.mTextLeft, y - h + descent, this.mTextRight, y + descent, DEBUG_DRAW_PAINT);
        }//from w  w w . jav  a  2s.c o m

        if (this.mUseTexture) {
            y = y - h + descent;
        }

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

        if (this.mUseTexture && this.mExpandedTitleTexture != null) {
            // If we should use a texture, draw it instead of text
            canvas.drawBitmap(this.mExpandedTitleTexture, x, y, this.mTexturePaint);
        } else {
            canvas.drawText(this.mTitleToDraw, x, y, this.mTextPaint);
        }
    }

    canvas.restoreToCount(saveCount);
}