如何将重力设置为任何物体或屏幕中心?我很好奇其背后的逻辑。
我有两个圆形物体:aBody,它是静态物体,bBody,它是动态物体。世界的重力设置为 (0, 0)。
我希望它类似于链接中的图像:

我一直试图将这个自定义视图集中在我扩展LinearLayout的其他自定义视图中.这一切都需要通过代码来完成,因为它们都是在运行时完成的.
我已经尝试了设置引力的标准方法:
this.setGravity(Gravity.CENTER);
Run Code Online (Sandbox Code Playgroud)
这是在我的类中完成的,它扩展了LinearLayout.
我也尝试过LayoutParams方法:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
block.setLayoutParams(params);
this.addView(block);
Run Code Online (Sandbox Code Playgroud)
这是在我将块添加到视图的地方完成的(如您所见).
两种方法都导致块仍然在我视图的左上角对齐.我有什么其他选择,或者更重要的是,我做错了什么?
我试图弄清楚如何将重力应用到我正在创建的应用程序中.我在Opengl有一个球体,我想要像行星一样赋予它重力.所以靠近它的任何小物体都会"掉落"到它的表面.我在网上的每一个地方都显示公式,但从不显示例子.我想知道是否有人能指出我在c ++中的一个例子的方向.我真的不想使用物理库,我希望能够看一下这个例子并从中学习以便自己理解它.
我正在编写一个程序来模拟一个n体重力系统,它的精度是任意的,取决于我在每一步之间采取的"时间"步长.现在,它可以非常快速地运行多达500个物体,但之后它变得非常慢,因为它必须通过一个算法来确定每次迭代在每对物体之间施加的力.这是复杂的n(n + 1)/ 2 = O(n ^ 2),因此它很快变得非常糟糕也就不足为奇了.我想最昂贵的操作是我通过取平方根确定每对之间的距离.所以,在伪代码中,这就是我的算法当前运行的方式:
for (i = 1 to number of bodies - 1) {
for (j = i to number of bodies) {
(determining the force between the two objects i and j,
whose most costly operation is a square root)
}
}
Run Code Online (Sandbox Code Playgroud)
那么,有什么方法可以优化这个吗?任何花哨的算法可以重复使用过去迭代中使用的距离进行快速修改?有什么有损方法可以减少这个问题吗?也许忽略了x或y坐标(它是2维)超过一定数量的物体之间的关系,这是由它们的质量乘积决定的?对不起,如果这听起来像是在漫无边际,但是我能做些什么才能让它更快?我宁愿保持任意精确,但如果有一些解决方案能够以一点精度降低这个问题的复杂性,我会有兴趣听到它.
谢谢.
我有这个代码:
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout02"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView android:id="@+id/terranlogo1"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/terranlogo"
android:layout_centerHorizontal="true"
/>
<ImageView android:id="@+id/protosslogo1"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="top|right"
android:src="@drawable/protosslogo"
/>
<ImageView android:id="@+id/zergologo1"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="top|left"
android:src="@drawable/zerglogo"
/>
Run Code Online (Sandbox Code Playgroud)
右侧的图像视图未显示.其他两个都很好,我甚至尝试将它们设置为50 dp并且"protosslogo1"仍然没有显示.我究竟做错了什么?
谢谢,所有帮助表示赞赏 - Lijap
我想将文本设置在textview的中心(垂直和水平).它是垂直水平居中的,它在文本的顶部有更多的空间.
xml布局:
<TextView
android:id="@+id/txtQuantity"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center|center_vertical"
android:text="+"
android:textColor="@color/white"
android:textSize="25sp" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud) 我正在用pygame制作一个平台游戏,我想增加它的重力.现在我只有一张按下箭头键时会移动的图片,我的下一步就是引力.这是我的代码:
import pygame, sys
from pygame.locals import *
pygame.init()
FPS = 30
fpsClock = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((400, 300), 0, 32)
pygame.display.set_caption("Jadatja")
WHITE = (255, 255, 255)
catImg = pygame.image.load("images/cat.png")
catx = 10
caty = 10
movingRight = False
movingDown = False
movingLeft = False
movingUp = False
while True: #main game loop
#update
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_RIGHT:
#catx += 5
movingRight = True
movingLeft = False
elif event.key == K_DOWN:
#caty …Run Code Online (Sandbox Code Playgroud) 如何设置AbsListView.LayoutParams的布局重力?
我在自定义数组适配器中使用它,我需要根据变量设置布局重力.我的列表子包含一个线性布局根,但在适配器中,我无法设置LinearLayout参数,因为我得到的错误是LinearLayout.LayoutParams无法转换为AbsListView.Layout参数.
因此,我尝试为AbsListView设置layout_gravity,但它没有重力选项.
在这种情况下,如何以编程方式设置layout_gravity?
编辑:(添加源代码)
Listview Parent
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/messaging_background"
android:orientation="vertical"
android:padding="2dp" >
<ListView
android:id="@+id/messageThread"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/list_separator"
android:layout_alignParentTop="true"
android:layout_marginTop="5dp"
android:cacheColorHint="#00000000"
android:divider="@null"
android:dividerHeight="2dp"
android:fastScrollEnabled="false"
android:listSelector="#00000000"
android:transcriptMode="disabled" />
<View
android:id="@+id/list_separator"
android:layout_width="match_parent"
android:layout_height="1.0dip"
android:layout_above="@+id/footer"
android:background="@android:color/white" />
<LinearLayout
android:id="@+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/filter"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="0.1"
android:background="@null"
android:contentDescription="@string/message_filter_button"
android:scaleType="fitCenter"
android:src="@drawable/filter" />
<EditText
android:id="@+id/send_message_text"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.8"
android:imeOptions="actionSend"
android:inputType="textMultiLine"
android:maxLines="6"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarDefaultDelayBeforeFade="200"
android:scrollbarFadeDuration="300"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="vertical" />
<ImageButton
android:id="@+id/send"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="0.1"
android:background="@null"
android:contentDescription="@string/send_message_button"
android:scaleType="fitCenter" …Run Code Online (Sandbox Code Playgroud) 我有一个TextView与其他视图重叠.一切都很好,除了API上的重力等于或高于18.
这是我的TextView:
<TextView
android:id="@+id/overlappingTextView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignBottom="@id/behindLinearLayout"
android:layout_alignLeft="@id/behindLinearLayout"
android:layout_alignRight="@id/behindLinearLayout"
android:layout_alignTop="@id/behindLinearLayout"
android:background="@drawable/overlapping_bg"
android:gravity="center"
android:text="Waiting for user confirmation..."
android:textColor="@color/blue" />
Run Code Online (Sandbox Code Playgroud)
重力在4.2.2或更低的情况下正常工作但在4.3及更高版本上无效...它只能部分工作,它只是居中但只是垂直.
LE:
这个问题没有解决办法吗?或者有人知道为什么会这样吗?