如何使一个按钮随机移动 [英] How to make a Button randomly Move

查看:658
本文介绍了如何使一个按钮随机移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿家伙,我有一个关于如何使一个按钮,随机移动的问题每秒

Hey Guys i have a Question about how to make a button randomly move every second

黑瓦是一个按钮

输入图像的描述在这里

所以我要让它在随机移动的每一秒或更快

so i want to make it move randomly in every second or more fast

输入图像的描述在这里

这是我的XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/backgroundblank" >
                  <Button
                    android:id="@+id/Button01"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/black1" />
</RelativeLayout>

这是在code

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){
              //score+10(i dont know how to make score +10 if the button clicked)
              //if the button clicked
              //Do some logic here
                                         }                
            });

if (score = 100){
         //the speed of move are increase more fast
                }

}

有人能帮助我吗?

anyone can help me?

推荐答案

首先,你应该得到的屏幕尺寸

First you should get the screen size

public static Point getDisplaySize(@NonNull Context context) {
    Point point = new Point();
    WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    manager.getDefaultDisplay().getSize(point);
    return point;
}

然后,你应该得到一个随机x和随机y位置的按钮即可进入,使得其仍然在屏幕上

Then you should get a random x and random y position for the button to go to so that its still on screen

private void setButtonRandomPosition(Button button){
    int randomX = new Random().nextInt(getDisplaySize(this).x);
    int randomY = new Random().nextInt(getDisplaySize(this).y);

    button.setX(randomX);
    button.setY(randomY);
}

最后,要做到这一点每一秒

Finally to make this happen every second

private void startRandomButton(Button button) {
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            setButtonRandomPosition(button);
        }
    }, 0, 1000);//Update button every second
}

在上创建运行它像这样

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

    startRandomButton(blackbutton);
}

这篇关于如何使一个按钮随机移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