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:Main.java

public static Bitmap getRoundedCornerBitmap(Context context, Bitmap input, int pixels, int w, int h,
        boolean squareTL, boolean squareTR, boolean squareBL, boolean squareBR) {

    bitmapResult = Bitmap.createBitmap(w, h, Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmapResult);
    final float densityMultiplier = context.getResources().getDisplayMetrics().density;

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0 + 5, 0 + 5, w - 5, h - 5);
    final RectF rectF = new RectF(rect);

    // make sure that our rounded corner is scaled appropriately

    final float roundPx = pixels * densityMultiplier;

    paint.setAntiAlias(true);/* ww w.j  a  v  a2 s. c  o m*/
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    // canvas.drawColor(Color.BLACK);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    // draw rectangles over the corners we want to be square
    if (squareTL) {
        canvas.drawRect(0, 0, w / 2, h / 2, paint);
    }
    if (squareTR) {
        canvas.drawRect(w / 2, 0, w, h / 2, paint);
    }
    if (squareBL) {
        canvas.drawRect(0, h / 2, w / 2, h, paint);
    }
    if (squareBR) {
        canvas.drawRect(w / 2, h / 2, w, h, paint);
    }

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    // paint.setXfermode(new AvoidXfermode(Color.WHITE, 255,
    // AvoidXfermode.Mode.TARGET));
    canvas.drawBitmap(input, 0, 0, paint);

    return bitmapResult;
}

From source file:Main.java

private static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, float offset,
        boolean clipShadow, Paint paint) {

    int scaledWidth = width;
    int scaledHeight = height;

    final Canvas canvas = new Canvas();
    canvas.translate(offset / 2.0f, offset / 2.0f);

    Bitmap bitmap;//from   w ww . jav a  2s .  c o  m

    final Rect from = new Rect(x, y, x + width, y + height);
    final RectF to = new RectF(0, 0, width, height);

    if (m == null || m.isIdentity()) {
        bitmap = Bitmap.createBitmap(scaledWidth + (int) offset,
                scaledHeight + (int) (clipShadow ? (offset / 2.0f) : offset), Bitmap.Config.ARGB_8888);
        paint = null;
    } else {
        RectF mapped = new RectF();
        m.mapRect(mapped, to);

        scaledWidth = Math.round(mapped.width());
        scaledHeight = Math.round(mapped.height());

        bitmap = Bitmap.createBitmap(scaledWidth + (int) offset,
                scaledHeight + (int) (clipShadow ? (offset / 2.0f) : offset), Bitmap.Config.ARGB_8888);
        canvas.translate(-mapped.left, -mapped.top);
        canvas.concat(m);
    }

    canvas.setBitmap(bitmap);
    canvas.drawRect(0.0f, 0.0f, width, height, paint);
    canvas.drawBitmap(source, from, to, SCALE_PAINT);

    return bitmap;
}

From source file:com.rp.podemu.DockingLogoView.java

public void resetBitmap() {
    PodEmuLog.debug("Resetting logo");
    ImageView podemuLogo = (ImageView) ((Activity) context).findViewById(R.id.DOCK_status_icon);
    if (podemuLogo != null) {
        podemuLogo.setImageDrawable(ContextCompat.getDrawable(context, (R.drawable.podemu_icon_with_text)));
    }/*from w  ww  .  j  a  v a  2s.  c om*/

    if (mBitmap != null) {
        mPaint.setColor(Color.WHITE);
        Canvas mCanvas = new Canvas(mBitmap);
        mCanvas.drawRect(0, 0, mBitmap.getWidth(), mBitmap.getHeight(), mPaint);
        resizedBitmap = Bitmap.createScaledBitmap(mBitmap, IMAGE_SCALED_RES_X, IMAGE_SCALED_RES_Y, true);
        invalidate();
    }

    DockingLogoView dockingLogo = (DockingLogoView) findViewById(R.id.dockStationLogo);
    if (dockingLogo != null) {
        dockingLogo.setVisibility(ImageView.INVISIBLE);
    }
}

From source file:com.google.android.apps.santatracker.doodles.tilt.ColoredRectangleActor.java

@Override
public void draw(Canvas canvas) {
    canvas.drawRect(position.x, position.y, position.x + dimens.x, position.y + dimens.y, paint);
}

From source file:de.chrthms.chess.board.markers.MenaceFieldView.java

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

    Paint paint = new Paint();
    paint.setColor(ContextCompat.getColor(getContext(), R.color.basicFieldIvalidColor));

    canvas.drawRect(getLeft(), getTop(), getRight(), getBottom(), paint);

}

