安卓 - 如何将渐变颜色应用于位图?



我需要将渐变颜色动态应用于位图(它看起来像带有一些透明部分的划痕(,该位图将在另一个位图上绘制:这是我需要的结果。

这是我的代码:

Bitmap bitmapbackground = bitmaporiginal.copy(bitmaporiginal.getConfig(), true);
Bitmap bitmaptocolor = BitmapFactory.decodeResource(activity.getResources(), R.drawable.scratch);
LinearGradient gradient = new LinearGradient(0, 0, 0, bitmaptocolor.getHeight(), Color.parseColor("#D81B60"), Color.parseColor("#F48FB1"), Shader.TileMode.CLAMP);
Paint paint = new Paint();
paint.setShader(gradient);
Canvas canvas = new Canvas(bitmapbackground);
canvas.drawBitmap(bitmaptocolor, 0, 0, paint);

但通过这种方式,它不会将渐变颜色应用于划痕(它始终保持黑色(。我做错了什么?

确保bitmaptocolor.getHeight()实际上返回高度。

对颜色使用等效的十六进制。

LinearGradient gradient = new LinearGradient(0, 0, 0, bitmaptocolor.getHeight(), 0xD81B60, 0xD81B60, Shader.TileMode.CLAMP);

最新更新