Android - 用颜色部分填充路径
Android - Fill Path with color partially
我正在尝试使用 Android 中的路径绘制心形 Canvas。代码如下:
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// Fill the canvas with background color
canvas.drawColor(Color.WHITE);
// paint.setShader(null);
// Defining of the heart path starts
path.moveTo(left + WIDTH / 2, top + HEIGHT / 4); // Starting point
// Create a cubic Bezier cubic left path
path.cubicTo(left+WIDTH/5,top,
left+WIDTH/4,top+4*HEIGHT/5,
left+WIDTH/2, top+HEIGHT);
// This is right Bezier cubic path
path.cubicTo(left + 3 * WIDTH / 4, top + 4 * HEIGHT / 5,
left + 4 * WIDTH / 5, top,
left + WIDTH / 2, top + HEIGHT / 4);
paint.setShader(new LinearGradient(0, canvas.getHeight()/4, canvas.getWidth(), canvas.getHeight()/4, new int[]{Color.RED, Color.YELLOW, Color.GREEN}, new float[]{0, 0.6f, 1}, Shader.TileMode.CLAMP));
canvas.drawPath(path, paint);
heart_outline_paint.setColor(getResources().getColor(R.color.heart_outline_color)); // Change the boundary color
heart_outline_paint.setStrokeWidth(4);
heart_outline_paint.setStyle(Paint.Style.STROKE);
canvas.drawPath(path, heart_outline_paint);
}
我可以毫无问题地画出心形,并且可以使用画图中的 Fill
选项在心形内部填充颜色。但是我应该可以根据一些数据动态地填充心脏,它不能一直充满。到目前为止我取得的成就如下:
我进行了广泛的搜索,发现了很多类似的东西。其中一些包括:
- Android fill in part of a path?
- filling a circle gradually from bottom to top android
我还遇到了将 canvas 转换为位图并使用 Flood Fill Algorithm 在位图中填充颜色的概念,它允许用户在位图中填充颜色。但是,我不希望位图在触摸心脏内部时填充颜色,而是在单击按钮时填充颜色。
我以为filling a circle gradually from bottom to top android
会帮助我,但它使用了一个圆圈,我不精通 Canvas 这让我很难将圆圈代码适应这种形状。
如果有人对如何实现这一目标有一些想法或任何见解,那将非常有帮助。干杯。提前致谢。
P.S : 我也在 Paint 中使用 setShader
尝试了一些技巧,但没有什么能给我想要的东西。
编辑:
我偶然想到用另一种颜色在心形上画一个矩形,颜色与 canvas 的背景相同,这样它看起来就像一半被填满了!!我仍在研究这个想法,不确定这对我来说有多准确。如果有人有更好的主意,不客气。
您可以使用 Bitmap Masking
获得部分填充的心形。您在这里理想的做法是使用一个位图来屏蔽另一个位图。
在您的情况下,您可以在 canvas 中有一个填充的矩形,然后在新位图中使用 heart
形状作为遮罩。然后,您可以通过更改背景矩形的高度来动态更改心形的填充。
参考这个:。这包含 部分填充星 的实现。思路是一样的。
我使用了 Canvas 中可用的 clipPath
函数来实现我需要的功能。我用上面的方法画了心形,然后在上面画了一个矩形,然后用clipPath
函数剪掉了心形以外的区域。
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)); // Change the boundary color
heart_outline_paint.setStrokeWidth(15);
heart_outline_paint.setStyle(Paint.Style.STROKE);
canvas.drawPath(path, heart_outline_paint);
这将给我动态填充心脏的预期结果。动态更改 filled_amount
的值并调用 invalidate()
将使心脏看起来像是在动态填充。
@Henry 的回答可能更好,但这对我有用,我没有深入观察边缘,所以在这里和那里有点曲折就可以了。
我正在尝试使用 Android 中的路径绘制心形 Canvas。代码如下:
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// Fill the canvas with background color
canvas.drawColor(Color.WHITE);
// paint.setShader(null);
// Defining of the heart path starts
path.moveTo(left + WIDTH / 2, top + HEIGHT / 4); // Starting point
// Create a cubic Bezier cubic left path
path.cubicTo(left+WIDTH/5,top,
left+WIDTH/4,top+4*HEIGHT/5,
left+WIDTH/2, top+HEIGHT);
// This is right Bezier cubic path
path.cubicTo(left + 3 * WIDTH / 4, top + 4 * HEIGHT / 5,
left + 4 * WIDTH / 5, top,
left + WIDTH / 2, top + HEIGHT / 4);
paint.setShader(new LinearGradient(0, canvas.getHeight()/4, canvas.getWidth(), canvas.getHeight()/4, new int[]{Color.RED, Color.YELLOW, Color.GREEN}, new float[]{0, 0.6f, 1}, Shader.TileMode.CLAMP));
canvas.drawPath(path, paint);
heart_outline_paint.setColor(getResources().getColor(R.color.heart_outline_color)); // Change the boundary color
heart_outline_paint.setStrokeWidth(4);
heart_outline_paint.setStyle(Paint.Style.STROKE);
canvas.drawPath(path, heart_outline_paint);
}
我可以毫无问题地画出心形,并且可以使用画图中的 Fill
选项在心形内部填充颜色。但是我应该可以根据一些数据动态地填充心脏,它不能一直充满。到目前为止我取得的成就如下:
我进行了广泛的搜索,发现了很多类似的东西。其中一些包括:
- Android fill in part of a path?
- filling a circle gradually from bottom to top android
我还遇到了将 canvas 转换为位图并使用 Flood Fill Algorithm 在位图中填充颜色的概念,它允许用户在位图中填充颜色。但是,我不希望位图在触摸心脏内部时填充颜色,而是在单击按钮时填充颜色。
我以为filling a circle gradually from bottom to top android 会帮助我,但它使用了一个圆圈,我不精通 Canvas 这让我很难将圆圈代码适应这种形状。
如果有人对如何实现这一目标有一些想法或任何见解,那将非常有帮助。干杯。提前致谢。
P.S : 我也在 Paint 中使用 setShader
尝试了一些技巧,但没有什么能给我想要的东西。
编辑:
我偶然想到用另一种颜色在心形上画一个矩形,颜色与 canvas 的背景相同,这样它看起来就像一半被填满了!!我仍在研究这个想法,不确定这对我来说有多准确。如果有人有更好的主意,不客气。
您可以使用 Bitmap Masking
获得部分填充的心形。您在这里理想的做法是使用一个位图来屏蔽另一个位图。
在您的情况下,您可以在 canvas 中有一个填充的矩形,然后在新位图中使用 heart
形状作为遮罩。然后,您可以通过更改背景矩形的高度来动态更改心形的填充。
参考这个:
我使用了 Canvas 中可用的 clipPath
函数来实现我需要的功能。我用上面的方法画了心形,然后在上面画了一个矩形,然后用clipPath
函数剪掉了心形以外的区域。
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)); // Change the boundary color
heart_outline_paint.setStrokeWidth(15);
heart_outline_paint.setStyle(Paint.Style.STROKE);
canvas.drawPath(path, heart_outline_paint);
这将给我动态填充心脏的预期结果。动态更改 filled_amount
的值并调用 invalidate()
将使心脏看起来像是在动态填充。
@Henry 的回答可能更好,但这对我有用,我没有深入观察边缘,所以在这里和那里有点曲折就可以了。