網站首頁 編程語言 正文
本文實例為大家分享了Android使用貝塞爾曲線畫心形的具體代碼,供大家參考,具體內容如下
一開始我只是想畫個圓,可畫著畫著就成了心形,那就將錯就錯
1. 創建一個Activity
RelativeLayout container = findViewById(R.id.download_container);
? ? DisplayMetrics metrics = new DisplayMetrics();
? ? getWindowManager().getDefaultDisplay().getMetrics(metrics);
? ? deviceWidth = metrics.widthPixels;
? ? deviceHeight = metrics.heightPixels;
? ? Circle circle = new Circle(this, deviceWidth / 2, deviceHeight / 2, deviceWidth / 8);
? ? Line line = new Line(this, deviceWidth / 2, deviceHeight / 2, deviceWidth / 8);
? ? container.addView(line);
2. 創建一個自定義的View
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.view.View;
public class Line extends View {
? ? private Paint mPaint;
? ? private PointF startPointF;
? ? private PointF endPointF;
? ? private PointF controlPointF1, controlPointF2;
? ? private PointF startPointF2;
? ? private PointF endPointF2;
? ? private PointF controlPointF3, controlPointF4;
? ? public Line(Context context, float x, float y, float radius) {
? ? ? ? super(context);
? ? ? ? double d = (2 * Math.sqrt(2) - 1);
? ? ? ? this.startPointF = new PointF(x, y - radius);
? ? ? ? this.endPointF = new PointF(x, y + radius / 10);
? ? ? ? this.controlPointF1 = new PointF(x, (float) (y - d * radius));
? ? ? ? this.controlPointF2 = new PointF((float) (x + d * radius), (float) (y - d * radius));
? ? ? ? this.startPointF2 = new PointF(x, y - radius);
? ? ? ? this.endPointF2 = new PointF(x, y + radius / 10);
? ? ? ? this.controlPointF3 = new PointF(x, (float) (y - d * radius));
? ? ? ? this.controlPointF4 = new PointF((float) (x - d * radius), (float) (y - d * radius));
? ? ? ? this.mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
? ? ? ? this.mPaint.setColor(Color.WHITE);
? ? }
? ? @Override
? ? protected void onDraw(Canvas canvas) {
? ? ? ? super.onDraw(canvas);
? ? ? ? //繪制貝塞爾曲線
? ? ? ? Path path = new Path();
? ? ? ? path.moveTo(startPointF.x, startPointF.y);
? ? ? ? path.cubicTo(controlPointF1.x, controlPointF1.y, controlPointF2.x, controlPointF2.y, endPointF.x, endPointF.y);
? ? ? ? canvas.drawPath(path, mPaint);
? ? ? ? path.moveTo(startPointF2.x, startPointF2.y);
? ? ? ? path.cubicTo(controlPointF3.x, controlPointF3.y, controlPointF4.x, controlPointF4.y, endPointF2.x, endPointF2.y);
? ? ? ? canvas.drawPath(path, mPaint);
? ? }
}
運行效果
原文鏈接:https://blog.csdn.net/qq_18878455/article/details/90759968
相關推薦
- 2023-01-28 架構師說比起404我們更怕200錯誤_相關技巧
- 2023-03-22 幾分鐘教你掌握Redis簡單動態字符串SDS_Redis
- 2022-09-05 SparkStreaming寫入Hive慢
- 2023-02-03 使用PyGame顯示圖像的四種方案實例代碼_python
- 2022-03-23 CentOS7防火墻和端口相關命令介紹_Linux
- 2022-10-01 python3中_from...import...與import?...之間的區別詳解(包/模塊)_
- 2022-06-23 C++詳解哈夫曼樹的概念與實現步驟_C 語言
- 2022-06-12 C語言?深入探究動態規劃之區間DP_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支