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(@NonNull Rect r, @NonNull Paint paint) 

Source Link

Document

Draw the specified Rect using the specified Paint.

Usage

From source file:Main.java

public static Bitmap getColorPreviewBitmap(final Context context, final int color, final boolean border) {
    if (context == null)
        return null;
    final float density = context.getResources().getDisplayMetrics().density;
    final int width = (int) (32 * density), height = (int) (32 * density);

    final Bitmap bm = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    final Canvas canvas = new Canvas(bm);

    final int rectrangleSize = (int) (density * 5);
    final int numRectanglesHorizontal = (int) Math.ceil(width / rectrangleSize);
    final int numRectanglesVertical = (int) Math.ceil(height / rectrangleSize);
    final Rect r = new Rect();
    boolean verticalStartWhite = true;
    for (int i = 0; i <= numRectanglesVertical; i++) {

        boolean isWhite = verticalStartWhite;
        for (int j = 0; j <= numRectanglesHorizontal; j++) {

            r.top = i * rectrangleSize;//from w w  w .  j  a v  a 2 s.  com
            r.left = j * rectrangleSize;
            r.bottom = r.top + rectrangleSize;
            r.right = r.left + rectrangleSize;
            final Paint paint = new Paint();
            paint.setColor(isWhite ? Color.WHITE : Color.GRAY);

            canvas.drawRect(r, paint);

            isWhite = !isWhite;
        }

        verticalStartWhite = !verticalStartWhite;

    }
    canvas.drawColor(color);
    if (border) {
        final Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setStrokeWidth(1f * density);
        final float[] points = new float[] { 0, 0, width, 0, 0, 0, 0, height, width, 0, width, height, 0,
                height, width, height };
        canvas.drawLines(points, paint);
    }
    return bm;
}

From source file:Main.java

public static Bitmap createRoundedBottomBitmap(Context context, int width, int height, int color) {

    Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    //color = 0x80424242;  // FIXME

    final Paint paint = new Paint();
    final int roundPxInt = convertDipsToPixels(context, ROUND_DIPS);

    final Rect rect = new Rect(0, 0, width, height - roundPxInt);
    final RectF rectF = new RectF(rect);
    final Rect rectRound = new Rect(roundPxInt, height - roundPxInt, width - roundPxInt, height);
    final RectF rectFRound = new RectF(rectRound);

    paint.setAntiAlias(true);//  w w w .ja va 2s  . c  o  m
    paint.setColor(color);

    canvas.drawARGB(0, 0, 0, 0);

    // Corners
    Rect oval = new Rect(0, height - 2 * roundPxInt, 2 * roundPxInt, height);
    RectF ovalF = new RectF(oval);
    canvas.drawArc(ovalF, 90.0f, 90.0f, true, paint);

    oval = new Rect(width - 2 * roundPxInt, height - 2 * roundPxInt, width, height);
    ovalF = new RectF(oval);
    canvas.drawArc(ovalF, 0.0f, 90.0f, true, paint);

    // Big and small rectangles
    canvas.drawRect(rectF, paint);
    canvas.drawRect(rectFRound, paint);

    return output;
}

From source file:com.facebook.litho.TouchExpansionDelegate.java

void draw(Canvas canvas, Paint paint) {
    for (int i = mDelegates.size() - 1; i >= 0; i--) {
        canvas.drawRect(mDelegates.valueAt(i).mDelegateBounds, paint);
    }//from  w w w.j av  a2s.com
}

From source file:com.wytiger.common.widget.SlideSwitch.java

@Override
protected void onDraw(Canvas canvas) {
    if (mShape == SHAPE_RECT) {
        paint.setColor(Color.GRAY);
        canvas.drawRect(backRect, paint);
        paint.setColor(color_theme);//from www. j  a v  a 2  s .com
        paint.setAlpha(alpha);
        canvas.drawRect(backRect, paint);
        frontRect.set(frontRect_left, RIM_SIZE, frontRect_left + getMeasuredWidth() / 2 - RIM_SIZE,
                getMeasuredHeight() - RIM_SIZE);
        paint.setColor(Color.WHITE);
        canvas.drawRect(frontRect, paint);
    } else {
        // draw circle
        int radius;
        radius = backRect.height() / 2 - RIM_SIZE;
        paint.setColor(Color.GRAY);
        backCircleRect.set(backRect);
        canvas.drawRoundRect(backCircleRect, radius, radius, paint);
        paint.setColor(color_theme);
        paint.setAlpha(alpha);
        canvas.drawRoundRect(backCircleRect, radius, radius, paint);
        frontRect.set(frontRect_left, RIM_SIZE, frontRect_left + backRect.height() - 2 * RIM_SIZE,
                backRect.height() - RIM_SIZE);
        frontCircleRect.set(frontRect);
        paint.setColor(Color.WHITE);
        canvas.drawRoundRect(frontCircleRect, radius, radius, paint);
    }
}

