尝试获取在 android 中绘制的每个随机圆的 x、y 坐标
Trying to get the x, y coordinate of each random circle that is drawn in android
我正在制作一款会在屏幕上随机创建圆圈的游戏。随机创建的圆圈将具有红色或绿色的值。
我的问题是,我希望不仅能够确定用户何时点击其中一个圆圈,而且能够确定他们最终点击了哪个圆圈(红色或绿色)。
我的主要问题是试图找到将要绘制的圆的 x 和 y。
这是我的代码:
public class DrawingView extends View {
public DrawingView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
RectF rectf = new RectF(0, 0, 200, 0);
private static final int w = 100;
public static int lastColor = Color.BLACK;
private final Random random = new Random();
private final Paint paint = new Paint();
private final int radius = 230;
private final Handler handler = new Handler();
public static int redColor = Color.RED;
public static int greenColor = Color.GREEN;
private final Runnable updateCircle = new Runnable() {
@Override
public void run() {
lastColor = random.nextInt(2) == 1 ? redColor : greenColor;
paint.setColor(lastColor);
invalidate();
handler.postDelayed(this, 1000);
}
};
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
handler.post(updateCircle);
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
handler.removeCallbacks(updateCircle);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// your other stuff here
// code below works when its here
float randomWidth =(int) (random.nextInt(Math.abs(getWidth()-radius/2)) + radius/2f);
float randomHeight = (random.nextInt((int)Math.abs((getHeight()-radius/2 + radius/2f))));
canvas.drawCircle(randomWidth, randomHeight + radius/2f, radius, paint);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
int x = event.getX();
int y = event.getY();
if(isInsideCircle(x, y)){
//Do your things here
}
return true;
}
private boolean isInsideCircle(int x, int y){
if (((x - randomWidth)^2 + (y - randomHeight)^2) < radius^2)
return true;
return false;
}
}
下面的代码不起作用
public class DrawingView extends View {
public DrawingView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
RectF rectf = new RectF(0, 0, 200, 0);
private static final int w = 100;
public static int lastColor = Color.BLACK;
private final Random random = new Random();
private final Paint paint = new Paint();
private final int radius = 230;
private final Handler handler = new Handler();
public static int redColor = Color.RED;
public static int greenColor = Color.GREEN;
//its right below here the code compiles perfectly, but gets errors in logcat
int randomWidth =(int) (random.nextInt(Math.abs(getWidth()-radius/2)) + radius/2f);
int randomHeight = (random.nextInt((int)Math.abs((getHeight()-radius/2 + radius/2f))));
private final Runnable updateCircle = new Runnable() {
@Override
public void run() {
lastColor = random.nextInt(2) == 1 ? redColor : greenColor;
paint.setColor(lastColor);
invalidate();
handler.postDelayed(this, 1000);
}
};
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
handler.post(updateCircle);
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
handler.removeCallbacks(updateCircle);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// your other stuff here
canvas.drawCircle(randomWidth, randomHeight + radius/2f, radius, paint);
}
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
if(isInsideCircle(x, y)){
//Do your things here
}
return true;
}
private boolean isInsideCircle(int x, int y){
if (((x - randomWidth)^2 + (y - randomHeight)^2) < radius^2)
return true;
return false;
}
}
这里是 logcat
04-01 03:41:48.856: E/AndroidRuntime(4010): FATAL EXCEPTION: main
04-01 03:41:48.856: E/AndroidRuntime(4010): Process: com.Tripps.thesimplegame, PID: 4010
04-01 03:41:48.856: E/AndroidRuntime(4010): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Tripps.thesimplegame/com.Tripps.thesimplegame.Main}: java.lang.IllegalArgumentException: n <= 0: 0
04-01 03:41:48.856: E/AndroidRuntime(4010): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
04-01 03:41:48.856: E/AndroidRuntime(4010): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
04-01 03:41:48.856: E/AndroidRuntime(4010): at android.app.ActivityThread.access0(ActivityThread.java:144)
04-01 03:41:48.856: E/AndroidRuntime(4010): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
04-01 03:41:48.856: E/AndroidRuntime(4010): at android.os.Handler.dispatchMessage(Handler.java:102)
04-01 03:41:48.856: E/AndroidRuntime(4010): at android.os.Looper.loop(Looper.java:135)
04-01 03:41:48.856: E/AndroidRuntime(4010): at android.app.ActivityThread.main(ActivityThread.java:5221)
04-01 03:41:48.856: E/AndroidRuntime(4010): at java.lang.reflect.Method.invoke(Native Method)
04-01 03:41:48.856: E/AndroidRuntime(4010): at java.lang.reflect.Method.invoke(Method.java:372)
04-01 03:41:48.856: E/AndroidRuntime(4010): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
04-01 03:41:48.856: E/AndroidRuntime(4010): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
04-01 03:41:48.856: E/AndroidRuntime(4010): Caused by: java.lang.IllegalArgumentException: n <= 0: 0
04-01 03:41:48.856: E/AndroidRuntime(4010): at java.util.Random.nextInt(Random.java:182)
04-01 03:41:48.856: E/AndroidRuntime(4010): at com.Tripps.thesimplegame.DrawingView.<init>(DrawingView.java:38)
04-01 03:41:48.856: E/AndroidRuntime(4010): at com.Tripps.thesimplegame.Main.onCreate(Main.java:30)
04-01 03:41:48.856: E/AndroidRuntime(4010): at android.app.Activity.performCreate(Activity.java:5933)
04-01 03:41:48.856: E/AndroidRuntime(4010): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
04-01 03:41:48.856: E/AndroidRuntime(4010): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
04-01 03:41:48.856: E/AndroidRuntime(4010): ... 10 more
你所说的randomWidth
和randomheight
实际上是你的圆的x和y坐标。
这是方法标志:
canvas.drawCircle(x, y, radius, paint);
要检查用户是否在圆圈内点击,您可以这样做:
public boolean onTouch(View v, MotionEvent event) {
int x = (int)event.getX();
int y = (int)event.getY();
if(isInsideCircle(x, y)){
//Do your things here
}
return true;
}
private boolean isInsideCircle(int x, int y){
if (((x - center_x)^2 + (y - center_y)^2) < radius^2)
return true;
return false;
}
我正在制作一款会在屏幕上随机创建圆圈的游戏。随机创建的圆圈将具有红色或绿色的值。
我的问题是,我希望不仅能够确定用户何时点击其中一个圆圈,而且能够确定他们最终点击了哪个圆圈(红色或绿色)。 我的主要问题是试图找到将要绘制的圆的 x 和 y。
这是我的代码:
public class DrawingView extends View {
public DrawingView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
RectF rectf = new RectF(0, 0, 200, 0);
private static final int w = 100;
public static int lastColor = Color.BLACK;
private final Random random = new Random();
private final Paint paint = new Paint();
private final int radius = 230;
private final Handler handler = new Handler();
public static int redColor = Color.RED;
public static int greenColor = Color.GREEN;
private final Runnable updateCircle = new Runnable() {
@Override
public void run() {
lastColor = random.nextInt(2) == 1 ? redColor : greenColor;
paint.setColor(lastColor);
invalidate();
handler.postDelayed(this, 1000);
}
};
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
handler.post(updateCircle);
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
handler.removeCallbacks(updateCircle);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// your other stuff here
// code below works when its here
float randomWidth =(int) (random.nextInt(Math.abs(getWidth()-radius/2)) + radius/2f);
float randomHeight = (random.nextInt((int)Math.abs((getHeight()-radius/2 + radius/2f))));
canvas.drawCircle(randomWidth, randomHeight + radius/2f, radius, paint);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
int x = event.getX();
int y = event.getY();
if(isInsideCircle(x, y)){
//Do your things here
}
return true;
}
private boolean isInsideCircle(int x, int y){
if (((x - randomWidth)^2 + (y - randomHeight)^2) < radius^2)
return true;
return false;
}
}
下面的代码不起作用
public class DrawingView extends View {
public DrawingView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
RectF rectf = new RectF(0, 0, 200, 0);
private static final int w = 100;
public static int lastColor = Color.BLACK;
private final Random random = new Random();
private final Paint paint = new Paint();
private final int radius = 230;
private final Handler handler = new Handler();
public static int redColor = Color.RED;
public static int greenColor = Color.GREEN;
//its right below here the code compiles perfectly, but gets errors in logcat
int randomWidth =(int) (random.nextInt(Math.abs(getWidth()-radius/2)) + radius/2f);
int randomHeight = (random.nextInt((int)Math.abs((getHeight()-radius/2 + radius/2f))));
private final Runnable updateCircle = new Runnable() {
@Override
public void run() {
lastColor = random.nextInt(2) == 1 ? redColor : greenColor;
paint.setColor(lastColor);
invalidate();
handler.postDelayed(this, 1000);
}
};
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
handler.post(updateCircle);
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
handler.removeCallbacks(updateCircle);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// your other stuff here
canvas.drawCircle(randomWidth, randomHeight + radius/2f, radius, paint);
}
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
if(isInsideCircle(x, y)){
//Do your things here
}
return true;
}
private boolean isInsideCircle(int x, int y){
if (((x - randomWidth)^2 + (y - randomHeight)^2) < radius^2)
return true;
return false;
}
}
这里是 logcat
04-01 03:41:48.856: E/AndroidRuntime(4010): FATAL EXCEPTION: main
04-01 03:41:48.856: E/AndroidRuntime(4010): Process: com.Tripps.thesimplegame, PID: 4010
04-01 03:41:48.856: E/AndroidRuntime(4010): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Tripps.thesimplegame/com.Tripps.thesimplegame.Main}: java.lang.IllegalArgumentException: n <= 0: 0
04-01 03:41:48.856: E/AndroidRuntime(4010): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
04-01 03:41:48.856: E/AndroidRuntime(4010): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
04-01 03:41:48.856: E/AndroidRuntime(4010): at android.app.ActivityThread.access0(ActivityThread.java:144)
04-01 03:41:48.856: E/AndroidRuntime(4010): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
04-01 03:41:48.856: E/AndroidRuntime(4010): at android.os.Handler.dispatchMessage(Handler.java:102)
04-01 03:41:48.856: E/AndroidRuntime(4010): at android.os.Looper.loop(Looper.java:135)
04-01 03:41:48.856: E/AndroidRuntime(4010): at android.app.ActivityThread.main(ActivityThread.java:5221)
04-01 03:41:48.856: E/AndroidRuntime(4010): at java.lang.reflect.Method.invoke(Native Method)
04-01 03:41:48.856: E/AndroidRuntime(4010): at java.lang.reflect.Method.invoke(Method.java:372)
04-01 03:41:48.856: E/AndroidRuntime(4010): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
04-01 03:41:48.856: E/AndroidRuntime(4010): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
04-01 03:41:48.856: E/AndroidRuntime(4010): Caused by: java.lang.IllegalArgumentException: n <= 0: 0
04-01 03:41:48.856: E/AndroidRuntime(4010): at java.util.Random.nextInt(Random.java:182)
04-01 03:41:48.856: E/AndroidRuntime(4010): at com.Tripps.thesimplegame.DrawingView.<init>(DrawingView.java:38)
04-01 03:41:48.856: E/AndroidRuntime(4010): at com.Tripps.thesimplegame.Main.onCreate(Main.java:30)
04-01 03:41:48.856: E/AndroidRuntime(4010): at android.app.Activity.performCreate(Activity.java:5933)
04-01 03:41:48.856: E/AndroidRuntime(4010): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
04-01 03:41:48.856: E/AndroidRuntime(4010): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
04-01 03:41:48.856: E/AndroidRuntime(4010): ... 10 more
你所说的randomWidth
和randomheight
实际上是你的圆的x和y坐标。
这是方法标志:
canvas.drawCircle(x, y, radius, paint);
要检查用户是否在圆圈内点击,您可以这样做:
public boolean onTouch(View v, MotionEvent event) {
int x = (int)event.getX();
int y = (int)event.getY();
if(isInsideCircle(x, y)){
//Do your things here
}
return true;
}
private boolean isInsideCircle(int x, int y){
if (((x - center_x)^2 + (y - center_y)^2) < radius^2)
return true;
return false;
}