Android 布局 View 围绕一个圆圈旋转和间隔?

标签 android layout graphics rotation

我正在尝试找出一种围绕圆圈布置一系列 View 的方法,使每个 View 都旋转为面向圆圈外。下图是我要找的东西的粗略草图。外面的 block 是布局/ View 组,红色方 block 代表我要旋转的 View 。

Rough Sketch of what I want to do

我熟悉 PivotX、Pivo​​tY 和 Rotation View 属性,并怀疑我会以某种方式使用它们,但我不确定如何将它们与适当的布局一起使用以获得所需的效果效果。

最佳答案

这是一个执行此操作的示例。我创建了一个新的 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>

我将在代码中创建一些快速 View ,只需将它们替换为您拥有的任何 View 即可。通过将它们的 LayoutParams 的重力设置为 Gravity.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);
    }
}

这是结果:

Image of 8 views in a circle

关于Android 布局 View 围绕一个圆圈旋转和间隔?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14173012/

相关文章:

java - 在android中将数据从一个 Intent 传递到另一个 Intent

java - android中找不到文件异常

css - 三个表并排溢出div容器?

javascript - 为什么 HTML Canvas 中两个形状之间出现白线?

graphics - 确定三角形所在的体素

android 应用程序未显示在我的设备或 eclipse 中的模拟器上

java - 如何在 Android Studio 上向我的项目添加库?

android - 为适用于 Android 的 Google map 标记使用 xml 布局

java - 在 FlowLayout 中并排嵌套多个 CardLayout

qt - 改变 QImage/QPixmap 的色调