Example usage for android.graphics Paint setXfermode

List of usage examples for android.graphics Paint setXfermode

Introduction

In this page you can find the example usage for android.graphics Paint setXfermode.

Prototype

public Xfermode setXfermode(Xfermode xfermode) 

Source Link

Document

Set or clear the transfer mode object.

Usage

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int backgroundColor, int borderColor) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth() + 12, bitmap.getHeight() + 12,
            Bitmap.Config.ARGB_8888);//  w  w w .j  a v a 2  s .  c  o  m

    Canvas canvas = new Canvas(output);
    //canvas.drawARGB(Color.alpha(backgroundColor), Color.red(backgroundColor), Color.green(backgroundColor), Color.blue(backgroundColor));

    Paint borderPaint = new Paint();
    borderPaint.setAntiAlias(true);
    borderPaint.setColor(borderColor);
    borderPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));
    borderPaint.setShadowLayer(2.0f, 0.0f, 2.0f, Color.BLACK);

    int centerWidth = output.getWidth() / 2;
    int centerHeight = output.getHeight() / 2;
    canvas.drawCircle(centerWidth, centerHeight, ((centerWidth + centerHeight) / 2) - 4, borderPaint);

    Paint paint = new Paint();
    paint.setAntiAlias(true);

    Rect rectS = new Rect(0, 0, output.getWidth() - 12, output.getHeight() - 12);
    Rect rectD = new Rect(0, 0, output.getWidth(), output.getHeight());

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
    canvas.drawBitmap(bitmap, rectS, rectD, paint);

    return output;
}

From source file:Main.java

public static Bitmap toCircleBitmap(Bitmap bitmap) {

    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int r = width < height ? width : height;

    Bitmap buffer = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(buffer);
    Paint paint = new Paint();
    paint.setAntiAlias(true);/*from  w w  w  .j  a  v  a 2s. c o m*/
    RectF rect = new RectF(0, 0, r, r);

    canvas.drawCircle(r / 2, r / 2, r / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, null, rect, paint);
    bitmap.recycle();
    return buffer;
}

From source file:Main.java

public static Bitmap getRoundBitmap(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Bitmap out = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(out);
    Paint paint = new Paint();
    paint.setColor(Color.WHITE);/* ww w . ja v  a 2s.  c  om*/
    paint.setAntiAlias(true);
    canvas.drawCircle(width / 2, height / 2, width / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    Rect rect = new Rect(0, 0, width, height);
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return out;
}

From source file:Main.java

public static Bitmap drawTextToBitmap(Bitmap bitmap, String gText) {
    OutputStream outStream = null;
    Bitmap.Config bitmapConfig = bitmap.getConfig();
    // set default bitmap config if none
    if (bitmapConfig == null) {
        bitmapConfig = Bitmap.Config.ARGB_8888;
    }//from w ww .j  a  v  a  2s  .  co  m
    String dataPath = Environment.getExternalStorageDirectory().toString() + "/SignChat/Temp/temp" + "0"
            + pictureNum + ".jpg";

    try {

        FileOutputStream out = new FileOutputStream(dataPath);

        // NEWLY ADDED CODE STARTS HERE [
        Canvas canvas = new Canvas(bitmap);

        Paint paint = new Paint();
        paint.setColor(Color.WHITE); // Text Color
        paint.setStrokeWidth(12); // Text Size
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)); // Text
        // Overlapping
        // Pattern
        // some more settings...

        canvas.drawBitmap(bitmap, 0, 0, paint);
        canvas.drawText("Testing...", 10, 10, paint);
        // NEWLY ADDED CODE ENDS HERE ]

        bitmap.compress(CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return bitmap;
}

From source file:Main.java

public static Bitmap createRoundCornerBitmap(Bitmap bitmap, float roundPx, boolean lt, boolean rt, boolean lb,
        boolean rb) {
    Bitmap roundCornerBitmap = createRoundCornerBitmap(bitmap, roundPx);
    Canvas canvas = new Canvas(roundCornerBitmap);
    canvas.drawARGB(0, 0, 0, 0);// www  . j  a  va  2s . c  o  m
    int bw = bitmap.getWidth();
    int bh = bitmap.getHeight();
    int centerW = bw / 2;
    int centerH = bh / 2;

    Paint paint = new Paint();
    Paint defaultPaint = new Paint();
    int color = 0xff424242;
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    paint.setAntiAlias(true);
    paint.setColor(color);

    Bitmap cutBitmap = null;
    Canvas cutCanvas = null;
    if (!lt) {
        cutBitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ARGB_8888);
        cutCanvas = new Canvas(cutBitmap);
        cutCanvas.drawRect(0, 0, centerW, centerH, defaultPaint);
        cutCanvas.drawBitmap(bitmap, 0, 0, paint);
        canvas.drawBitmap(cutBitmap, 0, 0, defaultPaint);
        cutBitmap.recycle();
    }
    if (!rt) {
        cutBitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ARGB_8888);
        cutCanvas = new Canvas(cutBitmap);
        cutCanvas.drawRect(centerW, 0, bw, centerH, defaultPaint);
        cutCanvas.drawBitmap(bitmap, 0, 0, paint);
        canvas.drawBitmap(cutBitmap, 0, 0, defaultPaint);
        cutBitmap.recycle();
    }
    if (!lb) {
        cutBitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ARGB_8888);
        cutCanvas = new Canvas(cutBitmap);
        cutCanvas.drawRect(0, centerH, centerW, bh, defaultPaint);
        cutCanvas.drawBitmap(bitmap, 0, 0, paint);
        canvas.drawBitmap(cutBitmap, 0, 0, defaultPaint);
        cutBitmap.recycle();
    }
    if (!rb) {
        cutBitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ARGB_8888);
        cutCanvas = new Canvas(cutBitmap);
        cutCanvas.drawRect(centerW, centerH, bw, bh, defaultPaint);
        cutCanvas.drawBitmap(bitmap, 0, 0, paint);
        canvas.drawBitmap(cutBitmap, 0, 0, defaultPaint);
        cutBitmap.recycle();
    }
    return roundCornerBitmap;
}

