Chinaunix首页 | 论坛 | 博客
  • 博客访问: 449496
  • 博文数量: 145
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1060
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-22 11:52
个人简介

专注计算机技术: Linux Android 云计算 虚拟化 网络

文章分类

全部博文(145)

文章存档

2016年(3)

2015年(21)

2014年(75)

2013年(46)

我的朋友

分类: Android平台

2013-11-01 16:07:15

画箭头这个东西太麻烦啦,开始想用把箭头画好,然后到指定点旋转的方法,但是,效果一直不好。想用数学的方法来画,但是发现计算很复杂啊。于是google,发现一个兄台使用了java当中的awt实现了画箭头(),于是就借过来,改了一下,结果真能用。成果不敢独占,在此拿来给大家分享:


点击(此处)折叠或打开

  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(0, 0, 100, 100);
  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.     }


显示效果如下图所示:

阅读(2809) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~