如何使一个按钮的布局每秒随机移动布局、按钮

2023-09-04 03:00:09 作者:敷衍〞怎么演

好了,所以在这里我有这样的XML像这样

Ok so in here i have this xml like this

<?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>

这是展示这样的

it is show like this

按钮是内部的布局,所以我想使按钮时,点击它会是这个样子

the button was inside a layout so i want to make the button when clicked it will be like this

按钮随机移动的布局有每秒,而不是从布局,可以使应用程序内部的力量接近

the button randomly move inside the layout there every second and not out from the layout that can make app force close

第二个问题的,如何设置快速随机按钮举动?像设置布敦随机移动,每2二,增加更快速,可以设置按钮的移动速度更快?

Second question, how to set the fast of random button move? like set the buton random move every 2 second and increase it more fast, it is possible set the button move faster?

我只是有这个code现在

i just have this code now

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

任何人都可以帮助一些code?在此先感谢。

Anyone can help with some code? Thanks in Advance.

推荐答案

希望这部影片有助于为第1部分:的 Android应用开发入门 - 34 - 动画和过渡

Hope this video helps for the 1st part: Android App Development for Beginners - 34 - Animations and Transitions.

有关第二部分,你可以使用一个循环是这样的:

For second part you may use a loop like this:

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