- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在 Canvas 上画一个心形。我发现了不同的数学方程式,但我无法将它们中的任何一个转换成我可以在 onDraw 方法中实现的代码。我希望在这里有一颗类似于这种形状的心:
我追求的形状方程在哪里:
最佳答案
public class HeartShape extends FrameLayout {
private Paint paint;
public HeartShape(@NonNull Context context) {
super(context);
init();
}
private void init() {
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.RED);
paint.setStyle(Style.STROKE);
setWillNotDraw(false);
}
public HeartShape(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public HeartShape(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public HeartShape(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
@Override
protected void onDraw(Canvas canvas) {
Path path = createHeartPath(canvas.getClipBounds().right,canvas.getClipBounds().bottom);
canvas.drawPath(path,paint);
super.onDraw(canvas);
}
private Path createHeartPath(int width, int height) {
Path path = new Path();
path.moveTo(0,height/3f);
path.lineTo(width,height/3f);
path.moveTo(width/2f,0f);
path.lineTo(width/2f,height);
float pX = width/2f;
float pY = (height/100f)*33.33f;
float x1 = (width/100f)*50;
float y1 = (height/100f)*5;
float x2 = (width/100f)*90;
float y2 = (height/100f)*10;
float x3 = (width/100f)*90;
float y3 = (height/100f)*33.33f;
path.moveTo(pX,pY);
path.cubicTo(x1, y1, x2, y2, x3, y3);
path.moveTo(x3,pY);
x1 = (width/100f)*90;
y1 = (height/100f)*55f;
x2 = (width/100f)*65;
y2 = (height/100f)*60f;
x3 = (width/100f)*50;
y3 = (height/100f)*90f;
path.cubicTo(x1, y1, x2, y2, x3, y3);
// path.lineTo(pX,pY);
x1 = (width/100f)*50;
y1 = (height/100f)*5;
x2 = (width/100f)*10;
y2 = (height/100f)*10;
x3 = (width/100f)*10;
y3 = (height/100f)*33.33f;
path.moveTo(pX,pY);
path.cubicTo(x1, y1, x2, y2, x3, y3);
path.moveTo(x3,pY);
x1 = (width/100f)*10;
y1 = (height/100f)*55f;
x2 = (width/100f)*35f;
y2 = (height/100f)*60f;
x3 = (width/100f)*50f;
y3 = (height/100f)*90f;
path.cubicTo(x1, y1, x2, y2, x3, y3);
//path.lineTo(pX,pY);
path.moveTo(x3,y3);
path.close();
return path;
}
}
输出
移除画线代码并设置paint.setStyle(Style.FILL);
private Path createHeartPath(int width, int height) {
Path path = new Path();
//path.moveTo(0,height/3f);
//path.lineTo(width,height/3f);
//path.moveTo(width/2f,0f);
//path.lineTo(width/2f,height);
float pX = width/2f;
float pY = (height/100f)*33.33f;
float x1 = (width/100f)*50;
float y1 = (height/100f)*5;
float x2 = (width/100f)*90;
float y2 = (height/100f)*10;
float x3 = (width/100f)*90;
float y3 = (height/100f)*33.33f;
path.moveTo(pX,pY);
path.cubicTo(x1, y1, x2, y2, x3, y3);
path.moveTo(x3,pY);
x1 = (width/100f)*90;
y1 = (height/100f)*55f;
x2 = (width/100f)*65;
y2 = (height/100f)*60f;
x3 = (width/100f)*50;
y3 = (height/100f)*90f;
path.cubicTo(x1, y1, x2, y2, x3, y3);
path.lineTo(pX,pY);
x1 = (width/100f)*50;
y1 = (height/100f)*5;
x2 = (width/100f)*10;
y2 = (height/100f)*10;
x3 = (width/100f)*10;
y3 = (height/100f)*33.33f;
path.moveTo(pX,pY);
path.cubicTo(x1, y1, x2, y2, x3, y3);
path.moveTo(x3,pY);
x1 = (width/100f)*10;
y1 = (height/100f)*55f;
x2 = (width/100f)*35f;
y2 = (height/100f)*60f;
x3 = (width/100f)*50f;
y3 = (height/100f)*90f;
path.cubicTo(x1, y1, x2, y2, x3, y3);
path.lineTo(pX,pY);
path.moveTo(x3,y3);
path.close();
return path;
}
我们可以修改 createHeartPath()
方法的逻辑/绘图物理以获得更好的输出,欢迎提出新的建议和更改。
关于android - 在 Canvas 上画一颗心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22775442/
我想以 headless 模式(屏幕上根本没有 GUI)将 JPanel 绘制到 BufferedImage 中。 final JPanel panel = createPanel(); panel.
我是 Canvas 的新手,正在尝试创建看起来逼真的 float 粒子动画。 目前,我正在创建 400 个随机散布在 400x400 Canvas 上的粒子。 然后,在每个 requestAnimat
有没有办法在悬停时停止悬 float 画? :hover 这是一个显示动画的链接: https://codepen.io/youbiteme/pen/RprPrN 最佳答案 只需为您的 svg 悬停添
我想在谷歌地图上绘制覆盖图,其中除了特定点周围 1.5 公里半径以外的所有内容都被遮蔽了。为此,我尝试使用带有大量边框的圆圈,所以我会在边框中放置透明中心和覆盖颜色来实现这一点,但它无法渲染。
我正在尝试通过扩展类 UIView 来创建自定义 View ,该类可以在自定义 View 的中心显示一个圆圈。为了添加自定义绘图,我重写了 draw(_ rect: CGRect) 方法,如下所示。
我是一名优秀的程序员,十分优秀!