如何使用 canvas 在位图上绘制图案

How to Draw a Pattern on Bitmap using canvas

我只想知道有没有办法在位图上绘制图案,something like this. For example, I want to draw a pattern of this over my bitmap.

选项 1:透明前景

如果您遵循描述的过程 here to merge the two images, you can draw one image onto another. Simply change the opacity of the top-most image as described here。考虑以下示例:

Bitmap bitmap = Bitmap.createBitmap(/* width */, /* height */, Config.ARGB_8888);
Paint alpha = new Paint();
alpha.setAlpha(/* alpha */);
Canvas canvas = new Canvas(bitmap);
Bitmap background = BitmapFactory.decodeResource(
    getResources(), R.drawable.background);
Bitmap foreground = BitmapFactory.decodeResource(
    getResources(), R.drawable.pattern);
canvas.drawBitmap(background, /* xPos */, /* yPos */, null);
canvas.drawBitmap(foreground, /* xPos */, /* yPos */, alpha);

选项 2:前景遮住背景

或者,如果您试图屏蔽图像,您可以考虑实施找到的解决方案 here

你绝对可以设置 bitmap.And 在 it.Once 之上创建 canvas canvas 在位图上创建,你绝对可以在其上绘制自定义图案。

您可以在此处查看如何绘制文本的完整指南:

https://www.skoumal.net/en/android-how-draw-text-bitmap/

长话短说:

复制您的位图以使其可变并基于它创建 Canvas。