Example usage for android.graphics PorterDuffXfermode PorterDuffXfermode

List of usage examples for android.graphics PorterDuffXfermode PorterDuffXfermode

Introduction

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

Prototype

public PorterDuffXfermode(PorterDuff.Mode mode) 

Source Link

Document

Create an xfermode that uses the specified porter-duff mode.

Usage

From source file:com.android.andryyu.lifehelper.widget.RippleView.java

private Bitmap getCircleBitmap(final int radius) {
    final Bitmap output = Bitmap.createBitmap(originBitmap.getWidth(), originBitmap.getHeight(),
            Bitmap.Config.ARGB_8888);//from   w  w w . j a  v a  2s .  co m
    final Canvas canvas = new Canvas(output);
    final Paint paint = new Paint();
    final Rect rect = new Rect((int) (x - radius), (int) (y - radius), (int) (x + radius), (int) (y + radius));

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    canvas.drawCircle(x, y, radius, paint);

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

    return output;
}

From source file:com.graphics.FingerPaint.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    mPaint.setXfermode(null);/*  w  w  w .  j a v a  2s .co m*/
    mPaint.setAlpha(0xFF);

    localCoArray.eraserFlag = false;
    localCoArray.scratchFlag = false;
    switch (item.getItemId()) {
    case COLOR_MENU_ID:
        new ColorPickerDialog(this, this, mPaint.getColor()).show();
        return true;
    case EMBOSS_MENU_ID:
        if (mPaint.getMaskFilter() != mEmboss) {
            mPaint.setMaskFilter(mEmboss);
            localCoArray.embossFlag = true;
            localCoArray.blurFlag = false;
        } else {
            mPaint.setMaskFilter(null);
            localCoArray.embossFlag = false;
        }
        return true;
    case BLUR_MENU_ID:
        if (mPaint.getMaskFilter() != mBlur) {
            mPaint.setMaskFilter(mBlur);
            localCoArray.embossFlag = false;
            localCoArray.blurFlag = true;
        } else {
            mPaint.setMaskFilter(null);
            localCoArray.blurFlag = false;
        }
        return true;
    case ERASE_MENU_ID:
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        localCoArray.eraserFlag = true;
        localCoArray.scratchFlag = false;
        return true;
    case SRCATOP_MENU_ID:
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
        mPaint.setAlpha(0x80);
        localCoArray.scratchFlag = true;
        localCoArray.eraserFlag = false;
        return true;
    }
    return super.onOptionsItemSelected(item);
}

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

/**
 * /* ww  w  .ja v  a 2  s. co  m*/
 * 
 * @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.dunrite.xpaper.utility.Utils.java

public static Drawable combineImages2(Drawable background, Drawable foreground, Drawable deviceMisc,
        int backgroundCol, int foregroundCol, String type, Context context) {
    Bitmap cs;//from www . j  ava 2s . co m
    Bitmap device = null;
    int width;
    int height;

    //TEXTURE TESTING
    String textureLocation = "";
    Bitmap foregroundTexture = null;
    //TODO: will need some type of way to know which location to put the texture (foreground/background/both)
    //        String textureLocation = "foreground";
    //        type = "";
    //TODO: will need some type of way to know which foreground texture drawable to pull from
    //        Bitmap foregroundTexture = ((BitmapDrawable) ContextCompat.getDrawable(context, R.drawable.texture_bamboo)).getBitmap();
    //        foregroundTexture = foregroundTexture.copy(Bitmap.Config.ARGB_8888, true);

    //convert from drawable to bitmap
    Bitmap back = ((BitmapDrawable) background).getBitmap();
    back = back.copy(Bitmap.Config.ARGB_8888, true);

    Bitmap fore = ((BitmapDrawable) foreground).getBitmap();
    fore = fore.copy(Bitmap.Config.ARGB_8888, true);

    if (type.equals("device")) {
        device = ((BitmapDrawable) deviceMisc).getBitmap();
        device = device.copy(Bitmap.Config.ARGB_8888, true);
    }
    //initialize Canvas
    if (type.equals("preview") || type.equals("device")) {
        width = back.getWidth() / 2;
        height = back.getHeight() / 2;
    } else {
        width = back.getWidth();
        height = back.getHeight();
    }
    cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas comboImage = new Canvas(cs);

    Paint paint1 = new Paint();
    paint1.setFilterBitmap(false);
    //Filter for Background
    if (textureLocation.equals("background") || textureLocation.equals("both")) {
        paint1.setColorFilter(new PorterDuffColorFilter(backgroundCol, PorterDuff.Mode.DST_ATOP));
    } else {
        paint1.setColorFilter(new PorterDuffColorFilter(backgroundCol, PorterDuff.Mode.SRC_ATOP));
    }

    //Filter for Foreground
    Paint paint2 = new Paint();
    paint2.setFilterBitmap(false);
    if (textureLocation.equals("foreground") || textureLocation.equals("both")) {
        //DIFFICULT CASE
        //create new canvas to combine
        Canvas foreCanvas = new Canvas(fore);

        //set up paint for texture
        Paint paintTexture = new Paint();
        paintTexture.setFilterBitmap(false);
        paintTexture.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

        //draw our combination
        foreCanvas.drawBitmap(foregroundTexture, 0, 0, paintTexture);

        //set up theme for outer image
        paint2.setColorFilter(new PorterDuffColorFilter(foregroundCol, PorterDuff.Mode.DST_IN));
    } else {
        paint2.setColorFilter(new PorterDuffColorFilter(foregroundCol, PorterDuff.Mode.SRC_ATOP));
    }

    //Draw both images
    if (type.equals("preview") || type.equals("device")) {
        if (type.equals("device") && device != null) {
            comboImage.drawBitmap(
                    Bitmap.createScaledBitmap(device, device.getWidth() / 2, device.getHeight() / 2, true), 0,
                    0, null);
            device.recycle();
        }
        comboImage.drawBitmap(Bitmap.createScaledBitmap(back, back.getWidth() / 2, back.getHeight() / 2, true),
                0, 0, paint1);
        comboImage.drawBitmap(Bitmap.createScaledBitmap(fore, fore.getWidth() / 2, fore.getHeight() / 2, true),
                0, 0, paint2);

    } else {
        comboImage.drawBitmap(back, 0, 0, paint1);
        comboImage.drawBitmap(fore, 0, 0, paint2);
    }
    back.recycle();
    fore.recycle();

    return new BitmapDrawable(context.getResources(), cs);
}

