2017-08-31 9 views
0

私はアンドロイドでカスタムメニューを作成したいと思います。次の図のようになります。Android - メニュー使用のために円弧を分割する

メニューの

画像 enter image description here

私はすでにメニューポイントを除くメニューの主要部分を作成するために達成しました。それで、次の画像のように見えます。

メニューの現在の状態 enter image description here

このメニューでは、のCustomViewであると私はcanvas.drawArc方法とRectFとしてこれらの弧を描きました。 私はそれが非常に明白で、私が達成したいと思います。私は、円弧を均等にサイズの小さなアークに分割したり、新しいアークを追加したりしたい(これはまったく別のアークです)。 私は単純にこれらのアークの境界を1/3に設定しようとしましたが、このような結果は得られません。

誰かがこれを行う方法を知っていますか、または私のアプローチを完全に変更しますか?

答えて

0

いくつかの研究、実験、簡単な数学の後、私は次のコードを思いついた。

private void drawMenu(Canvas canvas, RectF arcBoundaries, int arcAngle, int sweep, Paint arcPaint, MenuPosition position, double scaleValue, int strokeWidth) { 

     int countAllMenuItems = getMenuCount(position); 
     int newAngle = arcAngle; 
     int counter = 0; 

     for (MenuItemView menuItem : menuItems) { 
      if (menuItem.getPosition().toLowerCase().equals(position.toString().toLowerCase())) { 
       canvas.drawArc(arcBoundaries, newAngle, sweep/countAllMenuItems, false, arcPaint); 
       newAngle += sweep/countAllMenuItems; 
       if (counter != countAllMenuItems - 1) { 
        calculateLines(arcBoundaries, newAngle, strokeWidth); 
       } 
       counter++; 
      } 
     } 

    } 
} 

private void calculateLines(RectF arcBoundaries, int angle, int strokeWidth) { 
    int realAngle = angle % 360; 
    float xRadius = arcBoundaries.width()/2; 
    float yRadius = arcBoundaries.height()/2; 

    double x = arcBoundaries.centerX() + (xRadius-(strokeWidth/2))*Math.cos(realAngle * Math.PI/180); 
    double y = arcBoundaries.centerY() + (yRadius-(strokeWidth/2))*Math.sin(realAngle * Math.PI/180); 
    double xEnd = arcBoundaries.centerX() + (xRadius+(strokeWidth/2))*Math.cos(realAngle * Math.PI/180); 
    double yEnd = arcBoundaries.centerY() + (yRadius+(strokeWidth/2))*Math.sin(realAngle * Math.PI/180); 


    lineList.put(new Point((int) x, (int) y), new Point((int) xEnd, (int) yEnd)); 
} 

実際にはとても簡単です。すべてのMenuItemの数と掃引角度を使って、各行の角度を計算します。次に、角度関数を使用してxとyの開始値と終了値を計算します。

だから式使用:

X(に隣接する側)= centerX(座標系に合わせる)+斜辺* COS(角度)

Y(反対側)= centerY(座標系に適応し)+斜辺* sin(angle)

角度関数の角度は、ラジアン単位でなければなりません。また、ストロークスタイルを使ってこれらの円弧を描くので、ストロークの中心に半径が移動するため、ストロークの減算/追加が必要でした。

私はこれらの値をリストに入れて、他のすべてを描いた後に描画します。したがって、それらはすべての上にあります。

同じ問題を抱えているすべての人を助けてくれることを望みます。

関連する問題