From source file:com.microsoft.mimickeralarm.mimics.CountDownTimerView.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    float percentage = (float) mMillisUntilFinished / (float) mTotalTime;
    canvas.drawRect(0, 0, mWidth, mHeight, m100PercentPaint);
    canvas.drawRect(0, 0, 0.75f * mWidth, mHeight, m75PercentPaint);
    canvas.drawRect(0, 0, 0.5f * mWidth, mHeight, m50PercentPaint);
    canvas.drawRect(0, 0, 0.25f * mWidth, mHeight, m25PercentPaint);
    canvas.drawRect(percentage * mWidth, 0, mWidth, mHeight, mWhitePaint);
}

From source file:com.hgdendi.contactslist.common.FloatingBarItemDecoration.java

private void drawTitleArea(Canvas c, int left, int right, View child, RecyclerView.LayoutParams params,
        int position) {
    final int rectBottom = child.getTop() - params.topMargin;
    c.drawRect(left, rectBottom - mTitleHeight, right, rectBottom, mBackgroundPaint);
    c.drawText(mList.get(position), child.getPaddingLeft() + mTextStartMargin,
            rectBottom - (mTitleHeight - mTextHeight) / 2 - mTextBaselineOffset, mTextPaint);
}

From source file:com.ruesga.rview.widget.MergedStatusChart.java

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

    // Bars//from  w ww  . j  a v  a 2 s  . c om
    canvas.drawRect(mOpenRect.left, mOpenRect.top,
            Math.min(mViewArea.width() * mAnimationDelta, mOpenRect.right), mOpenRect.bottom, mOpenPaint);
    canvas.drawRect(mMergedRect.left, mMergedRect.top,
            Math.min(mViewArea.width() * mAnimationDelta, mMergedRect.right), mMergedRect.bottom, mMergedPaint);
    canvas.drawRect(mAbandonedRect.left, mAbandonedRect.top,
            Math.min(mViewArea.width() * mAnimationDelta, mAbandonedRect.right), mAbandonedRect.bottom,
            mAbandonedPaint);

    // Number of items as text
    if (mAnimationDelta > .9f) {
        canvas.drawText(String.valueOf(mOpen), mOpenRect.left + mLabelPadding,
                mOpenRect.top + (mOpenRect.height() / 2) + (mLabelHeight / 2), mLabelPaint);
        canvas.drawText(String.valueOf(mMerged), mOpenRect.left + mLabelPadding,
                mMergedRect.top + (mMergedRect.height() / 2) + (mLabelHeight / 2), mLabelPaint);
        canvas.drawText(String.valueOf(mAbandoned), mOpenRect.left + mLabelPadding,
                mAbandonedRect.top + (mAbandonedRect.height() / 2) + (mLabelHeight / 2), mLabelPaint);
    }
}

From source file:com.hayukleung.app.view.refresh.SwipeProgressBar.java

private void drawProgress(Canvas canvas, int width, int height) {
    mPaint.setColor(mColor1);/* w  ww .  jav  a  2  s  . c om*/
    canvas.drawRect(0, 0, width * mPercentage, height, mPaint);
}

From source file:com.savvasdalkitsis.betwixt.demo.InterpolatorView.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawColor(Color.WHITE);
    int inset = (getHeight() - rectHeight()) / 2;
    canvas.drawRect(getPaddingLeft(), getPaddingTop() + inset, getWidth() - getPaddingRight(),
            getHeight() - inset - getPaddingBottom(), rectPaint);
    float x = 0;/*  w  w w.  j ava  2 s.  c om*/
    float y = 0;
    for (int i = 0; i < STEPS; i++) {
        float newX = i / (float) STEPS;
        float newY = interpolator.getInterpolation(newX);
        if (i == 0) {
            x = newX;
            y = newY;
        }
        canvas.drawLine(x(x), y(y), x(newX), y(newY), paint);
        x = newX;
        y = newY;
    }
    canvas.drawText(description, getPaddingLeft() + textPaddingLeft, getHeight() - textHeight, textPaint);
    if (animating) {
        canvas.drawRect(getPaddingLeft(), getPaddingTop(), getWidth() - getPaddingRight(),
                getHeight() - getPaddingBottom(), dim);
        int areaWidth = getWidth() - getPaddingLeft() - getPaddingRight() - animationInset * 2;
        float circleX = getPaddingLeft() + animationInset + areaWidth * progress;
        linePath.reset();
        linePath.moveTo(getPaddingLeft() + animationInset, getPaddingTop());
        linePath.lineTo(getPaddingLeft() + animationInset, getHeight() - getPaddingBottom());
        linePath.moveTo(getWidth() - getPaddingRight() - animationInset, getPaddingTop());
        linePath.lineTo(getWidth() - getPaddingRight() - animationInset, getHeight() - getPaddingBottom());
        canvas.drawPath(linePath, linePaint);
        canvas.drawCircle(circleX, getHeight() / 2, radius, circlePaint);
    }
}