【问题标题】:How can i change the color of a picture that I got on canvas如何更改画布上的图片颜色
【发布时间】:2013-02-01 11:26:51
【问题描述】:

我有一个带有背景图片的画布,我可以在上面写字、画线、放置较小的图片、旋转、缩放等。 我需要更改其中一个位图的颜色。 我有一个看起来像这样的颜色选择器:

public void colorChanged(int color) {
    if (isText) {
        myView.setTextColor(color);
    } else if(isDrawing) {
        mPaint.setColor(color);
        myView.setPaint(mPaint);
    } else if(ispic) {
           //TODO
    }
}

我尝试了与我所拥有的 isText 部分代码类似的东西,但它只会改变我放置的行的颜色,或者移动我的图片(如果我不改变颜色,它当前是透明的)。

myView 是一个 CustomView,我有我的 onDraw 方法。

【问题讨论】:

  • 意思是你要改变位图背景色还是画布背景色?

标签: android colors bitmap android-canvas draw


【解决方案1】:

选项 1:

Paint p = new Paint(Color.RED);
ColorFilter filter = new LightingColorFilter(Color.RED, 1);
p.setColorFilter(filter);

然后用 Paint 对象绘制。

选项 2:

    Bitmap sourceBitmap = BitmapFactory.decodeFile(imgPath);
    float[] colorTransform = {
            0, 1f, 0, 0, 0, 
            0, 0, 0f, 0, 0,
            0, 0, 0, 0f, 0, 
            0, 0, 0, 1f, 0};

    ColorMatrix colorMatrix = new ColorMatrix();
    colorMatrix.setSaturation(0f); //Remove Colour 
    colorMatrix.set(colorTransform); //Apply Red say

    ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
    Paint paint = new Paint();
    paint.setColorFilter(colorFilter);   

    Display display = getWindowManager().getDefaultDisplay(); 

    Bitmap resultBitmap = Bitmap.createBitmap(sourceBitmap, 0, (int)(display.getHeight() * 0.15), display.getWidth(), (int)(display.getHeight() * 0.75));            


    Canvas canvas = new Canvas(resultBitmap);
    canvas.drawBitmap(resultBitmap, 0, 0, paint);

【讨论】:

  • 我从颜色选择器中获取颜色,我怎么知道如何制作矩阵,或者每种颜色都一样?
  • 你从你的颜色选择器中得到什么样的值??是十六进制还是别的什么?
【解决方案2】:

您可以在android中使用ColorMatrix更改位图的颜色。

访问this post 了解更多信息。 ColorMatrix here 的示例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-15
    • 2017-02-02
    • 1970-01-01
    • 2018-12-10
    • 2018-01-24
    • 2018-10-30
    • 2020-11-19
    相关资源
    最近更新 更多