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 createReflectionImageWithOrigin(Bitmap bitmap) {
    final int reflectionGap = 4;
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);/*from ww w .  j  a v a 2s.c  o  m*/

    Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2, width, height / 2, matrix, false);

    Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmapWithReflection);
    canvas.drawBitmap(bitmap, 0, 0, null);
    Paint deafalutPaint = new Paint();
    canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);

    canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
            bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
    paint.setShader(shader);
    // Set the Transfer mode to be porter duff and destination in
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
    // Draw a rectangle using the paint with our linear gradient
    canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);

    return bitmapWithReflection;
}

From source file:Main.java

public static Bitmap createScreenshot4(View view, int thumbnailWidth, int thumbnailHeight) {
    if (view != null) {
        Bitmap mCapture;//from www .j a va  2  s  .  c o  m
        try {
            mCapture = Bitmap.createBitmap(thumbnailWidth, thumbnailHeight, Bitmap.Config.RGB_565);
        } catch (OutOfMemoryError e) {
            return null;
        }
        Canvas c = new Canvas(mCapture);
        Paint transPainter = new Paint();
        transPainter.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        c.drawRect(0, 0, mCapture.getWidth(), mCapture.getHeight(), transPainter);
        try {
            view.draw(c);
        } catch (Exception e) {
        }
        return mCapture;
    }
    return null;
}

From source file:Main.java

public static Bitmap rotateAndFrame(Bitmap bitmap) {
    final boolean positive = sRandom.nextFloat() >= 0.5f;
    final float angle = (ROTATION_ANGLE_MIN + sRandom.nextFloat() * ROTATION_ANGLE_EXTRA)
            * (positive ? 1.0f : -1.0f);
    final double radAngle = Math.toRadians(angle);

    final int bitmapWidth = bitmap.getWidth();
    final int bitmapHeight = bitmap.getHeight();

    final double cosAngle = Math.abs(Math.cos(radAngle));
    final double sinAngle = Math.abs(Math.sin(radAngle));

    final int strokedWidth = (int) (bitmapWidth + 2 * PHOTO_BORDER_WIDTH);
    final int strokedHeight = (int) (bitmapHeight + 2 * PHOTO_BORDER_WIDTH);

    final int width = (int) (strokedHeight * sinAngle + strokedWidth * cosAngle);
    final int height = (int) (strokedWidth * sinAngle + strokedHeight * cosAngle);

    final float x = (width - bitmapWidth) / 2.0f;
    final float y = (height - bitmapHeight) / 2.0f;

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

    canvas.rotate(angle, width / 2.0f, height / 2.0f);
    canvas.drawBitmap(bitmap, x, y, sPaint);
    canvas.drawRect(x, y, x + bitmapWidth, y + bitmapHeight, sStrokePaint);

    return decored;
}

From source file:Main.java

public static Bitmap createReflectImages(Bitmap bImap) {
    if (null == bImap) {
        return null;
    }/*from ww w . j  a  v  a 2s.c om*/

    final int reflectionGap = 4;
    int width = bImap.getWidth();
    int height = bImap.getHeight();

    Bitmap bitmapWithReflection = null;
    try {
        Matrix matrix = new Matrix();
        matrix.preScale(1, -1);
        Bitmap reflectionImage = Bitmap.createBitmap(bImap, 0, height / 2, width, height / 2, matrix, false);
        bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmapWithReflection);
        canvas.drawBitmap(bImap, 0, 0, null);
        Paint deafaultPaint = new Paint();
        canvas.drawRect(0, height, width, height + reflectionGap, deafaultPaint);
        canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
        Paint paint = new Paint();
        LinearGradient shader = new LinearGradient(0, bImap.getHeight(), 0,
                bitmapWithReflection.getHeight() + reflectionGap, 0x80ffffff, 0x00ffffff, TileMode.CLAMP);
        paint.setShader(shader);
        paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
        canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);
    } catch (OutOfMemoryError e) {
    }

    return bitmapWithReflection;
}

From source file:Main.java

