如何在android中画一条曲线?

2024-05-21

我是 Android 新手,正在开发一个关于绘制线条的示例项目。我想画一条连接两点的曲线或高架线(x1,y1 and x2,y2)。我试过canvas.drawArc()方法,但是RectF内的值drawArc方法只是圆的 x,y 中心点。它在我的两点之间给了我一个弧线。但我想要一条曲线完全连接我的两点。有人可以帮助我吗?提前致谢。


在 onDraw 方法中声明此方法:

private void drawOvalAndArrow(Canvas canvas){


    Paint circlePaint = new Paint();
    circlePaint.setStyle(Paint.Style.FILL_AND_STROKE);
    circlePaint.setAntiAlias(true);
    circlePaint.setStrokeWidth(2);
    circlePaint.setColor(Color.CYAN);

    float centerWidth = canvas.getWidth()/2; //get center x of display
    float centerHeight = canvas.getHeight()/2; //get center y of display
    float circleRadius = 20; //set radius 
    float circleDistance = 200; //set distance between both circles

    //draw circles
    canvas.drawCircle(centerWidth, centerHeight, circleRadius, circlePaint);
    canvas.drawCircle(centerWidth+circleDistance, centerHeight, circleRadius, circlePaint);


    //to draw an arrow, just lines needed, so style is only STROKE
    circlePaint.setStyle(Paint.Style.STROKE);       
    circlePaint.setColor(Color.RED);

    //create a path to draw on
    Path arrowPath = new Path();

    //create an invisible oval. the oval is for "behind the scenes" ,to set the path´
    //area. Imagine this is an egg behind your circles. the circles are in the middle of this egg
    final RectF arrowOval = new RectF();
    arrowOval.set(centerWidth, 
            centerHeight-80, 
            centerWidth + circleDistance, 
            centerHeight+80);

    //add the oval to path
    arrowPath.addArc(arrowOval,-180,180);

    //draw path on canvas
    canvas.drawPath(arrowPath, circlePaint);


    //draw arrowhead on path start
     arrowPath.moveTo(centerWidth,centerHeight ); //move to the center of first circle
     arrowPath.lineTo(centerWidth-circleRadius, centerHeight-circleRadius);//draw the first arrowhead line to the left
     arrowPath.moveTo(centerWidth,centerHeight );//move back to the center
     arrowPath.lineTo(centerWidth+circleRadius, centerHeight-circleRadius);//draw the next arrowhead line to the right

     //same as above on path end
     arrowPath.moveTo(centerWidth+circleDistance,centerHeight );
     arrowPath.lineTo((centerWidth+circleDistance)-circleRadius, centerHeight-circleRadius);
     arrowPath.moveTo(centerWidth+circleDistance,centerHeight );
     arrowPath.lineTo((centerWidth+circleDistance)+circleRadius, centerHeight-circleRadius);

     //draw the path
     canvas.drawPath(arrowPath,circlePaint);

}

此外,这还将找到屏幕的两侧(横向模式),并在屏幕上绘制完美的曲线

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    PointF mPoint1 = new PointF(w/1.2F, h/1.2F);
    PointF mPoint2 = new PointF(w/24, h/1.2F);
    Path myPath1 = new Path();
    Paint paint  = new Paint();
    paint.setAntiAlias(true);
    paint.setStyle(Style.STROKE);
    paint.setStrokeWidth(2);
    paint.setColor(Color.WHITE);

    myPath1 = drawCurve(canvas, paint, mPoint1, mPoint2);
    canvas.drawPath(myPath1, paint);

}

private Path drawCurve(Canvas canvas, Paint paint, PointF mPointa, PointF mPointb) {

    Path myPath = new Path();
    myPath.moveTo(63*w/64, h/10);
    myPath.quadTo(mPointa.x, mPointa.y, mPointb.x, mPointb.y);
    return myPath;  
}

关于在 android 中绘画的有用参考:

如何在Android中使用canvas绘制弧线? http://www.coderzheaven.com/2013/01/10/draw-arcs-android-canvas/

带有风景的基础绘画 https://guides.codepath.com/android/Basic-Painting-with-Views

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在android中画一条曲线? 的相关文章

