先看下效果图:

下面我们直接看代码

我自定义了一个View,代码如下:

package com.davis.drawtrangle;import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;import java.util.ArrayList;
import java.util.List;public class DrawView extends View {private List<PointBean> pointLists = new ArrayList<PointBean>();private PointBean pointBean = new PointBean(-1, -1, -1, -1);private static int height = 30;private static int bottom = 10;private Paint paint = new Paint(){{setColor(Color.RED);setAntiAlias(true);setStrokeWidth(4.0f);}};public DrawView(Context context) {super(context);}public DrawView(Context context, @Nullable AttributeSet attrs) {super(context, attrs);}public DrawView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}public void clear(){if(pointBean != null){pointBean.setStartX(-1);pointBean.setStartY(-1);pointBean.setEndX(-1);pointBean.setEndY(-1);}if(pointLists != null && pointLists.size() > 0){pointLists.clear();}invalidate();}@Overrideprotected void onDraw(Canvas canvas) {//super.onDraw(canvas);if(pointLists != null && pointLists.size() > 0){for(int i=0;i<pointLists.size();i++){PointBean pb = pointLists.get(i);canvas.drawLine(pb.getStartX(), pb.getStartY(), pb.getEndX(), pb.getEndY(), paint);drawTrangle(canvas, paint, pb.getStartX(), pb.getStartY(), pb.getEndX(), pb.getEndY(), height, bottom);}}if(pointBean != null && pointBean.getStartX() != -1&& pointBean.getStartY() != -1 && pointBean.getEndX() != -1&& pointBean.getEndY() != -1){canvas.drawLine(pointBean.getStartX(), pointBean.getStartY(), pointBean.getEndX(), pointBean.getEndY(), paint);drawTrangle(canvas, paint, pointBean.getStartX(), pointBean.getStartY(), pointBean.getEndX(), pointBean.getEndY(), height, bottom);}}/*** 绘制三角* @param canvas* @param fromX* @param fromY* @param toX* @param toY* @param height* @param bottom*/private void drawTrangle(Canvas canvas, Paint paintLine, float fromX, float fromY, float toX, float toY, int height, int bottom){try{float juli = (float) Math.sqrt((toX - fromX) * (toX - fromX)+ (toY - fromY) * (toY - fromY));// 获取线段距离float juliX = toX - fromX;// 有正负,不要取绝对值float juliY = toY - fromY;// 有正负,不要取绝对值float dianX = toX - (height / juli * juliX);float dianY = toY - (height / juli * juliY);float dian2X = fromX + (height / juli * juliX);float dian2Y = fromY + (height / juli * juliY);//终点的箭头Path path = new Path();path.moveTo(toX, toY);// 此点为三边形的起点path.lineTo(dianX + (bottom / juli * juliY), dianY- (bottom / juli * juliX));path.lineTo(dianX - (bottom / juli * juliY), dianY+ (bottom / juli * juliX));path.close(); // 使这些点构成封闭的三边形canvas.drawPath(path, paintLine);}catch (Exception ex){ex.printStackTrace();}}@Overridepublic boolean onTouchEvent(MotionEvent event) {int action = event.getAction();switch (action){case MotionEvent.ACTION_DOWN:onActionDown(event);break;case MotionEvent.ACTION_MOVE:onActionMove(event);break;case MotionEvent.ACTION_UP:onActionUp(event);break;}return true;//return super.onTouchEvent(event);}private void onActionDown(MotionEvent event){try {if(pointBean == null){pointBean = new PointBean(-1, -1, -1, -1);}pointBean.setStartX(event.getX());pointBean.setStartY(event.getY());}catch (Exception ex){ex.printStackTrace();}invalidate();}private void onActionMove(MotionEvent event){try{if(pointBean != null){pointBean.setEndX(event.getX());pointBean.setEndY(event.getY());}}catch (Exception ex){ex.printStackTrace();}invalidate();}private void onActionUp(MotionEvent event){try {if(pointBean != null){pointBean.setEndX(event.getX());pointBean.setEndY(event.getY());PointBean pb = new PointBean();pb.setStartX(pointBean.getStartX());pb.setStartY(pointBean.getStartY());pb.setEndX(pointBean.getEndX());pb.setEndY(pointBean.getEndY());pointLists.add(pb);pointBean.setStartX(-1);pointBean.setStartY(-1);pointBean.setEndX(-1);pointBean.setEndY(-1);}}catch (Exception ex){ex.printStackTrace();}invalidate();}}

其中关键绘制箭头的是 drawTrangle() 这个方法。

完整的代码已上传到github上:Canvas绘制带箭头的直线

Android Canvas绘制带箭头的直线相关推荐

  1. Matlab任意两点之间绘制带箭头的直线

    Matlab任意两点之间绘制带箭头的直线 简单绘制任意两点之间.本来不想自己写的,可是网上的代码用起来不舒服,所以简单看看原理,原来就是个坐标变换而已.索性自己写了一份,分享如下: function ...

  2. 关于利用canvas画带箭头的直线旋转

    利用canvas在Vue项目中使用: <imgid="range_right"src=""style="position: absolute;l ...

  3. 【MFC】如何画带箭头的直线

    [MFC]如何画带箭头的直线 前言 代码 获得箭头坐标 前言 如何绘制带箭头的直线,说来也简单,只需要算出箭头左右两边的坐标,再使用LineTo()函数即可.话不多说上代码. 代码 获得箭头坐标 已知 ...

  4. html5画直线箭头,HTML5 canvas画带箭头的虚线

    今天给大家讲解的是在HTML5 canvas画带箭头的虚线.关于Canvas 对象表示一个 HTML 画布元素 -.它没有自己的行为,但是定义了一个 API 支持脚本化客户端绘图操作. 本案例注意事项 ...

  5. canvas上,如何绘制带有箭头的直线?

    话不多说,直接上代码~~~ <canvas id="canvasId" width="400" height="400" style= ...

  6. Raphael绘制流程图(二),添加带箭头的直线

    上一篇文章中,我们添加了元素(矩形框),接下来给矩形框加上带箭头的直线,代码如下: var wf_r = null; //画板对象 var wf_steps = []; //步骤数组 var wf_w ...

  7. CAD里面怎么画带箭头的直线

    转自:http://jingyan.baidu.com/article/9113f81b0192e72b3214c709.html?st=2&os=0&bd_page_type=1&a ...

  8. R语言使用rnorm函数生成正太分布数据、使用plot函数可视化折线图、使用arrows函数在可视化图像中绘制箭头曲线、绘制带箭头线段,可以设置箭头角度,有几个箭头(1起点箭头、2终点箭头,3双箭头)

    R语言使用rnorm函数生成正太分布数据.使用plot函数可视化折线图.使用arrows函数在可视化图像中绘制箭头曲线.绘制带箭头线段,可以设置箭头角度,有几个箭头(1起点箭头.2终点箭头,3双箭头) ...

  9. Matlab如何绘制带箭头图形

    Matlab如何绘制带箭头图形 带箭头坐标轴 带箭头曲线 实例演示 例1 带箭头曲线 例2 带箭头曲线+带箭头坐标轴 联系作者 带箭头坐标轴 前段时间推出了绘制箭头坐标轴曲线的方法,许多网友联系我索取 ...

最新文章

  1. 北邮国院c语言期末考试题,北邮C语言复习题2014.ppt
  2. python语言if语句-Python在if语句中等同于(logical-and)
  3. common pool2 mysql_用common-pool自定义资源池
  4. option:contains后面加变量_什么是配置环境变量,配置以后有什么作用呢?
  5. linux下达梦数据库启动_linux上安装tomcat和达梦数据库
  6. 华擎 j3455 时钟 linux,J3455安装centos步骤
  7. Docker容器镜像删除不掉解决办法?
  8. HTML5之美(转)
  9. PHP常用时间函数总结
  10. shell基础之变量及表达式
  11. @Transactional注解属性(1)
  12. 鱼群算法matlab代码,人工鱼群算法MATLAB实现
  13. 非线性光纤光学——四波混频
  14. VBM计算操作过程记录
  15. c++ 字符串替换所有字符串
  16. 开拓海外市场,需要选择怎样的云服务?
  17. CentOS换源、linux配置IP、腾讯云SHH秘钥、公钥
  18. ROS新手问题 [rospack] Error: package '***' not found
  19. FineBI 的关联视图及多表关联 自助数据集
  20. 搜狗双拼如何打单韵母字

热门文章

  1. qr分解求线性方程组_梯度下降求解线性方程组算例设计
  2. html中点击照片时放大缩小,基于jquery实现一张图片点击鼠标放大再点缩小
  3. php调用谷歌翻译接口_一个google翻译的php调用方式
  4. PHP获取表单数据的方法有几种,如何实现PHP获取表单数据与HTML嵌入PHP脚本
  5. 【POJ - 2186】Popular Cows (Tarjan缩点)
  6. 【HDU - 1870】愚人节的礼物(水题模拟 思想类似于栈?)
  7. 12.深度学习练习:Residual Networks(注定成为经典)
  8. android okgo参数,Android OkGo基本操作
  9. 打印心形c语言,C语言打印心形
  10. python一节课多久_第一节课 python简介