【发布时间】:2023-04-10 00:11:01
【问题描述】:
我正在尝试使用 Android 中的 Path 绘制心形画布。代码如下:
@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);
}
我可以毫无问题地绘制心脏,并且可以使用 Paint 中的 Fill 选项在心脏内部填充颜色。但是我应该可以根据一些数据动态填满心脏,不能一直填满。我目前取得的成果如下:
我进行了广泛的搜索,发现了很多类似的事情。其中一些包括:
我还遇到了将画布转换为位图并使用Flood Fill Algorithm 在位图中填充颜色的概念,它允许用户在位图中填充颜色。但是,我不希望位图在触摸心脏内部时填充颜色,而是在按钮单击动作时填充。
我以为filling a circle gradually from bottom to top android 会帮助我,但它使用了一个圆圈,而且我对 Canvas 并不精通,这使我在将圆圈代码适应这种形状方面非常薄弱。
如果有人对如何实现这一点有一些想法或任何见解,那将非常有帮助。干杯。提前致谢。
P.S : 我还尝试了一些在 Paint 中使用 setShader 的技巧,但没有什么能满足我的需求。
编辑:
我偶然发现了一个想法,用另一种与画布背景相同的颜色在心脏上绘制一个矩形,这样它看起来就像是半填充的!我仍在研究这个想法,不确定这对我来说有多准确。如果有人有更好的想法,欢迎您。
【问题讨论】:
-
"But I should be able to fill the heart dynamically according to some data and it cannot be filled fully all the time."在实践中意味着什么? -
就像心脏必须填充 10% 或 20% 取决于用户获得的点数。
-
我使用了你提到的在心形填充上绘制一个矩形,但在心形边框后面。对我来说效果很好。
标签: android bitmap android-canvas