Java Code Examples for android.graphics.Paint.setXfermode()

The following are Jave code examples for showing how to use setXfermode() of the android.graphics.Paint class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: Pocket-Plays-for-Twitch   File: Service.java   View Source Code Vote up 6 votes
/**
 * Creates a bitmap with rounded corners.
 * @param bitmap The bitmap
 * @param i the corner radius in pixels
 * @return The bitmap with rounded corners
 */
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int i) {
	if (bitmap == null) {
		return Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
	}

	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) i, (float) i, paint);

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

	return output;
}
 
Example 2
Project: ChatExchange-old   File: SpotlightView.java   View Source Code Vote up 6 votes
private void init(Context context) {
    setWillNotDraw(false);
    setVisibility(INVISIBLE);

    lineStroke = Utils.dpToPx(4);
    isReady = false;
    isRevealAnimationEnabled = true;
    dismissOnTouch = false;
    isPerformClick = false;
    enableDismissAfterShown = false;
    dismissOnBackPress = false;
    handler = new Handler();
    preferencesManager = new PreferencesManager(context);
    eraser = new Paint();
    eraser.setColor(0xFFFFFFFF);
    eraser.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    eraser.setFlags(Paint.ANTI_ALIAS_FLAG);
}
 
Example 3
Project: chat-sdk-android-push-firebase   File: ChatBubbleImageView.java   View Source Code Vote up 6 votes
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float pixels) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
            .getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    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(0xff424242);

    canvas.drawRoundRect(rectF, pixels, pixels, paint);

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

    return output;
}
 
Example 4
Project: LQRWeChat   File: ImageUtils.java   View Source Code Vote up 6 votes
/**
 * @param x              图像的宽度
 * @param y              图像的高度
 * @param image          源图片
 * @param outerRadiusRat 圆角的大小
 * @return 圆角图片
 */
public static Bitmap createFramedPhoto(int x, int y, Bitmap image, float outerRadiusRat) {
    // 根据源文件新建一个darwable对象
    Drawable imageDrawable = new BitmapDrawable(image);

    // 新建一个新的输出图片
    Bitmap output = Bitmap.createBitmap(x, y, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    // 新建一个矩形
    RectF outerRect = new RectF(0, 0, x, y);

    // 产生一个红色的圆角矩形
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.RED);
    canvas.drawRoundRect(outerRect, outerRadiusRat, outerRadiusRat, paint);

    // 将源图片绘制到这个圆角矩形上
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    imageDrawable.setBounds(0, 0, x, y);
    canvas.saveLayer(outerRect, paint, Canvas.ALL_SAVE_FLAG);
    imageDrawable.draw(canvas);
    canvas.restore();

    return output;
}
 
Example 5
Project: Android-UtilCode   File: ImageUtils.java   View Source Code Vote up 6 votes
/**
 * 转为圆角图片
 *
 * @param src     源图片
 * @param radius  圆角的度数
 * @param recycle 是否回收
 * @return 圆角图片
 */
public static Bitmap toRoundCorner(Bitmap src, float radius, boolean recycle) {
    if (null == src) return null;
    int width = src.getWidth();
    int height = src.getHeight();
    Bitmap ret = Bitmap.createBitmap(width, height, src.getConfig());
    Paint paint = new Paint();
    Canvas canvas = new Canvas(ret);
    Rect rect = new Rect(0, 0, width, height);
    paint.setAntiAlias(true);
    canvas.drawRoundRect(new RectF(rect), radius, radius, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(src, rect, rect, paint);
    if (recycle && !src.isRecycled()) src.recycle();
    return ret;
}
 
Example 6
Project: AndroidBasicLibs   File: ImageUtils.java   View Source Code Vote up 6 votes
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
    if (bitmap == null) {
        return null;
    }
    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, roundPx, roundPx, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
}
 