public static Bitmap createReflectionBitmap(Bitmap bitmap) {
    final int reflectionGap = 4;
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);//  ww w  .j a  va2s .  c o  m

    Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2, width, height / 2, matrix, false);

    Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmapWithReflection);
    canvas.drawBitmap(bitmap, 0, 0, null);
    Paint deafalutPaint = new Paint();
    canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);

    canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
            bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
    paint.setShader(shader);
    // Set the Transfer mode to be porter duff and destination in
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
    // Draw a rectangle using the paint with our linear gradient
    canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);

    return bitmapWithReflection;
}

From source file:Main.java

public static Bitmap createReflectionBitmap(Bitmap bitmap) {
    final int reflectionGap = 4;
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);/*from  ww  w .  j  a  va2s  .co  m*/

    Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2, width, height / 2, matrix, false);

    Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmapWithReflection);
    canvas.drawBitmap(bitmap, 0, 0, null);
    Paint deafalutPaint = new Paint();
    canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);

    canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
            bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, Shader.TileMode.CLAMP);
    paint.setShader(shader);
    // Set the Transfer mode to be porter duff and destination in
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    // Draw a rectangle using the paint with our linear gradient
    canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);

    return bitmapWithReflection;
}

From source file:Main.java

public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
    final int reflectionGap = 4;
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();

    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);//from   www.j  a v  a  2 s  .c  o m

    Bitmap reflectionImage;
    reflectionImage = Bitmap.createBitmap(bitmap, 0, h / 2, w, h / 2, matrix, false);

    Bitmap bitmapWithReflection = Bitmap.createBitmap(w, (h + h / 2), Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmapWithReflection);
    canvas.drawBitmap(bitmap, 0, 0, null);
    Paint defaultPaint = new Paint();
    canvas.drawRect(0, h, w, h + reflectionGap, defaultPaint);

    canvas.drawBitmap(reflectionImage, 0, h + reflectionGap, null);

    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
            bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, Shader.TileMode.CLAMP);
    paint.setShader(shader);
    // Set the Transfer mode to be porter duff and destination in
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    // Draw a rectangle using the paint with our linear gradient
    canvas.drawRect(0, h, w, bitmapWithReflection.getHeight() + reflectionGap, paint);

    return bitmapWithReflection;
}

From source file:Main.java

public static void drawBackground(Canvas canvas, int backgroundColor, int width, int height) {
    Paint paint = new Paint();
    paint.setStyle(Paint.Style.FILL_AND_STROKE);
    paint.setAntiAlias(true);/*from   w  w w  .j  a v a  2  s.co m*/
    paint.setColor(backgroundColor);
    canvas.drawRect(0, 0, width, height, paint);
}

From source file:Main.java

public static Bitmap setReflection(Bitmap bitmap, int distance, float ratio) {
    final int reflectionGap = distance;
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);//  ww w  .  j ava  2 s . com
    Bitmap reflectionBitmap = Bitmap.createBitmap(bitmap, 0, height / 2, width, (int) (height * ratio), matrix,
            false);
    Bitmap withReflectionBitmap = Bitmap.createBitmap(width, (height + (int) (height * ratio) + reflectionGap),
            Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(withReflectionBitmap);
    canvas.drawBitmap(bitmap, 0, 0, null);
    Paint defaultPaint = new Paint();
    defaultPaint.setColor(Color.TRANSPARENT);
    canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);
    canvas.drawBitmap(reflectionBitmap, 0, height + reflectionGap, null);

    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0, withReflectionBitmap.getHeight(),
            0x70ffffff, 0x00ffffff, Shader.TileMode.MIRROR);
    paint.setShader(shader);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));

    canvas.drawRect(0, height, width, withReflectionBitmap.getHeight(), paint);
    return withReflectionBitmap;
}

From source file:Main.java

public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
    if (bitmap == null) {
        return null;
    }//w  w w . j  a  v  a 2s  . c o  m
    final int reflectionGap = 4;
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);

    Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height * 2 / 3, width, height / 3, matrix, false);
    Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 3), Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmapWithReflection);
    canvas.drawBitmap(bitmap, 0, 0, null);
    Paint defaultPaint = new Paint();
    defaultPaint.setColor(Color.TRANSPARENT);
    canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);

    canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
            bitmapWithReflection.getHeight() + reflectionGap, 0x88ffffff, 0x00ffffff, TileMode.CLAMP);
    paint.setShader(shader);
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
    canvas.drawRect(0, height, width, bitmapWithReflection.getHeight(), paint);

    return bitmapWithReflection;
}