android - 带有透明文本的 Canvas

标签 android canvas

我正在尝试执行您在图片中看到的以下操作,但我想要一个白色方 block 而不是那个黑色方 block :

enter image description here

到目前为止,这是我的代码:

public class SmallWhiteThing extends View {

Context context;

Paint paint = new Paint();

// CONSTRUCTOR
public SmallWhiteThing(Context context) {
    super(context);
    setFocusable(true);
}

public SmallWhiteThing(Context context, AttributeSet attrs)
{
    super(context, attrs);
    this.context = context;
}

public SmallWhiteThing(Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);
    this.context = context;
}

@Override
protected void onDraw(Canvas canvas) {

    Paint paint = new Paint();
    BitmapFactory.Options options = new BitmapFactory.Options();  
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;

    Bitmap b = Bitmap.createBitmap(120, 120, Bitmap.Config.ALPHA_8);
    Canvas c = new Canvas(b);   
    c.drawColor(Color.WHITE);

    paint.setStrokeWidth(0);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    paint.setTextSize(40);
    paint.setAntiAlias(true);

    c.drawText("Hello", 30, 30, paint);

    canvas.drawBitmap(b, 140, 270, paint);
}

我试过了,你可以看到这个: c.drawColor(Color.WHITE); 但没有任何运气。

非常感谢您的提示。

我正在尝试别的东西,我得到了这个: enter image description here

代码:

Bitmap b = Bitmap.createBitmap(120, 120, Bitmap.Config.ALPHA_8);

    Canvas c = new Canvas(b);   
    c.drawColor(Color.WHITE);
    paint.setColor(Color.WHITE);
    paint.setStrokeWidth(0f);
    c.drawRect(0, 0, 150, 150, paint);

    canvas.drawBitmap(b, 100, 100, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
    paint.setTextSize(40);
    paint.setAntiAlias(true);
    paint.setColor(Color.WHITE);
    canvas.drawText("Helloo", 100, 200, paint);

最佳答案

对于需要它的人。这是有效的:

当前用法(在屏幕中填充 ImageView ):

//Params: Text, textSize
createBlabla("Text to show", 35);


public void createBlabla(String text, int fontSize){        
    int paddingRight = 10;
    int paddingLeft = 5;
    int paddingBottom = 5;

    //Paint config
    Paint paint = new Paint();
    paint.setTextSize(fontSize);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
    paint.setAntiAlias(true);

    Bitmap largeWhiteBitmap = Bitmap.createBitmap((int) paint.measureText(text) + paddingRight, fontSize + paddingRight, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(largeWhiteBitmap);
    canvas.drawColor(Color.WHITE); 

    canvas.drawText(text, paddingLeft, fontSize, paint);

    ImageView imv = (ImageView)MainActivity.this.findViewById(R.id.imageView1);
    imv.setImageBitmap(largeWhiteBitmap);
}

enter image description here

请注意,您在屏幕上看到的是一个完整的相对布局,中间有一个 imageView。此 ImageView 使用前面的代码获取位图作为图像。 relativeLayout 有绿色背景。

关于android - 带有透明文本的 Canvas ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19911601/

相关文章:

android - 使用 Google App Engine 和 Firebase Cloud Messaging 进行通知

android - 使用 -no-window 在模拟器中运行时测试检测进程崩溃

Javascript Canvas 复杂性

javascript - 无法将一个 Canvas 放在另一个 Canvas 下

javascript - 当 image.src 有时间标签时更新 Canvas 图像

java - 解析 iso8583 消息时出现问题

安卓 : Support MapFragment Returns null

android - 在实际手机上编译 Android 应用程序

java - Canvas 和图形上下文 - 如何更改画笔的形状

html - 在 HTML5 Canvas 上模仿 photoshop/painter 平滑绘制?