我正在制作Android应用。目标是制作圈子,当触摸某事时发生。更详细的这个应用程序将显示2种类型的圆圈,红色或绿色圆圈。圆的颜色存储在lastColor变量中。颜色的目的是当用户触摸"发生了一些红色或绿色的圆圈。例如,如果触摸绿色圆圈,则会在该分数中添加一个点,或者如果单击红色圆圈,则会更改活动,但是当单击任一个圆圈时都不会发生任何事情。这是onTouch方法
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
if(isInsideCircle(x, y) == true){
//Do your things here
if(redColor == lastColor){
Intent i = new Intent(v.getContext(), YouFailed.class);
v.getContext().startActivity(i);
} else {
addPoints++;
}
}else {
}
return true;
}
这部分代码是无法正常运行的。以下是整个班级。
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;
int randomWidth = 0;
int randomHeight = 0;
public static int addPoints = 0;
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
if (random == null) {
randomWidth = (int) (random.nextInt(Math.abs(getWidth() - radius / 2)) + radius / 2f);
randomHeight = (random.nextInt((int) Math.abs((getHeight() - radius / 2 + radius / 2f))));
}
else {
randomWidth = (int) (random.nextInt(Math.abs(getWidth() - radius / 2)) + radius / 2f);
randomHeight = (random.nextInt((int) Math.abs((getHeight() - radius / 2 + radius / 2f))));
}
canvas.drawCircle(randomWidth, randomHeight + radius / 2f, radius, paint);
paint.setColor(Color.BLACK);
paint.setTextSize(150);
canvas.drawText("Score: " + addPoints, 120, 300, paint);
}
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
if (isInsideCircle(x, y) == true) {
//Do your things here
if (redColor == lastColor) {
Intent i = new Intent(v.getContext(), YouFailed.class);
v.getContext().startActivity(i);
}
else {
addPoints++;
}
}
else {
}
return true;
}
public boolean isInsideCircle(int x, int y) {
if ((((x - randomWidth) * (x - randomWidth)) + ((y - randomHeight) * (y - randomHeight))) < ((radius) * (radius)))
return true;
return false;
}
}
答案 0 :(得分:0)
您需要覆盖onTouchEvent函数。我测试了代码,它完全有效!另外,确保在addPoints ++之后返回false以确保只添加一个点。希望这适合你!
@Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
if (isInsideCircle(x, y)) {
//Do your things here
if (redColor == lastColor) {
Intent i = new Intent(getContext(), null);
getContext().startActivity(i);
}
else {
addPoints++;
return false;
}
}
else {
}
return true;
}