如何应用动态阿尔法面具在Android文本 [英] How to apply dynamic alpha mask to a Text on Android

查看:216
本文介绍了如何应用动态阿尔法面具在Android文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望具有可绘制形状为圆形或任何一个动态阿尔法口罩,并将其应用到Android上drawed文本。 下面是我想要一个例子:

I want to make a dynamic alpha mask with drawable shapes as circles or whatever, and apply it to a drawed text on Android. Here is an example of what i want :

我想,使其与 setXfermode(新PorterDuffXfermode(Mode.SRC_IN)),但我无法得到它的工作。 这里是code我在的OnDraw(帆布油画)方法:

I am trying to make it with setXfermode(new PorterDuffXfermode(Mode.SRC_IN)), but i can't get it work. Here is the code I have in onDraw(Canvas canvas) method :

Paint paint = new Paint();
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(Color.WHITE);
canvas.drawCircle(50, 50, 50, paint);

paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
paint.setColor(Color.RED);
canvas.drawText("hello", 0, 50, paint);

在此先感谢您的帮助

Thanks in advance for your help

推荐答案

尝试单独创建源和掩模位图。大多数我见过的例子涉及使用两个位图,并使用drawBitmap进行屏蔽。

Try creating your source and mask bitmaps separately. Most of the examples I have seen involve using two bitmaps and using drawBitmap to perform the masking.

我使用PorterDuff.Mode.DST_IN供漆,然后绘制源图像(无漆),随后用掩模图像(与涂料)。事情是这样的:

I use PorterDuff.Mode.DST_IN for the paint, then draw the source image (with no paint) followed by the mask image (with the paint). Something like this:

        Bitmap bitmapOut = Bitmap.createBitmap(sizeX, sizeY,
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmapOut);

        Paint xferPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        xferPaint.setColor(Color.BLACK);

        xferPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));

        canvas.drawBitmap(sourceImage, 0, 0, null);
        canvas.drawBitmap(alphaMask, 0, 0, xferPaint);

在这一点上,bitmapOut包含我的蒙面形象。

At this point, bitmapOut contains my masked image.

这篇关于如何应用动态阿尔法面具在Android文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