Android Canvas 旋转Rect功能实施

来源:互联网 发布:cs软件界面设计工具 编辑:程序博客网 时间:2024/06/08 11:25

旋转方框功能实现图解


代码实现
1.采用矩阵映射,得到旋转后的功能按钮的新坐标(旋转按钮,删除按钮,缩放按钮)
  //检测点击是否在范围      getTouchRect(rotateRect).contains(x1, y1);       //得到旋转后按钮点击事件框      public RectF getTouchRect(RectF rect){//          System.out.println("处理触摸事件。。。。");            if(matrix==null){                  return rect;            }          matrix.mapRect(tempRect,rect);          return tempRect;      }

2.计算角度
判断三点是顺时针还是逆时针方向
资料:http://blog.sina.com.cn/s/blog_631f9e4b0100g874.html   
已知三点求 三点之间夹角角度
资料:http://blog.csdn.net/lilin8905/article/details/69373069

public static double calcAngle(Point cen, Point first, Point second)       {           final double M_PI = 3.1415926535897  ;                  double ma_x = first.x - cen.x;           double ma_y = first.y - cen.y;           double mb_x = second.x - cen.x;           double mb_y = second.y - cen.y;           double v1 = (ma_x * mb_x) + (ma_y * mb_y);           double ma_val = Math.sqrt(ma_x * ma_x + ma_y * ma_y);           double mb_val = Math.sqrt(mb_x * mb_x + mb_y * mb_y);           double cosM = v1 / (ma_val * mb_val);           double angleAMB = Math.acos(cosM) * 180 / M_PI;                                   int v=(cen.x-first.x)*(second.y-cen.y)-(cen.y-first.y)*(second.x-cen.x);          if(v>0){//          System.out.println("逆时针");            return -angleAMB;          }else if(v<0){//          System.out.println("顺时针");          }else{//          System.out.println("不变");          }          return angleAMB;       }

3.绘制图形

 @Override

 public void onDraw(Canvas canvas) {                        canvas.save();            canvas.rotate(degree,rect.left+(rect.right-rect.left)/2,rect.top+(rect.bottom-rect.top)/2); //以矩形中心点旋转图形            canvas.drawRect(rect, paint);            if(isEdit){                               canvas.drawBitmap(deleteIcon, deleteRect.left, deleteRect.top, paint);                  canvas.drawBitmap(resizeIcon, resizeRect.left, resizeRect.top, paint);                  canvas.drawBitmap(rotateIcon, rotateRect.left, rotateRect.top, paint);            }            canvas.restore();   }


原创粉丝点击