Android 在任意点画圆

Android 在任意点画圆,android,Android,我有一个应用程序,我需要在活动中的随机位置随机抽取点数。然后我需要像类固醇一样把这些点移动到任何方向。我该怎么做?请看下图 好吧,如果我没听错的话,您希望为您的应用程序制作一些“小行星” 这并不特定于Android,但您可能需要在应用程序中将小行星定义为一个实体,当您需要小行星时,您只需创建一个随机数量的小行星,并带有随机位置(您可能希望检查该位置是否已经有小行星或其他物体,以避免碰撞) 除此之外,您只需要为每个小行星指定一个速度(在2D平面中,X和Y速度),并随着应用程序的进展在循环中相应地更

我有一个应用程序,我需要在活动中的随机位置随机抽取点数。然后我需要像类固醇一样把这些点移动到任何方向。我该怎么做?请看下图


好吧,如果我没听错的话,您希望为您的应用程序制作一些“小行星”

这并不特定于Android,但您可能需要在应用程序中将小行星定义为一个实体,当您需要小行星时,您只需创建一个随机数量的小行星,并带有随机位置(您可能希望检查该位置是否已经有小行星或其他物体,以避免碰撞)

除此之外,您只需要为每个小行星指定一个速度(在2D平面中,X和Y速度),并随着应用程序的进展在循环中相应地更新该速度

这是一个简单的例子,但下面是:

//To make things easier, let's assume you have an Entity class, from which every game object is inherited
public abstract class Entity {

   // Fields used to know object position
   private float x;
   private float y;

   // Fields used to calculate object motion
   private float x_speed;
   private float y_speed;

   ...

   // You would probably have a generic method to draw every entity - details are not relevant to your question, but you should draw the object taking it's x and y coordinates into account here
   public void draw() { ... }

   // Generic function to update the object's position regarding its speed
   public void updatePosition() {
      this.x += this.x_speed;
      this.y += this.y_speed;
   }

  ...

}

//Let's say you have an Asteroid class, which represents each asteroid

public class Asteroid extends Entity {

   // Just add a constructor to set it's initial position and speed
   public Asteroid(float initial_x, float initial_y, float ini_x_speed, float ini_y_speed) {
      this.x = initial_x;
      this.y = initial_y;
      this.x_speed = ini_x_speed;
      this.y_speed = ini_y_speed;
   }
}
从这里开始,您只需创建随机数量的小行星对象,具有随机位置,并在应用程序的主循环上为每个实体调用updatePosition和draw方法


编辑:哦,别忘了“清除”在每个循环周期中绘制的内容,这样您就不会在原来的位置看到已绘制的对象了。:)

好吧,如果我没听错的话,您希望为您的应用程序制作一些“小行星”

这并不特定于Android,但您可能需要在应用程序中将小行星定义为一个实体,当您需要小行星时,您只需创建一个随机数量的小行星,并带有随机位置(您可能希望检查该位置是否已经有小行星或其他物体,以避免碰撞)

除此之外,您只需要为每个小行星指定一个速度(在2D平面中,X和Y速度),并随着应用程序的进展在循环中相应地更新该速度

这是一个简单的例子,但下面是:

//To make things easier, let's assume you have an Entity class, from which every game object is inherited
public abstract class Entity {

   // Fields used to know object position
   private float x;
   private float y;

   // Fields used to calculate object motion
   private float x_speed;
   private float y_speed;

   ...

   // You would probably have a generic method to draw every entity - details are not relevant to your question, but you should draw the object taking it's x and y coordinates into account here
   public void draw() { ... }

   // Generic function to update the object's position regarding its speed
   public void updatePosition() {
      this.x += this.x_speed;
      this.y += this.y_speed;
   }

  ...

}

//Let's say you have an Asteroid class, which represents each asteroid

public class Asteroid extends Entity {

   // Just add a constructor to set it's initial position and speed
   public Asteroid(float initial_x, float initial_y, float ini_x_speed, float ini_y_speed) {
      this.x = initial_x;
      this.y = initial_y;
      this.x_speed = ini_x_speed;
      this.y_speed = ini_y_speed;
   }
}
从这里开始,您只需创建随机数量的小行星对象,具有随机位置,并在应用程序的主循环上为每个实体调用updatePosition和draw方法

编辑:哦,别忘了“清除”在每个循环周期中绘制的内容,这样您就不会在原来的位置看到已绘制的对象了。:)

看看

在onDraw方法中,使用屏幕的宽度和高度创建一个随机对象,并根据需要多次在这些点上绘制点

和onTouchevent()检查链接中的方法更改这些点的位置

在onDraw方法中,使用屏幕的宽度和高度创建一个随机对象,并根据需要多次在这些点上绘制点


和onTouchevent()检查链接中的方法更改这些点的位置

谢谢你提到我的博客:)是的,忘了提及礼貌,沃伦·费思先生你的博客真的帮了我很多,我的侧牙非常感谢你提到我的博客:)是的,忘了提及礼貌,沃伦·费思先生你的博客真的帮了我很多,非常感谢我这边也是