user1472707
user1472707

Reputation: 51

Android Custom Brush Colors

I'm trying to make custom brushes for my Android painting app. I started with Michael's code (found here) and I have managed to get .png brushes and use that as a bitmap and redraw it. It works fine but I can't change the colour. Tried using the setcolorfilter and colormatrixfilter but it doesn't seem to be working. Anyone knows how I can do this?

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

When I tried using the Colormatrixfilter, the .set function was giving an error.

Upvotes: 2

Views: 2292

Answers (1)

Prokash Sarkar
Prokash Sarkar

Reputation: 11873

I had the same issue. In order to change the bitmap color you need to add color to your paint object and apply it into bitmap. See the working example here,

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

Result,

enter image description here

Upvotes: 1

Related Questions