作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我可以使用
在 Canvas 上画一个圆和一个长方形path.addCircle()
和
path.addRect()
。
现在我想知道如何画三角形、星形、正方形或心形?
最佳答案
为了以后直接求答案,我用canvas画了一个几乎对称的星星,如图:
主要工具是使用 Paths。
假设您已设置:
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
Path path = new Path();
然后在你的 onDraw 中,你可以像我下面那样使用路径。它将适本地缩放到任何尺寸的 Canvas
@Override
protected void onDraw(Canvas canvas) {
float mid = getWidth() / 2;
float min = Math.min(getWidth(), getHeight());
float fat = min / 17;
float half = min / 2;
float rad = half - fat;
mid = mid - half;
paint.setStrokeWidth(fat);
paint.setStyle(Paint.Style.STROKE);
canvas.drawCircle(mid + half, half, rad, paint);
path.reset();
paint.setStyle(Paint.Style.FILL);
// top left
path.moveTo(mid + half * 0.5f, half * 0.84f);
// top right
path.lineTo(mid + half * 1.5f, half * 0.84f);
// bottom left
path.lineTo(mid + half * 0.68f, half * 1.45f);
// top tip
path.lineTo(mid + half * 1.0f, half * 0.5f);
// bottom right
path.lineTo(mid + half * 1.32f, half * 1.45f);
// top left
path.lineTo(mid + half * 0.5f, half * 0.84f);
path.close();
canvas.drawPath(path, paint);
super.onDraw(canvas);
}
关于android - 如何在 Canvas 上绘制三角形、星形、正方形或心形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7007429/
我想给一颗用 CSS 制作的心命名。而且我似乎不知道该怎么做。 我已经有了这个代码: #heart { position:relative; width:100px; height:100
我正在使用 php 将 ♥ 类型的字符输入到我的数据库中。 我在页面上设置了UTF-8 和 插入的结果是一个?字符 我敢肯定有人这样做过,但我真的很难把它做好。 编辑: MySQL表的排序规则和字
我编写了一个小应用程序,其中显示了几张 Android 卡,每个卡都有彩色标题。 我希望能够在每张卡片的右上角添加图标,就像下图中的那样。到目前为止,尽管我发现如下所示的屏幕截图表明这是可能的。我在网
我是一名优秀的程序员,十分优秀!