From source file:com.askjeffreyliu.camera2barcode.CameraActivity.java

@Subscribe //(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MultiResultEvent event) {
    if (event != null && event.results != null && event.results.length > 0) {
        mGraphicOverlay.clear();//w  ww .j  a  v  a 2  s  . c  om
        handler.removeCallbacks(runnable);

        for (int i = 0; i < event.results.length; i++) {
            final Result r = event.results[i];
            ArrayList<Point> pointArrayList = new ArrayList<>();
            for (int j = 0; j < r.getResultPoints().length; j++) {
                float x, y;
                try {
                    x = r.getResultPoints()[j].getX();
                    y = r.getResultPoints()[j].getY();
                } catch (NullPointerException e) {
                    return;
                }

                final float scaledX = x / event.width * mGraphicOverlay.getWidth();
                final float scaledY = y / event.height * mGraphicOverlay.getHeight();
                pointArrayList.add(new Point((int) scaledX, (int) scaledY));
            }
            final Rect rect = Utils.createRect(pointArrayList, r.getBarcodeFormat() == BarcodeFormat.QR_CODE);

            mGraphicOverlay.add(new GraphicOverlay.Graphic(mGraphicOverlay) {
                @Override
                public void draw(Canvas canvas) {
                    canvas.drawRect(rect, paint);
                    canvas.drawText(r.getText(), rect.left, rect.bottom, textPaint);
                }
            });

            CameraActivity.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    getPageSetOverlay(false);
                }
            });
            handler.postDelayed(runnable, 100);
        }
    }
}

From source file:arun.com.chromer.browsing.article.view.ElasticDragDismissFrameLayout.java

@Override
public void dispatchDraw(Canvas canvas) {
    super.dispatchDraw(canvas);

    if (draggingBackground != null) {
        canvas.drawRect(draggingBackground, draggingBackgroundPaint);
    }// w  ww  .j  a  v a 2 s  . c  o  m
}

From source file:com.xcyo.live.view.SlideSwitch.java

@Override
protected void onDraw(Canvas canvas) {
    if (shape == SHAPE_RECT) {
        paint.setColor(Color.GRAY);
        canvas.drawRect(backRect, paint);
        paint.setColor(color_theme);/*from   w ww.j  av  a  2 s.  c o m*/
        paint.setAlpha(alpha);
        canvas.drawRect(backRect, paint);
        frontRect.set(frontRect_left, RIM_SIZE, frontRect_left + getMeasuredWidth() / 2 - RIM_SIZE,
                getMeasuredHeight() - RIM_SIZE);
        paint.setColor(Color.WHITE);
        canvas.drawRect(frontRect, paint);
    } else {
        // draw circle
        int radius;
        radius = backRect.height() / 2;
        paint.setColor(Color.GRAY);
        backCircleRect.set(backRect);
        canvas.drawRoundRect(backCircleRect, radius, radius, paint);
        paint.setColor(color_theme);
        paint.setAlpha(alpha);
        canvas.drawRoundRect(backCircleRect, radius, radius, paint);
        frontRect.set(frontRect_left, RIM_SIZE, frontRect_left + backRect.height() - 2 * RIM_SIZE,
                backRect.height() - RIM_SIZE);
        frontCircleRect.set(frontRect);
        paint.setColor(Color.WHITE);
        canvas.drawRoundRect(frontCircleRect, radius, radius, paint);
    }
}

From source file:com.example.administrator.iclub21.view.SlideSwitch.java

