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:com.yk.notification.util.BitmapUtil.java

/**
 * //from   ww w  . j  av a 2s . c  o  m
 * 
 * @param bitmap
 *            ?Bitmap
 * @return Bitmap
 */
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);

    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:com.yk.notification.util.BitmapUtil.java

/**
 * /*from  w  w w.  ja  va2  s  .c om*/
 * 
 * @param bitmap
 *            ?Bitmap
 * @param roundPx
 *            ?
 * @return Bitmap
 */
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {

    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}

From source file:com.wanikani.androidnotifier.DashboardFragment.java

/**
 * Apply a circular mask on the given bitmap. This method is
 * used to display the avatar.//  w w  w.  j  ava  2 s  .  co  m
 * @param bmp an input bitmap
 * @param result the output (masked) bitmap
 */
private Bitmap mask(Bitmap bmp) {
    Bitmap result, mask;
    Drawable dmask;
    Canvas canvas;
    Paint paint;

    result = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), Bitmap.Config.ARGB_8888);
    canvas = new Canvas(result);

    dmask = getResources().getDrawable(R.drawable.gravatar_mask);
    mask = ((BitmapDrawable) dmask).getBitmap();

    paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    canvas.drawBitmap(bmp, 0, 0, null);
    canvas.drawBitmap(mask, 0, 0, paint);

    return result;
}

From source file:com.yk.notification.util.BitmapUtil.java

/**
 * ??/*from ww  w. ja  va2 s. c  o m*/
 * 
 * @param bitmap
 *            Bitmap
 * @return Bitmap
 */
public static Bitmap getRoundBitmap(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    float roundPx;
    float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
    if (width <= height) {
        roundPx = width / 2;
        top = 0;
        bottom = width;
        left = 0;
        right = width;
        height = width;
        dst_left = 0;
        dst_top = 0;
        dst_right = width;
        dst_bottom = width;
    } else {
        roundPx = height / 2;
        float clip = (width - height) / 2;
        left = clip;
        right = width - clip;
        top = 0;
        bottom = height;
        width = height;
        dst_left = 0;
        dst_top = 0;
        dst_right = height;
        dst_bottom = height;
    }

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

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect src = new Rect((int) left, (int) top, (int) right, (int) bottom);
    final Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom);
    final RectF rectF = new RectF(dst);

    paint.setAntiAlias(true);

    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, src, dst, paint);
    return output;
}

From source file:com.tct.mail.browse.MessageHeaderView.java

/**
 * Frames the input bitmap in a circle./*from ww w .  ja v a  2 s. c  om*/
 */
private static Bitmap frameBitmapInCircle(Bitmap input) {
    if (input == null) {
        return null;
    }

    // Crop the image if not squared.
    int inputWidth = input.getWidth();
    int inputHeight = input.getHeight();
    int targetX, targetY, targetSize;
    if (inputWidth >= inputHeight) {
        targetX = inputWidth / 2 - inputHeight / 2;
        targetY = 0;
        targetSize = inputHeight;
    } else {
        targetX = 0;
        targetY = inputHeight / 2 - inputWidth / 2;
        targetSize = inputWidth;
    }

    // Create an output bitmap and a canvas to draw on it.
    Bitmap output = Bitmap.createBitmap(targetSize, targetSize, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    // Create a black paint to draw the mask.
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.BLACK);

    // Draw a circle.
    canvas.drawCircle(targetSize / 2, targetSize / 2, targetSize / 2, paint);

    // Replace the black parts of the mask with the input image.
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(input, targetX /* left */, targetY /* top */, paint);

    return output;
}

From source file:com.tct.mail.utils.NotificationUtils.java

/**
 * Crop a square bitmap into a circular one. Used for both contact photos and letter tiles.
 * @param icon Square bitmap to crop//  w ww.  j  a  v a2  s . c o m
 * @return Circular bitmap
 */
private static Bitmap cropSquareIconToCircle(Bitmap icon) {
    final int iconWidth = icon.getWidth();
    final Bitmap newIcon = Bitmap.createBitmap(iconWidth, iconWidth, Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(newIcon);
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, icon.getWidth(), icon.getHeight());

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    canvas.drawCircle(iconWidth / 2, iconWidth / 2, iconWidth / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(icon, rect, rect, paint);

    return newIcon;
}

From source file:com.malin.rxjava.activity.MainActivity.java

private void recycleImageView() {
    //ImageView??
    if (mGoToRecycleImageView) {
        Logger.d("onDestroy()> RecycleBitmap.recycleImageView(mImageView)");
        RecycleBitmap.recycleImageView(mImageView);
        mImageView.setImageBitmap(null);
    }//from  w ww  .java  2 s .c  om

    if (mManyBitmapSuperposition != null && !mManyBitmapSuperposition.isRecycled()) {
        mManyBitmapSuperposition.recycle();
        mManyBitmapSuperposition = null;
    }

    //@link http://blog.csdn.net/yanzi1225627/article/details/8236309
    if (mCanvas != null) {
        //?
        Paint paint = new Paint();
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        mCanvas.drawPaint(paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
        mCanvas = null;
    }

}

From source file:com.android.app.MediaPlaybackActivity.java

/**
 * /*  w  w  w.  j a  va 2 s .c  o m*/
 *
 * @param src
 * @return
 */
private Bitmap createCircleBitmap(Bitmap src) {
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setARGB(255, 241, 239, 229);

    int width = src.getWidth();
    int height = src.getHeight();
    Bitmap target = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(target);

    canvas.drawCircle(width / 2, width / 2, width / 2, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(src, 0, 0, paint);

    return target;
}

From source file:me.tb.player.SkeletonActivity.java

@Override
public Bitmap getCircleBitmap(Bitmap bitmap) {
    final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(output);

    final int color = Color.RED;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);

    paint.setAntiAlias(true);/*from   w w  w.j  a v  a2s.com*/
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawOval(rectF, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    bitmap.recycle();

    return output;
}

From source file:org.mariotaku.twidere.util.Utils.java

public static Bitmap createAlphaGradientBanner(final Bitmap orig) {
    if (orig == null)
        return null;
    final int width = orig.getWidth(), height = orig.getHeight();
    final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(bitmap);
    final Paint paint = new Paint();
    final LinearGradient shader = new LinearGradient(width / 2, 0, width / 2, height, 0xffffffff, 0x00ffffff,
            Shader.TileMode.CLAMP);/*from  w ww  . j  av a2  s  .c o  m*/
    paint.setShader(shader);
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
    canvas.drawBitmap(orig, 0, 0, null);
    canvas.drawRect(0, 0, width, height, paint);
    return bitmap;
}