Java 如何使按钮每秒在布局中随机移动
好的,这里有这样的xmlJava 如何使按钮每秒在布局中随机移动,java,android,button,random,Java,Android,Button,Random,好的,这里有这样的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_widt
<?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>
有人能帮我写一些代码吗?提前感谢。希望此视频对第一部分有所帮助: 对于第二部分,您可以使用如下循环:
while(true){
delay(time);//time can be taken input from any EditText.
placeButton(x,y); // x and y are random numbers
}
在视图区域内生成随机整数 1) 这应该是您正在寻找的伪代码
int x = rnd(view.getX(), view.getX()+view.getWidth());
int y = rnd(view.getY(), view.getY()+view.getHeight());
view.setTranslationX(x);
view.setTranslationY(y);
2) 使用一个可以修改睡眠时间的线程,需要在主线程上调用ui更改,使用处理程序或asynctask进行处理可能的重复ok视频确实有帮助,但如果每秒随机设置按钮,是否有可能?摘自编辑文本?
int x = rnd(view.getX(), view.getX()+view.getWidth());
int y = rnd(view.getY(), view.getY()+view.getHeight());
view.setTranslationX(x);
view.setTranslationY(y);