随机推荐

  • 如何检查主音量是否静音

    如何在 Windows 7 操作系统中检查主音量是否静音我有静音或取消静音的代码 IE Public Const APPCOMMAND VOLUME MUTE As Integer H80000 Public Const APPCOMMAN
  • rake cucumber 和 rake spec 始终使用“开发”环境

    我运行 Cucumber 和 RSpec 测试的 rake 任务始终使用我的development环境 以下是相关的配置文件 RAILS ROOT config environments cucumber rb Edit at your o
  • C free() 是如何工作的? [复制]

    这个问题在这里已经有答案了 可能的重复 malloc 和 free 如何工作 https stackoverflow com questions 1119134 how malloc and free work include
  • 寻求有关共享内存锁定问题的文章

    我正在审查一些代码并对所使用的技术感到怀疑 在Linux环境中 有两个进程附加多个 共享内存段 第一个进程定期加载新的集合 要共享的文件 并将共享内存ID shmid 写入 主 共享内存段中的一个位置 第二道工序 不断读取这个 主 位置并使
  • 如何将 Spark DataFrame 以 csv 格式保存在磁盘上?

    例如 这样的结果 df filter project en select title count groupBy title sum 将返回一个数组 如何将 Spark DataFrame 作为 csv 文件保存在磁盘上 Apache Sp
  • 客户端应用程序立即对数据库中的更新做出反应的最佳方式是什么?

    对数据库中的数据更新做出立即反应的最佳方法是什么 我能立即想到的最简单的方法是一个线程 它检查数据库中某些数据的特定更改 并持续等待在某个预定义的时间长度内再次检查它 这个解决方案对我来说似乎是浪费和次优的 所以我想知道是否有更好的方法 我
  • 如何在 VSTS 中的托管代理上运行或安装工具

    我想在 VSTS 上以管理员身份运行 cmd 实际上 我正在尝试在 VSTS 托管代理上安装带有 Chocolatey 工具管理器的 git tfs 因此我在 VSTS 命令行任务上运行以下命令 SystemRoot System32 Wi
  • 如何使用 zio-test 测试异常情况

    我有以下功能 我想测试 def people id Int RIO R People 如果有 People 则此函数返回 Peopleid 分别 如果没有则失败 例如 IO fail ServiceException s No People
  • postgresql中的按日期聚合函数分组

    我在运行此查询时遇到错误 SELECT date updated at count updated at as total count FROM persons WHERE persons updated at BETWEEN 2012 1
  • 如何在 d3 js 中突出显示从根到选定节点的路径?

    我使用 d3 js 创建了一棵树 现在我创建了一个下拉菜单 其中包含树中所有节点的列表 现在 从下拉菜单中选择一个节点时 我想突出显示从根到该特定节点的路径 这个怎么做 首先创建一个 flatten 函数 它将分层数据变成一个 n 数组 f
  • emacs 去掉 shell 中的所有 ansi 颜色代码

    我在 OS X 上使用 emacs 24 但遇到了一个奇怪的问题 我看不到任何颜色代码 Emacs 似乎只是忽略它们 我的动机是查看 C 项目的 cmake llvm 和 googletest 框架的彩色输出 我想在编译模式下查看颜色 但是
  • 创建 Cookie 时需要帮助

    我有一个名为yes和另一个名叫no
  • 搜索多个字段

    我想我没有正确理解 django haystack 我有一个包含多个字段的数据模型 我希望搜索其中两个字段 class UserProfile models Model user models ForeignKey User unique
  • 外部实体更改后索引不更新

    我目前正在开发一个项目 使用 JPA 2 1 保存数据并使用 hibernate search 4 5 0 final 搜索实体 映射类和索引后 搜索工作正常 但是 当我更改值时描述B 类从 someStr 到 anotherStr 数据库
  • 重定向到 /admin/login/ 结果为 302

    当用户未经身份验证时 我尝试重定向到登录页面 在我的settings py我的课程有 MIDDLEWARE CLASSES path to AuthRequiredMiddleware 这是我的课程 class AuthRequiredMi
  • 改进迭代文本解析的 clojure lazy-seq 使用

    我正在编写一个 Clojure 实现这次编码挑战 http biostar stackexchange com questions 1759 code golf mean length of fasta sequences 尝试找出 Fas
  • XPATH 查询、HtmlAgilityPack 和提取文本

    我一直在尝试从名为 tim new 的类中提取链接 我也得到了解决方案 给出了解决方案 片段和必要的信息here https stackoverflow com questions 2982862 extracting a table ro
  • 如何使用 Swipe 视图实现 Android TabLayout 设计支持库

    我将使用 android TabLayout 设计支持库 但我不知道如何使用滑动视图 这是我的代码 XML
  • phpstorm xdebug 与 symfony2 项目

    我正在尝试使用 xdebug 和 phpstorm 调试 symfony2 应用程序 我的本地开发环境是Ubuntu 14 04 with apache2 Xdebug版本是2 2 7 我在另一个 php 不是 symfony2 项目上使用
  • 如何在android中画一条曲线?

    我是 Android 新手 正在开发一个关于绘制线条的示例项目 我想画一条连接两点的曲线或高架线 x1 y1 and x2 y2 我试过canvas drawArc 方法 但是RectF内的值drawArc方法只是圆的 x y 中心点 它在