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

这是一个这样做的例子。 我创建了一个新的Android项目,并用FrameLayout替换了已经存在的RelativeLayout 。 它只是> = API 11,因为在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); } }

这就是结果:

IaHoK.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值