gpt4 book ai didi

java - 如何使按钮每秒在布局中随机移动

转载 作者:行者123 更新时间:2023-12-02 04:24:46 25 4
gpt4 key购买 nike

好吧,我在这里有这样的 xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="30dp"
android:background="@drawable/backgroundblankred" >

<Button
android:id="@+id/button1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/eeclevelfirstblackbuton" />

</RelativeLayout>

</RelativeLayout>

显示是这样的

enter image description here

按钮位于布局内,所以我想让按钮在单击时像这样

enter image description here

按钮每秒随机在布局内移动,而不是从布局中移动,这会使应用程序强制关闭

第二个问题,如何设置按钮随机移动的速度?比如设置按钮每2秒随机移动一次并增加得更快,是否可以设置按钮移动得更快?

我现在只有这个代码

public class tested extends Activity {

Button buttonblack;
int score=0;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.tested);


buttonblack = (Button)findViewById(R.id.black1);
buttonblack.setOnClickListener(new View.OnClickListener() {

public void onClick(View v){
//If clicked, Move the button randomly
}
});

有人可以帮忙编写一些代码吗?提前致谢。

最佳答案

希望该视频对第一部分有所帮助:Android App Development for Beginners - 34 - Animations and Transitions .

对于第二部分,您可以使用如下循环:

while(true){
delay(time);//time can be taken input from any EditText.
placeButton(x,y); // x and y are random numbers
}

关于java - 如何使按钮每秒在布局中随机移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32335445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com