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.almalence.googsharing.Thumbnail.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int size, int pixels) {
    final int side = Math.min(bitmap.getWidth(), bitmap.getHeight());

    final Bitmap bitmapCropped = Bitmap.createBitmap(bitmap, (bitmap.getWidth() - side) / 2,
            (bitmap.getHeight() - side) / 2, side, side);

    final Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(output);

    final int color = 0xffffffff;
    final Paint paint = new Paint();
    final Rect rectSrc = new Rect(0, 0, bitmapCropped.getWidth(), bitmapCropped.getHeight());
    final Rect rect = new Rect(6, 6, output.getWidth() - 6, output.getHeight() - 6);
    final RectF rectF = new RectF(rect);
    final RectF rectFBorder = new RectF(0, 0, output.getWidth(), output.getHeight());
    final float roundPx = pixels;

    paint.setAntiAlias(true);/*from   w w w .  j  a va 2s  .c om*/
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

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

    paint.setXfermode(new PorterDuffXfermode(Mode.DST_ATOP));
    canvas.drawRoundRect(rectFBorder, roundPx, roundPx, paint);

    return output;
}

From source file:com.ibuildapp.romanblack.FanWallPlugin.data.Statics.java

/**
 * Sets the downloaded attached image.//from   w  w  w. j av a  2  s. c o m
 *
 * @param fileName picture file path
 */
public static Bitmap publishPicture(String fileName) {
    Bitmap bitmap = null;
    try {

        if (!TextUtils.isEmpty(fileName)) {
            try {
                BitmapFactory.Options opts = new BitmapFactory.Options();
                opts.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(fileName, opts);

                //Find the correct scale value. It should be the power of 2.
                int width = opts.outWidth, height = opts.outHeight;
                int scale = 1;
                while (true) {
                    if (width / 2 <= 150 || height / 2 <= 150) {
                        break;
                    }
                    width /= 2;
                    height /= 2;
                    scale *= 2;
                }
                BitmapFactory.Options opt = new BitmapFactory.Options();
                opt.inSampleSize = scale;

                bitmap = BitmapFactory.decodeFile(fileName, opt);
                int size = 0;
                if (bitmap.getHeight() > bitmap.getWidth()) {
                    size = bitmap.getWidth();
                } else {
                    size = bitmap.getHeight();
                }
                Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.RGB_565);
                Canvas canvas = new Canvas(output);

                final int color = 0xff424242;
                final Paint paint = new Paint();
                final Rect rect = new Rect(0, 0, size, size);
                final RectF rectF = new RectF(rect);
                final float roundPx = 0;

                paint.setAntiAlias(true);
                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);

                bitmap.recycle();

                return output;
            } catch (Exception e) {
                Log.w("", "");
            }
        }
    } catch (Exception ex) {

    }

    return null;
}

From source file:com.c4mprod.utils.ImageManager.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {

    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);
    final float roundPx = 20;

    paint.setAntiAlias(true);//from   w  w  w . j  a  va2  s. c  o  m
    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);

    if (output != bitmap) {
        bitmap.recycle();
    }
    return output;
}

From source file:mil.nga.giat.mage.sdk.utils.MediaUtility.java

public static Bitmap resizeAndRoundCorners(Bitmap bitmap, int maxSize) {
    boolean isLandscape = bitmap.getWidth() > bitmap.getHeight();

    int newWidth, newHeight;
    if (isLandscape) {
        newWidth = maxSize;//ww  w .  j a v  a 2 s  . co m
        newHeight = Math.round(((float) newWidth / bitmap.getWidth()) * bitmap.getHeight());
    } else {
        newHeight = maxSize;
        newWidth = Math.round(((float) newHeight / bitmap.getHeight()) * bitmap.getWidth());
    }

    Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, false);

    if (resizedBitmap != bitmap)
        bitmap.recycle();

    Bitmap roundedProfile = Bitmap.createBitmap(resizedBitmap.getWidth(), resizedBitmap.getHeight(),
            Config.ARGB_8888);

    Canvas roundedCanvas = new Canvas(roundedProfile);
    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, roundedProfile.getWidth(), roundedProfile.getHeight());
    final RectF rectF = new RectF(rect);
    final float roundPx = 7.0f;

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

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    roundedCanvas.drawBitmap(resizedBitmap, rect, rect, paint);
    return roundedProfile;
}

From source file:com.kth.baasio.baassample.BaseActivity.java

/**
 * Sets the icon color using some fancy blending mode trickery.
 *//* w w w.j a v  a2 s  .  c  om*/