From source file:com.silentcircle.common.util.ViewUtil.java

/**
 * Cut a circular image from provided bitmap.
 *
 * @param bitmap Bitmap from which to cut the circle.
 *
 *//*www  . ja  v a 2s  .  com*/
public static Bitmap getCircularBitmap(final Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int dimens = width;
    if (width > height) {
        dimens = height;
    }
    Bitmap output = Bitmap.createBitmap(dimens, dimens, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final float radius = width / 2.0f;
    final int color = 0xffff00ff;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, width, height);

    canvas.drawARGB(0, 0, 0, 0);
    paint.setAntiAlias(true);
    paint.setColor(color);
    canvas.drawCircle(radius, radius, radius, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}

From source file:de.vanita5.twittnuker.view.ShapedImageView.java

private void updateShadowBitmap() {
    if (USE_OUTLINE)
        return;//from   ww w.  jav a2 s  .c o  m
    final int width = getWidth(), height = getHeight();
    if (width <= 0 || height <= 0)
        return;
    final int contentLeft = getPaddingLeft(), contentTop = getPaddingTop(),
            contentRight = width - getPaddingRight(), contentBottom = height - getPaddingBottom();
    final int contentWidth = contentRight - contentLeft, contentHeight = contentBottom - contentTop;
    final float radius = mShadowRadius, dy = radius * 1.5f / 2;
    final int size = Math.round(Math.min(contentWidth, contentHeight) + radius * 2);
    mShadowBitmap = Bitmap.createBitmap(size, Math.round(size + dy), Config.ARGB_8888);
    Canvas canvas = new Canvas(mShadowBitmap);
    final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.WHITE);
    paint.setShadowLayer(radius, 0, radius * 1.5f / 2, SHADOW_START_COLOR);
    final RectF rect = new RectF(radius, radius, size - radius, size - radius);
    canvas.drawOval(rect, paint);
    paint.setShadowLayer(0, 0, 0, 0);
    paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
    canvas.drawOval(rect, paint);
    invalidate();
}

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

/**
 * /*from www  . j ava2  s.c  om*/
 * 
 * @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.tafayor.selfcamerashot.taflib.helpers.GraphicsHelper.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {

    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),

            bitmap.getHeight(), Bitmap.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);

    final float roundPx = 12;

    paint.setAntiAlias(true);/* w w  w.  j  a v  a  2s .c om*/

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

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

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}

From source file:com.mappn.gfan.ui.HomeTabActivity.java

private Bitmap drawBitmap(DisplayMetrics dm, Bitmap background, Bitmap corner) {
    Canvas canvas = new Canvas();
    final int height = background.getScaledHeight(dm);
    final int width = background.getScaledWidth(dm);
    Bitmap smallBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    canvas.setBitmap(smallBitmap);// w  w  w.j  a v a 2s. c o m
    Paint textPainter = new Paint(Paint.ANTI_ALIAS_FLAG);
    canvas.drawBitmap(background, 0, 0, textPainter);
    textPainter.setXfermode(new PorterDuffXfermode(Mode.SRC_OVER));
    canvas.drawBitmap(corner, width - corner.getScaledWidth(dm), 0, textPainter);
    canvas.save();
    return smallBitmap;
}

From source file:com.mallapp.utils.ImageLoader.java

/** 
* Crops a circle out of the thumbnail photo. 
*//*w ww .  ja  va 2 s . co m*/
public Bitmap getCroppedBitmap(final Bitmap bitmap) {
    Bitmap output = null;

    try {

        output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

        final Canvas canvas = new Canvas(output);

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

        final int halfWidth = bitmap.getWidth() / 2;
        final int halfHeight = bitmap.getHeight() / 2;

        canvas.drawCircle(halfWidth, halfHeight, Math.max(halfWidth, halfHeight), paint);

        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));

        canvas.drawBitmap(bitmap, rect, rect, paint);

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

    return output;
}