Android PorterDuffXfermode 和 BitmapShader,实现溶图遮罩效果

本文介绍如何使用PorterDuffXfermode和BitmapShader实现图形合成,包括将图片溶合成特定形状和利用文字作为遮罩进行图形处理的技术细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


Demo

https://gitee.com/olleh/MyShader.git

对照表中先绘制的是dst遮罩

在这里插入图片描述

PorterDuffXfermode实现溶图效果

将一个美女图片溶图成五角星的形状
在这里插入图片描述

 private Bitmap srcBitmap;
    private Bitmap dstBitmap;
    private Paint paint;


    private void init() {
        srcBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.women);
        dstBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.shape);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(dstBitmap, null, new RectF(0, 0, canvas.getWidth(), canvas.getHeight()), null);
        canvas.drawBitmap(srcBitmap, null, new RectF(0, 0, canvas.getWidth(), canvas.getHeight()), paint);
    }

PorterDuffXfermode实现文字遮罩

这里文字就相当于是形状
在这里插入图片描述

public class MyShapeTextView extends android.support.v7.widget.AppCompatTextView {
    public MyShapeTextView(Context context) {
        this(context, null);
    }

    public MyShapeTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyShapeTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private Bitmap dstBitmap;
    private Paint paint;


    private void init() {
        dstBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.gradient_a3_nr1014);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setTextSize(200);
        paint.setColor(Color.BLACK);
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawTextWithCenterPoint(canvas, canvas.getWidth() / 2, canvas.getHeight() / 2, "你好啊", paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(dstBitmap, null, new RectF(0, 0, canvas.getWidth(), canvas.getHeight()), paint);
        paint.setXfermode(null);
    }

    /**
     * 以中心点绘制文字
     *
     * @param canvas
     * @param centerX
     * @param centerY
     * @param text
     * @param paint
     */
    private void drawTextWithCenterPoint(Canvas canvas, int centerX, int centerY, String text, Paint paint) {
        //获取文本的宽度,但是是一个比较粗略的结果
        float textWidth = paint.measureText(text);
        //文字度量
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        //得到基线的位置
        float baselineY = centerY + (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
        //绘制
        canvas.drawText(text, centerX - textWidth / 2, baselineY, paint);
    }
}

使用BitmapShader实现文字遮罩

在这里插入图片描述

public class MyShapeTextView2 extends android.support.v7.widget.AppCompatTextView {
    public MyShapeTextView2(Context context) {
        this(context, null);
    }

    public MyShapeTextView2(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyShapeTextView2(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private Bitmap dstBitmap;
    private Paint paint;


    private void init() {
        dstBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.gradient_a3_nr1014);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setTextSize(200);
        paint.setColor(Color.BLACK);
        paint.setShader(new BitmapShader(dstBitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT));
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawTextWithCenterPoint(canvas, canvas.getWidth() / 2, canvas.getHeight() / 2, "你好啊222", paint);
    }

    /**
     * 以中心点绘制文字
     *
     * @param canvas
     * @param centerX
     * @param centerY
     * @param text
     * @param paint
     */
    private void drawTextWithCenterPoint(Canvas canvas, int centerX, int centerY, String text, Paint paint) {
        //获取文本的宽度,但是是一个比较粗略的结果
        float textWidth = paint.measureText(text);
        //文字度量
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        //得到基线的位置
        float baselineY = centerY + (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
        //绘制
        canvas.drawText(text, centerX - textWidth / 2, baselineY, paint);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值