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.lichkin.customview.PagerSlidingTitleIconTabStrip.java

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

    if (isInEditMode() || tabCount == 0) {
        return;/*from   www  . j av a 2 s .  c  om*/
    }

    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
    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);
    canvas.drawRect(lineLeft, 0, lineRight, indicatorHeight, rectPaint);

    // draw underline

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

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

From source file:com.mingle.discolor.PagerSlidingTabStrip.java

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

    if (isInEditMode() || tabCount == 0) {
        return;//w  ww.  ja va2  s  .  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
    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);
    // draw underline
    rectPaint.setColor(underlineColor);
    canvas.drawRect(0, height - underlineHeight, tabsContainer.getWidth(), height, rectPaint);

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

From source file:com.mugu.mp3prayer.utils.PagerSlidingTabStrip.java

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

    if (isInEditMode() || tabCount == 0) {
        return;//from   www.ja  v  a  2s .  co 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
    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 - underlineHeight, lineRight, height - underlineHeight,
            rectPaint);

    // draw underline

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

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

From source file:com.google.android.gms.samples.vision.face.facetracker.FaceGraphic.java

/**
 * Draws the face annotations for position on the supplied canvas.
 *//* w  ww .j a  va 2 s. c  o m*/
@Override
public void draw(Canvas canvas) {
    Face face = mFace;
    if (face == null) {
        return;
    }

    // Draws a circle at the position of the detected face, with the face's track id below.
    float x = translateX(face.getPosition().x + face.getWidth() / 2);
    float y = translateY(face.getPosition().y + face.getHeight() / 2);
    canvas.drawCircle(x, y, FACE_POSITION_RADIUS, mFacePositionPaint);
    canvas.drawText("id: " + mFaceId, x + ID_X_OFFSET, y + ID_Y_OFFSET, mIdPaint);
    canvas.drawText("happiness: " + String.format("%.2f", face.getIsSmilingProbability()), x - ID_X_OFFSET,
            y - ID_Y_OFFSET, mIdPaint);
    canvas.drawText("right eye: " + String.format("%.2f", face.getIsRightEyeOpenProbability()),
            x + ID_X_OFFSET * 2, y + ID_Y_OFFSET * 2, mIdPaint);
    canvas.drawText("left eye: " + String.format("%.2f", face.getIsLeftEyeOpenProbability()),
            x - ID_X_OFFSET * 2, y - ID_Y_OFFSET * 2, mIdPaint);

    try {
        Thread.sleep(1000);
    } catch (Exception e) {
        System.out.println("SLEEP EXCEPTION: " + e.getMessage());
    }

    String happiness = String.format("%.2f", face.getIsSmilingProbability());

    new RetrieveFeedTask().execute(happiness);

    // Draws a bounding box around the face.
    float xOffset = scaleX(face.getWidth() / 2.0f);
    float yOffset = scaleY(face.getHeight() / 2.0f);
    float left = x - xOffset;
    float top = y - yOffset;
    float right = x + xOffset;
    float bottom = y + yOffset;
    canvas.drawRect(left, top, right, bottom, mBoxPaint);
}

From source file:com.zzq.viewpagerindicator.TitlePageIndicator.java

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

    if (mViewPager == null) {
        return;/*  w  w  w  .  j  a  va 2  s  . com*/
    }
    final int count = mViewPager.getAdapter().getCount();
    if (count == 0) {
        return;
    }

    ArrayList<Rect> bounds = calculateAllBounds(mPaintText);

    if (mCurrentPage >= count) {
        setCurrentItem(count - 1);
        return;
    }
    final int paddingLeft = getPaddingLeft();
    final float bottom = getHeight() - getPaddingBottom();

    // 
    mPaintText.setColor(mSelectedColor);
    int textWidthCount = 0;
    int textheightCount = 0;
    for (int i = 0; i < bounds.size(); i++) {
        int textWidth = bounds.get(i).right - bounds.get(i).left;
        int textheight = bounds.get(i).bottom - bounds.get(i).top;
        textWidthCount += textWidth;
        textheightCount += textheight;
    }

    int textSpace = (getWidth() - getPaddingLeft() - getPaddingRight() - textWidthCount) / (bounds.size() + 1);
    for (int i = 0; i < bounds.size(); i++) {
        int textWidth = bounds.get(i).right - bounds.get(i).left;
        int textheight = bounds.get(i).bottom - bounds.get(i).top;

        canvas.drawText(getTitle(i).toString(), textSpace + (textSpace + textWidth) * i, textheight,
                mPaintText);
    }
    // 
    mPaint.setColor(mLineColor);
    canvas.drawRect(getPaddingLeft(), bottom - mBaseIndicatorLineHeight, getWidth() - getPaddingRight(), bottom,
            mPaint);
    // 
    int lineWidth = (getWidth() - getPaddingLeft() - getPaddingRight() - (bounds.size() * 2) * mHorizontalSpace)
            / (bounds.size());
    mPaint.setColor(mSelectedColor);
    final float left = paddingLeft + mHorizontalSpace
            + (lineWidth + mHorizontalSpace) * (mCurrentPage + mPositionOffset);
    final float right = left + (lineWidth + mHorizontalSpace);
    canvas.drawRect(left, bottom - mIndicatorLineHeight, right, bottom, mPaint);

}

