2

我正在尝试为我的 Android 绘画应用程序制作自定义画笔。我从 Michael 的代码(在此处找到)开始,我设法获得了 .png 画笔并将其用作位图并重绘它。它工作正常,但我不能改变颜色。尝试使用setcolorfilterandcolormatrixfilter但它似乎不起作用。任何人都知道我该怎么做?

private Bitmap mBitmapBrush;
   private Vector2 mBitmapBrushDimensions;
   private List<Vector2> mPositions = new ArrayList<Vector2>(100);
private Paint mPanit;


    public MyView(Context c) {
        super(c);

        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        mBitmapBrush = BitmapFactory.decodeResource(c.getResources(),R.drawable.brush1);
        mBitmapBrushDimensions = new Vector2(mBitmapBrush.getWidth(), mBitmapBrush.getHeight());

    }

 @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(0xFFAAAAAA);

        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
        for (Vector2 pos : mPositions) {

            canvas.drawBitmap(mBitmapBrush, pos.a, pos.b, mPanit);

        }



    invalidate();
    }

当我尝试使用 Colormatrixfilter 时,.set 函数出现错误。

4

1 回答 1

1

我遇到过同样的问题。为了更改位图颜色,您需要将颜色添加到您的绘画对象并将其应用到位图中。请参阅此处的工作示例,

  for (Vector2 pos : customBrushMap.get(p)) {
        Paint paint = new Paint();
        ColorFilter filter = new PorterDuffColorFilter(R.Color.GREEN, PorterDuff.Mode.SRC_IN);
        paint.setColorFilter(filter);
        canvas.drawBitmap(mBitmapBrush, pos.x, pos.y, paint);
}

结果,

在此处输入图像描述

于 2016-08-18T17:11:37.983 回答