From source file:Main.java

/**
 * Changes the paint color to the specified value.
 *
 * @param paint the object to mutate with the new color
 * @param argb a 32-bit integer with eight bits for alpha, red, green, and blue,
 *        respectively/*from  w ww . j  a  v a 2 s . c o  m*/
 */
public static void changePaint(Paint paint, int argb) {
    // TODO(user): can the following two lines can be replaced by:
    // paint.setColor(argb)?
    paint.setColor(argb & 0x00FFFFFF);
    paint.setAlpha((argb >> 24) & 0xFF);
    paint.setXfermode(null);
}

From source file:Main.java

public static Bitmap createFramedImage(Drawable imageDrawable, int borderThickness) {
    int size = Math.min(imageDrawable.getMinimumWidth(), imageDrawable.getMinimumHeight());
    Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    RectF outerRect = new RectF(0, 0, size, size);

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.RED);/*w  w  w  .jav a 2 s.  co  m*/
    canvas.drawRect(outerRect, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    imageDrawable.setBounds(0, 0, size, size);

    // Save the layer to apply the paint
    canvas.saveLayer(outerRect, paint, Canvas.ALL_SAVE_FLAG);
    imageDrawable.draw(canvas);
    canvas.restore();

    // FRAMING THE PHOTO
    float border = size / 15f;

    // 1. Create offscreen bitmap link: http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU#t=1035s
    Bitmap framedOutput = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    Canvas framedCanvas = new Canvas(framedOutput);
    // End of Step 1

    // Start - TODO IMPORTANT - this section shouldn't be included in the final code
    // It's needed here to differentiate step 2 (red) with the background color of the activity
    // It's should be commented out after the codes includes step 3 onwards
    // Paint squaredPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    // squaredPaint.setColor(Color.BLUE);
    // framedCanvas.drawRoundRect(outerRect, 0f, 0f, squaredPaint);
    // End

    // 2. Draw an opaque rounded rectangle link:
    RectF innerRect = new RectF(border, border, size - border, size - border);

    Paint innerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    innerPaint.setColor(Color.RED);
    //      framedCanvas.drawRoundRect(innerRect, cornerRadius, cornerRadius, outerPaint);
    framedCanvas.drawRect(innerRect, innerPaint);

    // 3. Set the Power Duff mode link:
    Paint outerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    outerPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));

    // 4. Draw a translucent rounded rectangle link:
    outerPaint.setColor(Color.argb(255, 255, 255, 255));
    //      framedCanvas.drawRoundRect(outerRect, cornerRadius, cornerRadius, outerPaint);
    framedCanvas.drawRect(outerRect, outerPaint);

    // 5. Draw the frame on top of original bitmap
    canvas.drawBitmap(framedOutput, 0f, 0f, null);

    return output;
}

From source file:Main.java

public static Bitmap toOvalBitmap(Bitmap bitmap) {
    Bitmap output = Bitmap.createBitmap(bitmap.getHeight(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    Paint paint = new Paint();
    paint.setAntiAlias(true);//w w  w  .j  a  va2  s  .  c o  m
    Rect rect = new Rect(0, 0, bitmap.getHeight(), bitmap.getHeight());
    RectF rectF = new RectF(rect);

    canvas.drawOval(rectF, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rectF, paint);
    return output;
}

From source file:Main.java

public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
    int i = bitmap.getWidth();
    int j = bitmap.getHeight();
    Matrix matrix = new Matrix();
    matrix.preScale(1.0F, -1F);/*from  w  w w  .  ja  va  2s.c om*/
    Bitmap bitmap1 = Bitmap.createBitmap(bitmap, 0, j / 2, i, j / 2, matrix, false);
    Bitmap bitmap2 = Bitmap.createBitmap(i, j + j / 2, android.graphics.Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap2);
    canvas.drawBitmap(bitmap, 0.0F, 0.0F, null);
    Paint paint = new Paint();
    canvas.drawRect(0.0F, j, i, j + 4, paint);
    canvas.drawBitmap(bitmap1, 0.0F, j + 4, null);
    Paint paint1 = new Paint();
    paint1.setShader(new LinearGradient(0.0F, bitmap.getHeight(), 0.0F, 4 + bitmap2.getHeight(), 0x70ffffff,
            0xffffff, android.graphics.Shader.TileMode.CLAMP));
    paint1.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.DST_IN));
    canvas.drawRect(0.0F, j, i, 4 + bitmap2.getHeight(), paint1);
    return bitmap2;
}

From source file:Main.java

public static Bitmap getScaledCircleCroppedBitmap(Bitmap bitmap, int destSize) {
    if (bitmap == null)
        return null;
    Bitmap output = Bitmap.createBitmap(destSize, destSize, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int srcSize = Math.min(bitmap.getWidth(), bitmap.getHeight());
    final int srcX = (bitmap.getWidth() - srcSize) / 2;
    final int srcY = (bitmap.getHeight() - srcSize) / 2;
    final Rect srcRect = new Rect(srcX, srcY, srcX + srcSize, srcY + srcSize);
    final Rect destRect = new Rect(0, 0, destSize, destSize);
    final int color = 0xff424242;
    final Paint paint = new Paint();

    paint.setAntiAlias(true);/* w ww  .  j a  v  a  2 s  . c  o m*/
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawCircle(destSize / 2, destSize / 2, destSize / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, srcRect, destRect, paint);
    return output;
}