Android布局视图围绕圆圈旋转和间隔?

Cod*_*ile 13 graphics layout android rotation

我试图想出一种方法来围绕圆形布局一系列视图,以这种方式旋转每个视图从圆圈向外.下图是我正在寻找的草图.外部块是布局/视图组,红色方块表示我想要旋转的视图.

粗略草图我想做什么

我熟悉PivotX,PivotY和旋转视图属性,并怀疑我将以某种方式使用它们,但我不确定如何将这些与适当的布局一起使用以获得所需的效果.

Dan*_*son 25

这是一个做这个的例子.我创建了一个新的Android项目,取而代之的是RelativeLayout那个已经存在的项目FrameLayout.这是> = API 11只是因为翻译和旋转调用View:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/hello_world" />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

我将在代码中创建一些快速视图,只需用您拥有的任何视图替换它们.我把他们都在布局的中心,通过设置他们的严重性LayoutParamsGravity.CENTER.然后,我正在翻译并将它们旋转到正确的位置:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final FrameLayout main = (FrameLayout)findViewById(R.id.main);

    int numViews = 8;
    for(int i = 0; i < numViews; i++)
    {
        // Create some quick TextViews that can be placed.
        TextView v = new TextView(this);
        // Set a text and center it in each view.
        v.setText("View " + i);
        v.setGravity(Gravity.CENTER);
        v.setBackgroundColor(0xffff0000);
        // Force the views to a nice size (150x100 px) that fits my display.
        // This should of course be done in a display size independent way.
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(150, 100);
        // Place all views in the center of the layout. We'll transform them
        // away from there in the code below.
        lp.gravity = Gravity.CENTER;
        // Set layout params on view.
        v.setLayoutParams(lp);

        // Calculate the angle of the current view. Adjust by 90 degrees to
        // get View 0 at the top. We need the angle in degrees and radians.
        float angleDeg = i * 360.0f / numViews - 90.0f;
        float angleRad = (float)(angleDeg * Math.PI / 180.0f);
        // Calculate the position of the view, offset from center (300 px from
        // center). Again, this should be done in a display size independent way.
        v.setTranslationX(300 * (float)Math.cos(angleRad));
        v.setTranslationY(300 * (float)Math.sin(angleRad));
        // Set the rotation of the view.
        v.setRotation(angleDeg + 90.0f);
        main.addView(v);
    }
}
Run Code Online (Sandbox Code Playgroud)

这就是结果:

一个圆圈中的8个视图的图像