Example 7
Project: Amazing   File: ScanView.java   View Source Code Vote up 6 votes
private void init(Context context, AttributeSet attrs) {
	mask = BitmapFactory.decodeResource(context.getResources(), R.drawable.face_mask);
	rect = BitmapFactory.decodeResource(context.getResources(), R.drawable.face_rect);
	leftScan = BitmapFactory.decodeResource(context.getResources(), R.drawable.up_left_scan);
	rightScan = BitmapFactory.decodeResource(context.getResources(), R.drawable.up_right_scan);
	faceW = mask.getWidth();
	faceH = mask.getHeight();
	paint = new Paint();
	paint.setAntiAlias(true);
	paint.setColor(Color.parseColor("#4C0C0F3C"));
	paint1 = new Paint();
	paint1.setAntiAlias(true);
	paint1.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
	paint2 = new Paint();
	paint2.setAntiAlias(true);
	paint2.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
	paint3 = new Paint();
	paint3.setAntiAlias(true);
	paint3.setColor(Color.parseColor("#000000"));
}
 
Example 8
Project: Android-UtilCode   File: ImageUtils.java   View Source Code Vote up 6 votes
/**
 * 转为圆形图片
 *
 * @param src     源图片
 * @param recycle 是否回收
 * @return 圆形图片
 */
public static Bitmap toRound(Bitmap src, boolean recycle) {
    if (isEmptyBitmap(src)) return null;
    int width = src.getWidth();
    int height = src.getHeight();
    int radius = Math.min(width, height) >> 1;
    Bitmap ret = Bitmap.createBitmap(width, height, src.getConfig());
    Paint paint = new Paint();
    Canvas canvas = new Canvas(ret);
    Rect rect = new Rect(0, 0, width, height);
    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    canvas.drawCircle(width >> 1, height >> 1, radius, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(src, rect, rect, paint);
    if (recycle && !src.isRecycled()) src.recycle();
    return ret;
}
 
Example 9
Project: jackknife   File: ImageUtils.java   View Source Code Vote up 6 votes
public static Bitmap makeRoundCornerBitmap(Bitmap bitmap, int pixels, int color) {
    Bitmap outputBitmap = Bitmap.createBitmap(bitmap.getWidth(),
            bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(outputBitmap);
    Paint paint = new Paint();
    Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    RectF rectF = new RectF(rect);
    float roundPx = pixels;
    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);
    return outputBitmap;
}
 
Example 10
Project: FirebasePost   File: SendingProgressView.java   View Source Code Vote up 5 votes
private void setupDonePaints() {
    doneBgPaint = new Paint();
    doneBgPaint.setAntiAlias(true);
    doneBgPaint.setStyle(Paint.Style.FILL);
    doneBgPaint.setColor(0xff39cb72);

    checkmarkPaint = new Paint();

    maskPaint = new Paint();
    maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
}
 
Example 11
Project: showroom-android   File: DecodeBitmapTask.java   View Source Code Vote up 5 votes
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float pixels, int width, int height) {
    final Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(output);

    final int sourceWidth = bitmap.getWidth();
    final int sourceHeight = bitmap.getHeight();

    float xScale = (float) width / bitmap.getWidth();
    float yScale = (float) height / bitmap.getHeight();
    float scale = Math.max(xScale, yScale);

    float scaledWidth = scale * sourceWidth;
    float scaledHeight = scale * sourceHeight;

    float left = (width - scaledWidth) / 2;
    float top = (height - scaledHeight) / 2;

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, width, height);
    final RectF rectF = new RectF(rect);

    final RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);

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

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

    return output;
}
 
Example 12
Project: Musicoco   File: BitmapUtils.java   View Source Code Vote up 5 votes
/**
 * 生成透明背景的圆形图片
 */
public static Bitmap getCircleBitmap(Bitmap bitmap) {
    if (bitmap == null) {
        return null;
    }
    try {
        Bitmap circleBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(circleBitmap);

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

        float radius = Math.min(bitmap.getWidth(), bitmap.getHeight()) / 2.0f;

        //绘制透明底
        canvas.drawARGB(0, 0, 0, 0);

        //绘制背景圆
        paint.setColor(Color.WHITE);
        canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, radius, paint);

        //取两层绘制交集。显示上层。
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

        //绘制图片并取交集,显示上层
        //取中间区域绘制
        int left = (int) ((bitmap.getWidth() - radius * 2) / 2);
        int top = (int) ((bitmap.getHeight() - radius * 2) / 2);
        canvas.drawBitmap(bitmap, -left, -top, paint);

        bitmap.recycle();
        bitmap = null;

        return circleBitmap;

    } catch (Exception e) {
        return bitmap;
    }
}
 
