android 使用Canvas画箭头

今天在做一个程序的时候需要画一个带箭头的坐标轴,找了很多办法都没成功,于是google,发现一个兄台使用了java当中的awt实现了画箭头,于是就借过来,改了一下,结果真能用。下面把画的一个箭头成果分享给大家:

[java]  view plain  copy
  1. public class MyCanvas extends View{  
  2.       
  3.     private Canvas myCanvas;  
  4.     private Paint myPaint=new Paint();  
  5.       
  6.     public MyCanvas(Context context) {  
  7.         super(context);  
  8.         // TODO Auto-generated constructor stub  
  9.     }  
  10.   
  11.     public MyCanvas(Context context, AttributeSet attrs, int defStyle) {  
  12.         super(context, attrs, defStyle);  
  13.         // TODO Auto-generated constructor stub  
  14.     }  
  15.   
  16.     public MyCanvas(Context context, AttributeSet attrs) {  
  17.         super(context, attrs);  
  18.         // TODO Auto-generated constructor stub  
  19.     }  
  20.   
  21.     @Override  
  22.     protected void onDraw(Canvas canvas) {  
  23.         // TODO Auto-generated method stub  
  24.         super.onDraw(canvas);  
  25.         this.myCanvas=canvas;  
  26.         drawAL(00100100);  
  27.     }  
  28.       
  29.     /** 
  30.      * 设置画笔默认样式 
  31.      */  
  32.     public void setPaintDefaultStyle(){  
  33.         myPaint.setAntiAlias(true);  
  34.         myPaint.setColor(Color.RED);  
  35.         myPaint.setStyle(Paint.Style.STROKE);  
  36.         myPaint.setStrokeWidth(3);  
  37.     }  
  38.       
  39.       
  40.     /** 
  41.      * 画圆 
  42.      * @param x x坐标 
  43.      * @param y y坐标 
  44.      * @param radius    圆的半径 
  45.      */  
  46.     public void drawCircle(float x,float y,float radius){  
  47.         myCanvas.drawCircle(x, y, radius, myPaint);  
  48.         invalidate();  
  49.     }  
  50.       
  51.     /** 
  52.      * 画一条直线 
  53.      * @param fromX 起点x坐标 
  54.      * @param fromY 起点Y坐标 
  55.      * @param toX   终点X坐标 
  56.      * @param toY   终点Y坐标 
  57.      */  
  58.     public void drawLine(float fromX,float fromY,float toX,float toY){  
  59.         Path linePath=new Path();  
  60.         linePath.moveTo(fromX, fromY);  
  61.         linePath.lineTo(toX, toY);  
  62.         linePath.close();  
  63.         myCanvas.drawPath(linePath, myPaint);  
  64.         invalidate();  
  65.     }  
  66.       
  67.       
  68.     /** 
  69.      * 画箭头 
  70.      * @param sx 
  71.      * @param sy 
  72.      * @param ex 
  73.      * @param ey 
  74.      */  
  75.     public void drawAL(int sx, int sy, int ex, int ey)  
  76.     {  
  77.         double H = 8// 箭头高度     
  78.         double L = 3.5// 底边的一半     
  79.         int x3 = 0;  
  80.         int y3 = 0;  
  81.         int x4 = 0;  
  82.         int y4 = 0;  
  83.         double awrad = Math.atan(L / H); // 箭头角度     
  84.         double arraow_len = Math.sqrt(L * L + H * H); // 箭头的长度     
  85.         double[] arrXY_1 = rotateVec(ex - sx, ey - sy, awrad, true, arraow_len);  
  86.         double[] arrXY_2 = rotateVec(ex - sx, ey - sy, -awrad, true, arraow_len);  
  87.         double x_3 = ex - arrXY_1[0]; // (x3,y3)是第一端点     
  88.         double y_3 = ey - arrXY_1[1];  
  89.         double x_4 = ex - arrXY_2[0]; // (x4,y4)是第二端点     
  90.         double y_4 = ey - arrXY_2[1];  
  91.         Double X3 = new Double(x_3);  
  92.         x3 = X3.intValue();  
  93.         Double Y3 = new Double(y_3);  
  94.         y3 = Y3.intValue();  
  95.         Double X4 = new Double(x_4);  
  96.         x4 = X4.intValue();  
  97.         Double Y4 = new Double(y_4);  
  98.         y4 = Y4.intValue();  
  99.         // 画线     
  100.         myCanvas.drawLine(sx, sy, ex, ey,myPaint);  
  101.         Path triangle = new Path();  
  102.         triangle.moveTo(ex, ey);  
  103.         triangle.lineTo(x3, y3);    
  104.         triangle.lineTo(x4, y4);   
  105.         triangle.close();  
  106.         myCanvas.drawPath(triangle,myPaint);  
  107.   
  108.     }  
  109.     // 计算     
  110.     public double[] rotateVec(int px, int py, double ang, boolean isChLen, double newLen)  
  111.     {  
  112.         double mathstr[] = new double[2];  
  113.         // 矢量旋转函数,参数含义分别是x分量、y分量、旋转角、是否改变长度、新长度     
  114.         double vx = px * Math.cos(ang) - py * Math.sin(ang);  
  115.         double vy = px * Math.sin(ang) + py * Math.cos(ang);  
  116.         if (isChLen) {  
  117.             double d = Math.sqrt(vx * vx + vy * vy);  
  118.             vx = vx / d * newLen;  
  119.             vy = vy / d * newLen;  
  120.             mathstr[0] = vx;  
  121.             mathstr[1] = vy;  
  122.         }  
  123.         return mathstr;  
  124.     }  
  125.       
  126.       
  127. }  
显示效果如下图所示:

完整例子源码下载地址:http://download.csdn.net/detail/u013184970/9564182

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

时代新人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值