旋转,一圈间隔Android的布局有何看法? [英] Android Layout views rotated and spaced around a circle?

查看:283
本文介绍了旋转,一圈间隔Android的布局有何看法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出一种方法来布局了一系列的一圈,在每个视图旋转到朝外从圆这样的观点。下面的图片是我要找的一个草图。外面块布局/ ViewGroup中,红色方块重新present的意见我想旋转。

I'm trying to figure out a way to layout a series of views around a circle, in such a way that each view is rotated to be facing outward from the circle. The picture below is a rough sketch of what I'm looking for. The outside block is the layout/viewgroup, the red squares represent the views I want to rotate.

我熟悉的PivotX,PivotY和旋转视图属性,并怀疑我会利用这些以某种方式,但我不知道如何在演唱会中使用这些用适当的布局,以获得所需的效果。

I'm familiar with the PivotX, PivotY, and Rotation view properties and suspect I will be making use of these in some way, but I'm not sure how to use these in concert with an appropriate layout to get the desired effect.

推荐答案

下面是做到这一点的一个例子。我创建了一个新的Andr​​oid项目,并取代了 RelativeLayout的这是已经在那里,有一个的FrameLayout 。这不仅是因为> = API 11平移和旋转的查看电话:

Here's an example that does this. I created a new Android project and replaced the RelativeLayout that's already there, with a FrameLayout. It's >= API 11 only because of translate and rotate calls in 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>

我将创建在code一些快速的意见,只是与你有什么看法取代它们。我把他们都在布局的中心,通过它们的的LayoutParams 的严重性设置为 Gravity.CENTER 。然后,我翻译,并将其旋转到正确的位置:

I'll create some quick views in code, just replace them with whatever views you have. I'm placing them all in the center of the layout, by setting the gravity of their LayoutParams to Gravity.CENTER. Then, I'm translating and rotating them to their correct positions:

@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);
    }
}

这是结果:

这篇关于旋转,一圈间隔Android的布局有何看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