Example 13
Project: andcoachmark   File: CircleView.java   View Source Code Vote up 5 votes
@Override
protected void dispatchDraw(Canvas canvas) {
    Paint paint = new Paint();
    paint.setColor(defaultColor);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
    canvas.drawCircle(mCenterX, mCenterY, mRadius, paint);
    super.dispatchDraw(canvas);
}
 
Example 14
Project: iosched-reader   File: BezelImageView.java   View Source Code Vote up 5 votes
public BezelImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    // Attribute initialization.
    final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BezelImageView,
            defStyle, 0);

    mMaskDrawable = a.getDrawable(R.styleable.BezelImageView_maskDrawable);
    if (mMaskDrawable != null) {
        mMaskDrawable.setCallback(this);
    }

    mBorderDrawable = a.getDrawable(R.styleable.BezelImageView_borderDrawable);
    if (mBorderDrawable != null) {
        mBorderDrawable.setCallback(this);
    }

    mDesaturateOnPress = a.getBoolean(R.styleable.BezelImageView_desaturateOnPress,
            mDesaturateOnPress);

    a.recycle();

    // Other initialization.
    mBlackPaint = new Paint();
    mBlackPaint.setColor(0xff000000);
    
    mMaskedPaint = new Paint();
    mMaskedPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

    // Always want a cache allocated.
    mCacheBitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);

    if (mDesaturateOnPress) {
        // Create a desaturate color filter for pressed state.
        ColorMatrix cm = new ColorMatrix();
        cm.setSaturation(0);
        mDesaturateColorFilter = new ColorMatrixColorFilter(cm);
    }
}
 
Example 15
Project: FlickLauncher   File: FolderIcon.java   View Source Code Vote up 5 votes
public void drawBackground(Canvas canvas, Paint paint) {
    canvas.save();
    canvas.translate(getOffsetX(), getOffsetY());

    paint.reset();
    if(Utilities.isAllowFolderTransparentPrefEnabled(Launcher.getLauncherActivity().getApplicationContext())){
        paint.setStyle(Paint.Style.STROKE);
    }else {
        paint.setStyle(Paint.Style.FILL);
    }
    paint.setXfermode(null);
    paint.setAntiAlias(true);

    int alpha = (int) Math.min(MAX_BG_OPACITY, BG_OPACITY * mColorMultiplier);
    if(Utilities.getFolderPreviewBackgroundPrefEnabled(Launcher.getLauncherActivity().getApplicationContext()) != -1){
        paint.setColor(Utilities.getFolderPreviewBackgroundPrefEnabled(Launcher.getLauncherActivity().getApplicationContext()));
    }else {
        paint.setColor(Color.argb(alpha, BG_INTENSITY, BG_INTENSITY, BG_INTENSITY));
    }
    float radius = getScaledRadius();

    canvas.drawCircle(radius, radius, radius, paint);
    canvas.clipPath(mClipPath, Region.Op.DIFFERENCE);

    paint.setStyle(Paint.Style.STROKE);
    paint.setColor(Color.TRANSPARENT);
    paint.setShadowLayer(mStrokeWidth, 0, mStrokeWidth, Color.argb(SHADOW_OPACITY, 0, 0, 0));
    canvas.drawCircle(radius, radius, radius, paint);

    canvas.restore();
}
 
Example 16
Project: BaseCore   File: ImageUtils.java   View Source Code Vote up 5 votes
/**
 * 添加倒影
 *
 * @param src              源图片的
 * @param reflectionHeight 倒影高度
 * @param recycle          是否回收
 * @return 带倒影图片
 */
