0

I have view in which, I want to display a heart that is filled based on percentage shown in middle of heart.

I tried canvas, was able to draw heart but could not partial fill, used cliprect,etc but could not achieve this, please see Image

enter image description here

Now if its 30 %, then only 30% area from bottom should be filled with redcolor rest should be white, also the color outside of heart is blue which needs to be same way always as shown in image.

       int percentage = 85;
        int w = 100;
        int h = 100;
        Bitmap bitmap = Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        Path path = new Path();
        // Fill the canvas with background color
        canvas.drawColor(Color.BLUE);
        paint.setShader(null);

        float width = 100.00f;
        float height =100.00f;
        // Starting point
        path.moveTo(width / 2, height / 5);

        // Upper left path
        path.cubicTo(5 * width / 14, 0,
                0, height / 15,
                width / 28, 2 * height / 5);

        // Lower left path
        path.cubicTo(width / 14, 2 * height / 3,
                3 * width / 7, 5 * height / 6,
                width / 2, height);

        // Lower right path
        path.cubicTo(4 * width / 7, 5 * height / 6,
                13 * width / 14, 2 * height / 3,
                27 * width / 28, 2 * height / 5);

        // Upper right path
        path.cubicTo(width, height / 15,
                9 * width / 14, 0,
                width / 2, height / 5);

        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.FILL);


        // draw text percentage
        Paint textPaint = new Paint();
        textPaint.setColor(Color.BLACK);
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setTextSize(12);
        int xPos = (canvas.getWidth() / 2);
        int yPos = (int) ((canvas.getHeight() / 2) - ((textPaint.descent() +    textPaint.ascent()) / 2)) ;
        //((textPaint.descent() + textPaint.ascent()) / 2) is the distance from the baseline to the center.
        canvas.drawText(percentage+" % ", xPos, yPos, textPaint);
        int heightOfClip = 100 - percentage;
        //clip
        canvas.clipRect(0,0,100,heightOfClip, Region.Op.XOR);
        // draw heart and color it
        canvas.drawPath(path, paint);
        // draw text percentage
        canvas.drawText(percentage+"%", xPos, yPos, textPaint);

        mSampleIv.setImageBitmap(bitmap);
geniushkg
  • 706
  • 9
  • 21
  • 2
    I just came across this one the other day, if using predefined images is an option. http://stackoverflow.com/questions/40522120/using-canvas-and-bitmap-in-android-how-to-get-this-image – Mike M. Mar 21 '17 at 13:00
  • @MikeM. thank you very much I was stucked into this since a day. – geniushkg Mar 21 '17 at 13:02
  • 1
    May be you can look into the source code of this library ... https://github.com/tangqi92/WaveLoadingView – Sreehari Mar 21 '17 at 13:07

0 Answers0