Search in sources :

Example 41 with PorterDuffXfermode

use of android.graphics.PorterDuffXfermode in project LshUtils by SenhLinsh.

the class BitmapUtil method combineImagesToSameSize.

/**
     * 合并Bitmap到相同大小
     * @param bgd 后景Bitmap
     * @param fg 前景Bitmap
     * @return 合成后Bitmap
     */
public static Bitmap combineImagesToSameSize(Bitmap bgd, Bitmap fg) {
    Bitmap bmp;
    int width = bgd.getWidth() < fg.getWidth() ? bgd.getWidth() : fg.getWidth();
    int height = bgd.getHeight() < fg.getHeight() ? bgd.getHeight() : fg.getHeight();
    if (fg.getWidth() != width && fg.getHeight() != height) {
        fg = zoom(fg, width, height);
    }
    if (bgd.getWidth() != width && bgd.getHeight() != height) {
        bgd = zoom(bgd, width, height);
    }
    bmp = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Paint paint = new Paint();
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_ATOP));
    Canvas canvas = new Canvas(bmp);
    canvas.drawBitmap(bgd, 0, 0, null);
    canvas.drawBitmap(fg, 0, 0, paint);
    return bmp;
}
Also used : Bitmap(android.graphics.Bitmap) PorterDuffXfermode(android.graphics.PorterDuffXfermode) Canvas(android.graphics.Canvas) Paint(android.graphics.Paint) Paint(android.graphics.Paint)

Example 42 with PorterDuffXfermode

use of android.graphics.PorterDuffXfermode in project LshUtils by SenhLinsh.

the class BitmapUtil method getRoundBitmap.

/** 转换图片成圆形 */
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;
}
Also used : RectF(android.graphics.RectF) Bitmap(android.graphics.Bitmap) Rect(android.graphics.Rect) PorterDuffXfermode(android.graphics.PorterDuffXfermode) Canvas(android.graphics.Canvas) Paint(android.graphics.Paint) Paint(android.graphics.Paint)

Example 43 with PorterDuffXfermode

use of android.graphics.PorterDuffXfermode in project newsrob by marianokamp.

the class PreviewGenerator method getRoundedCornerBitmap.

private Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    canvas.drawARGB(0, 0, 0, 0);
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);
    final int color = 0xffffffff;
    final Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundedCornerRadiusPx, roundedCornerRadiusPx, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, 0, 0, paint);
    return output;
}
Also used : RectF(android.graphics.RectF) Bitmap(android.graphics.Bitmap) Rect(android.graphics.Rect) PorterDuffXfermode(android.graphics.PorterDuffXfermode) Canvas(android.graphics.Canvas) Paint(android.graphics.Paint) Paint(android.graphics.Paint)

Example 44 with PorterDuffXfermode

use of android.graphics.PorterDuffXfermode in project android-app-common-tasks by multidots.

the class Common method getRoundedCornerBitmap.

/**
     * Get Rounded cornered bitmap
     *
     * @param bitmap
     * @param roundPixels
     * @return
     */
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, @SuppressWarnings("SameParameterValue") int roundPixels) {
    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);
    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, (float) roundPixels, (float) roundPixels, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
}
Also used : RectF(android.graphics.RectF) Bitmap(android.graphics.Bitmap) Rect(android.graphics.Rect) PorterDuffXfermode(android.graphics.PorterDuffXfermode) Canvas(android.graphics.Canvas) Paint(android.graphics.Paint) Paint(android.graphics.Paint) SuppressLint(android.annotation.SuppressLint)

Example 45 with PorterDuffXfermode

use of android.graphics.PorterDuffXfermode in project Android-Bootstrap by Bearded-Hen.

the class BootstrapProgressBar method createRoundedBitmap.

/**
     * Creates a rounded bitmap with transparent corners, from a square bitmap.
     * See <a href="http://stackoverflow.com/questions/4028270">StackOverflow</a>
     *
     * @param bitmap       the original bitmap
     * @param cornerRadius the radius of the corners
     * @param roundRight if you should round the corners on the right, note that if set to true and cornerRadius == 0 it will create a square
     * @param roundLeft if you should round the corners on the right, note that if set to true and cornerRadius == 0 it will create a square
     * @return a rounded bitmap
     */
private static Bitmap createRoundedBitmap(Bitmap bitmap, float cornerRadius, boolean roundRight, boolean roundLeft) {
    Bitmap roundedBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), ARGB_8888);
    Canvas canvas = new Canvas(roundedBitmap);
    final Paint paint = new Paint();
    final Rect frame = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    //        final Rect frameLeft = new Rect(0, 0, bitmap.getWidth() /2, bitmap.getHeight());
    //        final Rect frameRight = new Rect(bitmap.getWidth() /2, bitmap.getHeight(), bitmap.getWidth(), bitmap.getHeight());
    final Rect leftRect = new Rect(0, 0, bitmap.getWidth() / 2, bitmap.getHeight());
    final Rect rightRect = new Rect(bitmap.getWidth() / 2, 0, bitmap.getWidth(), bitmap.getHeight());
    // prepare canvas for transfer
    paint.setAntiAlias(true);
    paint.setColor(0xFFFFFFFF);
    paint.setStyle(Paint.Style.FILL);
    canvas.drawARGB(0, 0, 0, 0);
    canvas.drawRoundRect(new RectF(frame), cornerRadius, cornerRadius, paint);
    if (!roundLeft) {
        canvas.drawRect(leftRect, paint);
    }
    if (!roundRight) {
        canvas.drawRect(rightRect, paint);
    }
    // draw bitmap
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, frame, frame, paint);
    return roundedBitmap;
}
Also used : RectF(android.graphics.RectF) Bitmap(android.graphics.Bitmap) Rect(android.graphics.Rect) PorterDuffXfermode(android.graphics.PorterDuffXfermode) Canvas(android.graphics.Canvas) Paint(android.graphics.Paint)

Aggregations

PorterDuffXfermode (android.graphics.PorterDuffXfermode)152 Paint (android.graphics.Paint)127 Canvas (android.graphics.Canvas)103 Bitmap (android.graphics.Bitmap)92 Rect (android.graphics.Rect)65 RectF (android.graphics.RectF)51 Matrix (android.graphics.Matrix)11 LinearGradient (android.graphics.LinearGradient)10 SuppressLint (android.annotation.SuppressLint)9 TypedArray (android.content.res.TypedArray)8 Point (android.graphics.Point)6 IOException (java.io.IOException)6 BitmapDrawable (android.graphics.drawable.BitmapDrawable)5 ColorMatrix (android.graphics.ColorMatrix)4 Path (android.graphics.Path)4 Resources (android.content.res.Resources)3 Drawable (android.graphics.drawable.Drawable)3 ResourceHelper (com.klinker.android.launcher.api.ResourceHelper)3 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)3 RadialGradient (android.graphics.RadialGradient)2