public static Bitmap addReflection(Bitmap src, int reflectionHeight, boolean recycle) {
    if (isEmptyBitmap(src)) return null;
    // 原图与倒影之间的间距
    final int REFLECTION_GAP = 0;
    int srcWidth = src.getWidth();
    int srcHeight = src.getHeight();
    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);
    Bitmap reflectionBitmap = Bitmap.createBitmap(src, 0, srcHeight - reflectionHeight,
            srcWidth, reflectionHeight, matrix, false);
    Bitmap ret = Bitmap.createBitmap(srcWidth, srcHeight + reflectionHeight, src.getConfig());
    Canvas canvas = new Canvas(ret);
    canvas.drawBitmap(src, 0, 0, null);
    canvas.drawBitmap(reflectionBitmap, 0, srcHeight + REFLECTION_GAP, null);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    LinearGradient shader = new LinearGradient(0, srcHeight,
            0, ret.getHeight() + REFLECTION_GAP,
            0x70FFFFFF, 0x00FFFFFF, Shader.TileMode.MIRROR);
    paint.setShader(shader);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    canvas.drawRect(0, srcHeight + REFLECTION_GAP,
            srcWidth, ret.getHeight(), paint);
    if (!reflectionBitmap.isRecycled()) reflectionBitmap.recycle();
    if (recycle && !src.isRecycled()) src.recycle();
    return ret;
}
 
Example 17
Project: AppCommonFrame   File: BitmapUtil.java   View Source Code Vote up 4 votes
/**
 * 获得带倒影的图片 . <br>
 * @author liulongzhenhai 2012-8-1 下午5:25:37 <br>
 * @param bitmap 图片对象
 * @return 图片
 */
public static Bitmap createReflectionImageWithOrigin(final Bitmap bitmap) {
	final int reflectionGap = 4;
	final int w = bitmap.getWidth();
	final int h = bitmap.getHeight();

	final Matrix matrix = new Matrix();
	matrix.preScale(1, -1);

	final Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, h / 2, w, h / 2, matrix, false);

	final Bitmap bitmapWithReflection = Bitmap.createBitmap(w, h + h / 2, Config.ARGB_8888);

	final Canvas canvas = new Canvas(bitmapWithReflection);
	canvas.drawBitmap(bitmap, 0, 0, null);
	final Paint deafalutPaint = new Paint();
	canvas.drawRect(0, h, w, h + reflectionGap, deafalutPaint);

	canvas.drawBitmap(reflectionImage, 0, h + reflectionGap, null);

	final Paint paint = new Paint();
	final 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, h, w, bitmapWithReflection.getHeight() + reflectionGap, paint);

	return bitmapWithReflection;
}
 
Example 18
Project: TakePhoto   File: FileUtil.java   View Source Code Vote up 4 votes
/**
 * 将图片转化为圆形头像 
 * @Title: toRoundBitmap
 * @throws
 */
public static Bitmap toRoundBitmap(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;

		left = 0;
		top = 0;
		right = width;
		bottom = 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 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); // 填充整个Canvas

	// 以下有两种方法画圆,drawRounRect和drawCircle
	canvas.drawRoundRect(rectF, roundPx, roundPx, paint);// 画圆角矩形,第一个参数为图形显示区域,第二个参数和第三个参数分别是水平圆角半径和垂直圆角半径。
	// canvas.drawCircle(roundPx, roundPx, roundPx, paint);

	paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));// 设置两张图片相交时的模式,参考http://trylovecatch.iteye.com/blog/1189452
	canvas.drawBitmap(bitmap, src, dst, paint); // 以Mode.SRC_IN模式合并bitmap和已经draw了的Circle

	return output;
}
 
Example 19
Project: screenfilter   File: ShaderPainter.java   View Source Code Vote up 4 votes
public ShaderPainter() {
    maskPaint = new Paint();
    maskPaint.setColor(Color.TRANSPARENT);
    maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    maskPaint.setStyle(Paint.Style.FILL);
}
 
Example 20
Project: FancyShowCaseView-master   File: Utils.java   View Source Code Vote up 3 votes
/**
 * Draws focus circle
 *
 * @param bitmap bitmap to draw
 * @param point  circle point
 * @param radius circle radius
 */
static void drawFocusCircle(Bitmap bitmap, int[] point, int radius) {
    Paint p = new Paint();
    p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    Canvas c = new Canvas(bitmap);
    c.drawCircle(point[0], point[1], radius, p);
}