Android自定义画笔颜色

时间:2012-06-27 11:30:46

标签: java android graphic brush colormatrixfilter

我正在尝试为我的Android绘画应用制作自定义画笔。我从迈克尔的代码(found here)开始,我设法得到.png画笔并将其用作位图并重新绘制。它工作正常,但我无法改变颜色。尝试使用setcolorfiltercolormatrixfilter,但它似乎不起作用。谁知道我怎么能这样做?

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函数发出错误。

1 个答案:

答案 0 :(得分: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);
}

结果,

enter image description here