【问题标题】:Android - How to apply gradient color to bitmap?Android - 如何将渐变颜色应用于位图?
【发布时间】:2018-09-11 20:59:25
【问题描述】:

我需要将渐变颜色动态应用到将在另一个位图上绘制的位图(它看起来像带有一些透明部分的划痕):this is the result i need

这是我的代码:

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);

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

【问题讨论】:

    标签: android colors bitmap gradient


    【解决方案1】:

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

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

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

    【讨论】:

    • 我尝试设置明确的高度和十六进制颜色,但结果没有改变。但是,如果我用 canvas.drawRect(0, 0, x, y, paint) 绘制一个矩形,它的颜色是正确的。我真的不明白为什么它不适用于drawBitmap。
    猜你喜欢
    • 2022-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多