I used the clipPath function available in Canvas to achieve what I need. I draw the heart with the method above and draw a rectangle above it, and I use the clipPath function to cut out the area that is outside the heart.
public static double filled_amount = .90; path.moveTo(left_x_moveto, left_y_moveto); path.cubicTo(left_x1, left_y1, left_x2, left_y2, left_x3, left_y3); path.cubicTo(right_x2, right_y2, right_x1, right_y1, left_x_moveto, left_y_moveto); path.close(); Rect rect = new Rect((int)(canvas.getWidth()*.10),(int)(canvas.getHeight()*filled_amount),(int) canvas.getWidth(), (int) canvas.getHeight()); canvas.clipPath(path); paint.setColor(Color.WHITE); paint.setStyle(Paint.Style.FILL); canvas.drawPath(path, paint); canvas.drawRect(rect, rect_paint); heart_outline_paint.setColor(getResources().getColor(R.color.heart_outline_color));
This will give me the desired result of heart filling. Changing the dynamic value of filled_amount and calling invalidate() will make it look like the heart is being filled dynamically.
@ Henry's answer may be the best, but it did the trick for me, and I donβt look deep into the edges, so a few zigzags here and there everything is in order.
source share