【问题标题】:How to make a Button randomly Move如何使按钮随机移动
【发布时间】:2015-11-22 15:41:36
【问题描述】:

我有一个关于如何让按钮每秒随机移动的问题。

黑色瓷砖是一个按钮:

所以我想让它每秒随机移动或更快。

这是我的 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>

这是代码

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

谁能帮帮我?

【问题讨论】:

  • 查看我的更新答案

标签: java android button random


【解决方案1】:

首先你应该得到屏幕尺寸

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 位置让按钮转到,以便它仍然在屏幕上

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

终于让这一切发生在每一秒

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

【讨论】:

  • 好的兄弟,但我还有另一个问题,请参阅我的更新问题
【解决方案2】:

Activity onCreate 使用此代码

 Button button = (Button)findViewById(R.id.my_button);

创建方法

   public void buttonmove()
  {
  RelativeLayout .LayoutParams absParams = (RelativeLayout .LayoutParams)button.getLayoutParams();
  DisplayMetrics displaymetrics = new DisplayMetrics();
  getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
  int width = displaymetrics.widthPixels;
  int height = displaymetrics.heightPixels;
  Random r = new Random();
  absParams.x =  r.nextInt(width) ;
  absParams.y =  r.nextInt(height);
  button.setLayoutParams(absParams);
      }                 

如果你想在特定时间使用计时器

 Timer t=new Timer();
t.schedule(new TimerTask() {
public void run() {
  buttonmove();//call method
}
 }, new SimpleDateFormat("HH:mm:ss").parse("13:40:20"));//set time here    

【讨论】:

  • 以及如何设置按钮移动的计时器?
猜你喜欢
  • 2023-03-20
  • 1970-01-01
  • 2016-05-20
  • 1970-01-01
  • 2015-11-26
  • 1970-01-01
  • 2014-11-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多