From source file:com.android.launcher3.Utilities.java

/**
 * @param scale the scale to apply before drawing {@param icon} on the canvas
 *//*  w ww  .j  av a  2 s  . co  m*/
public static Bitmap createIconBitmap(Drawable icon, Context context, float scale) {
    synchronized (sCanvas) {
        final int iconBitmapSize = getIconBitmapSize();

        int width = iconBitmapSize;
        int height = iconBitmapSize;

        if (icon instanceof PaintDrawable) {
            PaintDrawable painter = (PaintDrawable) icon;
            painter.setIntrinsicWidth(width);
            painter.setIntrinsicHeight(height);
        } else if (icon instanceof BitmapDrawable) {
            // Ensure the bitmap has a density.
            BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
            Bitmap bitmap = bitmapDrawable.getBitmap();
            if (bitmap != null && bitmap.getDensity() == Bitmap.DENSITY_NONE) {
                bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
            }
        }
        int sourceWidth = icon.getIntrinsicWidth();
        int sourceHeight = icon.getIntrinsicHeight();
        if (sourceWidth > 0 && sourceHeight > 0) {
            // Scale the icon proportionally to the icon dimensions
            final float ratio = (float) sourceWidth / sourceHeight;
            if (sourceWidth > sourceHeight) {
                height = (int) (width / ratio);
            } else if (sourceHeight > sourceWidth) {
                width = (int) (height * ratio);
            }
        }

        // no intrinsic size --> use default size
        int textureWidth = iconBitmapSize;
        int textureHeight = iconBitmapSize;

        final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight, Bitmap.Config.ARGB_8888);
        final Canvas canvas = sCanvas;
        canvas.setBitmap(bitmap);

        final int left = (textureWidth - width) / 2;
        final int top = (textureHeight - height) / 2;

        @SuppressWarnings("all") // suppress dead code warning
        final boolean debug = false;
        if (debug) {
            // draw a big box for the icon for debugging
            canvas.drawColor(sColors[sColorIndex]);
            if (++sColorIndex >= sColors.length)
                sColorIndex = 0;
            Paint debugPaint = new Paint();
            debugPaint.setColor(0xffcccc00);
            canvas.drawRect(left, top, left + width, top + height, debugPaint);
        }

        sOldBounds.set(icon.getBounds());
        icon.setBounds(left, top, left + width, top + height);
        canvas.save(Canvas.MATRIX_SAVE_FLAG);
        canvas.scale(scale, scale, textureWidth / 2, textureHeight / 2);
        icon.draw(canvas);
        canvas.restore();
        icon.setBounds(sOldBounds);
        canvas.setBitmap(null);

        return bitmap;
    }
}

From source file:com.tcl.lzhang1.mymusic.ui.widget.PagerSlidingTabStrip.java

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

    if (isInEditMode() || tabCount == 0) {
        return;//from w w w.java2 s .  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();
    LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) currentTab.getLayoutParams();
    params.weight = 1;
    currentTab.setLayoutParams(params);
    // if there is an offset, start interpolating left and right coordinates
    // between current and next tab
    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);

    // draw underline

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

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

From source file:com.bwash.bwashcar.view.PagerSlidingTabStrip.java

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

    if (isInEditMode() || tabCount == 0) {
        return;//from www.j  a va 2 s .com
    }

    final int height = getHeight();

    // draw indicator line

    rectPaint.setColor(indicatorColor);

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

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

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

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

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

    // draw underline

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

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

From source file:com.geeya.wifitv.widget.PagerSlidingTabStrip.java

@SuppressLint("DrawAllocation")
@Override//from  w  ww. jav a 2s .c om
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (isInEditMode() || tabCount == 0) {
        return;
    }

    final int height = getHeight();

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

    // draw underline

    Shader mShader = new LinearGradient(0, height - underlineHeight, tabsContainer.getWidth(), height,
            new int[] { 0xfffba534, 0xffffff00, 0xfffba534 }, null, Shader.TileMode.REPEAT);
    rectPaint.setShader(mShader);
    canvas.drawRect(0, height - underlineHeight, tabsContainer.getWidth(), height, rectPaint);

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

From source file:com.ymt.demo1.customViews.widget.PagerSlidingTabStrip.java

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

    if (isInEditMode() || tabCount == 0) {
        return;/*ww  w.j  a v  a2s  .  co 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
    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);
    }

    //
    RectF rectF = new RectF(lineLeft + 28, 28, lineRight - 28, height - 28);
    canvas.drawRoundRect(rectF, 45, 45, rectPaint);

    // draw underline

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

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