protected void setActionBarColor(int color) {
    if (color == 0) {
        color = 0xffffffff;
    }

    final Resources res = getResources();
    Drawable maskDrawable = res.getDrawable(R.drawable.actionbar_icon_mask);
    if (!(maskDrawable instanceof BitmapDrawable)) {
        return;
    }

    Bitmap maskBitmap = ((BitmapDrawable) maskDrawable).getBitmap();
    final int width = maskBitmap.getWidth();
    final int height = maskBitmap.getHeight();

    Bitmap outBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(outBitmap);
    canvas.drawBitmap(maskBitmap, 0, 0, null);

    Paint maskedPaint = new Paint();
    maskedPaint.setColor(color);
    maskedPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));

    canvas.drawRect(0, 0, width, height, maskedPaint);

    BitmapDrawable outDrawable = new BitmapDrawable(res, outBitmap);
    getSupportActionBar().setIcon(outDrawable);
}

From source file:com.benefit.buy.library.http.query.callback.BitmapAjaxCallback.java

private static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
    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);
    final float roundPx = pixels;
    paint.setAntiAlias(true);/*from   www  .  j av a2  s . c  om*/
    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:us.shandian.blacklight.ui.common.SwipeUpAndDownRefreshLayout.java

@Override
public void draw(Canvas canvas) {
    Object progressBar = null;/*from   w w w.  ja va2  s  .c  o m*/

    try {
        progressBar = mProgressBar.get(this);
    } catch (Exception e) {

    }

    Method m = mSetBounds;

    if (m == null && progressBar != null) {
        try {
            m = progressBar.getClass().getDeclaredMethod("setBounds", int.class, int.class, int.class,
                    int.class);
            m.setAccessible(true);
        } catch (Exception e) {

        }
    }

    if (m != null) {
        mSetBounds = m;

        try {
            m.invoke(progressBar, 0, 0, 0, 0);
        } catch (Exception e) {

        }
    }

    super.draw(canvas);

    if (m != null) {
        Paint p = new Paint();
        p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        mCanvas.drawPaint(p);
        try {
            m.invoke(progressBar, 0, 0, mWidth, mProgressBarHeight);

            Method method = mDraw;

            if (method == null) {
                method = progressBar.getClass().getDeclaredMethod("draw", Canvas.class);
                method.setAccessible(true);
            }

            mDraw = method;
            method.invoke(progressBar, mCanvas);
        } catch (Exception e) {

        }

        canvas.drawBitmap(mBitmap, 0, isDown() ? mHeight - mProgressBarHeight : 0, null);
    }
}

From source file:com.sebible.cordova.videosnapshot.VideoSnapshot.java

private void drawTimestamp(Bitmap bm, String prefix, long timeMs, int textSize) {
    float w = bm.getWidth(), h = bm.getHeight();
    float size = (float) (textSize * bm.getWidth()) / 1280;
    float margin = (float) (w < h ? w : h) * 0.05f;

    Canvas c = new Canvas(bm);
    Paint p = new Paint();
    p.setColor(Color.WHITE);//from  w  w w.  j  av a2s .  c o  m
    p.setStrokeWidth((int) (size / 10));
    p.setTextSize((int) size); // Text Size
    p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)); // Text Overlapping Pattern

    long second = (timeMs / 1000) % 60;
    long minute = (timeMs / (1000 * 60)) % 60;
    long hour = (timeMs / (1000 * 60 * 60)) % 24;

    String text = String.format("%s %02d:%02d:%02d", prefix, hour, minute, second);
    Rect r = new Rect();
    p.getTextBounds(text, 0, text.length(), r);
    //c.drawBitmap(originalBitmap, 0, 0, paint);
    c.drawText(text, bm.getWidth() - r.width() - margin, bm.getHeight() - r.height() - margin, p);
}

From source file:com.appbase.androidquery.callback.BitmapAjaxCallback.java

private static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {

    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);
    final float roundPx = pixels;

    paint.setAntiAlias(true);/*www.j a v  a 2  s . c  om*/
    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.atwal.wakeup.battery.util.Utilities.java

public static Bitmap composeIcon(Drawable icon, Drawable maskIcon, int width, int height) {
    Bitmap app_mask_icon_scaled = Bitmap.createScaledBitmap(((BitmapDrawable) maskIcon).getBitmap(), width,
            height, false);/*from   www  .j ava  2  s.c om*/
    Bitmap icon_scaled = Bitmap.createScaledBitmap(((BitmapDrawable) icon).getBitmap(), width, height, false);
    Paint paint = new Paint();
    PorterDuffXfermode porterDuffXfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    canvas.drawBitmap(app_mask_icon_scaled, 0, 0, paint);
    paint.setXfermode(porterDuffXfermode);
    canvas.drawBitmap(icon_scaled, 0, 0, paint);
    canvas.setBitmap(null);
    return bitmap;
}