@Override
protected void onDraw(Canvas canvas) {
    if (shape == SHAPE_RECT) {
        paint.setColor(Color.GRAY);
        canvas.drawRect(backRect, paint);
        paint.setColor(color_theme);//ww  w.ja va  2s .  c o m
        paint.setAlpha(alpha);
        canvas.drawRect(backRect, paint);
        frontRect.set(frontRect_left, RIM_SIZE, frontRect_left + getMeasuredWidth() / 2 - RIM_SIZE,
                getMeasuredHeight() - RIM_SIZE);
        paint.setColor(Color.WHITE);
        canvas.drawRect(frontRect, paint);
    } else {
        // draw circle
        int radius;
        radius = backRect.height() / 2 - RIM_SIZE;
        paint.setColor(Color.GRAY);
        backCircleRect.set(backRect);
        canvas.drawRoundRect(backCircleRect, radius, radius, paint);
        paint.setColor(color_theme);
        paint.setAlpha(alpha);
        canvas.drawRoundRect(backCircleRect, radius, radius, paint);
        frontRect.set(frontRect_left, RIM_SIZE, frontRect_left + backRect.height() - 2 * RIM_SIZE,
                backRect.height() - RIM_SIZE);
        frontCircleRect.set(frontRect);
        paint.setColor(Color.WHITE);
        canvas.drawRoundRect(frontCircleRect, radius, radius, paint);
    }
}

From source file:com.tcl.widget.demo.ui.widget.supercleaner.SlideSwitch.java

@Override
protected void onDraw(Canvas canvas) {
    if (shape == SHAPE_RECT) {
        paint.setColor(Color.GRAY);
        canvas.drawRect(backRect, paint);
        paint.setColor(color_theme);/*from   w  w w . ja  va  2 s .  com*/
        paint.setAlpha(alpha);
        canvas.drawRect(backRect, paint);
        frontRect.set(frontRect_left, RIM_SIZE, frontRect_left + getMeasuredWidth() / 2 - RIM_SIZE,
                getMeasuredHeight() - RIM_SIZE);
        paint.setColor(Color.WHITE);
        canvas.drawRect(frontRect, paint);
    } else {
        // draw circle
        int radius;
        radius = backRect.height() / 2 - RIM_SIZE;
        paint.setColor(Color.GRAY);
        backCircleRect.set(backRect);
        canvas.drawRoundRect(backCircleRect, radius, radius, paint);

        paint.setColor(color_theme);
        paint.setAlpha(alpha);
        canvas.drawRoundRect(backCircleRect, radius, radius, paint);

        frontRect.set(frontRect_left, RIM_SIZE, frontRect_left + backRect.height() - 2 * RIM_SIZE,
                backRect.height() - RIM_SIZE);
        frontCircleRect.set(frontRect);
        paint.setColor(Color.WHITE);
        canvas.drawRoundRect(frontCircleRect, radius, radius, paint);
    }
}

From source file:com.mx.hb.moon.view.SlideSwitch.java

@Override
protected void onDraw(Canvas canvas) {
    if (shape == SHAPE_RECT) {//
        paint.setColor(Color.LTGRAY);
        canvas.drawRect(backRect, paint);
        paint.setColor(color_theme);//from w ww  .  j  av a  2s .  c  o  m
        paint.setAlpha(alpha);
        canvas.drawRect(backRect, paint);
        frontRect.set(frontRect_left, RIM_SIZE, frontRect_left + getMeasuredWidth() / 2 - RIM_SIZE,
                getMeasuredHeight() - RIM_SIZE);
        paint.setColor(Color.WHITE);
        canvas.drawRect(frontRect, paint);
    } else {//
        int radius;
        radius = backRect.height() / 2 - RIM_SIZE;
        paint.setColor(Color.LTGRAY);
        backCircleRect.set(backRect);
        canvas.drawRoundRect(backCircleRect, radius, radius, paint);
        paint.setColor(color_theme);
        paint.setAlpha(alpha);
        canvas.drawRoundRect(backCircleRect, radius, radius, paint);
        frontRect.set(frontRect_left, RIM_SIZE, frontRect_left + backRect.height() - 2 * RIM_SIZE,
                backRect.height() - RIM_SIZE);
        frontCircleRect.set(frontRect);
        paint.setColor(Color.WHITE);
        canvas.drawRoundRect(frontCircleRect, radius, radius, paint);
    }
}