如何检查屏幕是否未被触摸

时间:2015-05-05 23:19:50

标签: java android boolean android-canvas ontouchevent

我有一款在屏幕上随机显示圆圈的游戏。圆圈可以是随机的绿色或红色。如果你碰到一个红色的圆圈,就会发生如果你触摸绿色圆圈,会发生一些事情;但是如果你想检查绿色圆圈是否显示而用户没有点击它会怎么样?这是我的代码:

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;
    public static int savedScore;
    public static List<String> a = new ArrayList<String>();
    public static String[] savedScores = new String[a.size()];
    Paint red;
    public static int howManyPoints;
    public static int highestScore = 0;
    boolean isTouched;
    Thread newThread = new Thread();

    private final Runnable updateCircle = new Runnable() {
        @Override 
        public void run() {
            lastColor = random.nextInt(2) == 1 ? redColor : greenColor;
            paint.setColor(lastColor);
            if(lastColor == greenColor){
                isTouched = false;
            }

            if(addPoints < 10){
                handler.postDelayed(this, 850);
            }
            if(addPoints > 9 && addPoints < 30){
                handler.postDelayed(this,700);
            }
            if(addPoints > 29){
                handler.postDelayed(this, 600);
            }
            if(addPoints > 50){
                handler.postDelayed(this, 450);
            }
            if(addPoints > 100){
                handler.postDelayed(this, 400);
            }
            postInvalidate();
        }
    };

    public void what(){
        try {
            newThread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
            if(isTouched == false){
                howManyPoints = addPoints;
                handler.removeCallbacks(updateCircle);
                lastColor = redColor;
                addPoints = 0;
                Intent i = new Intent(this.getContext(), YouFailed.class);
                this.getContext().startActivity(i);
            }
        }

    @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
        Paint back = new Paint();
        back.setColor(Color.BLACK);
        Rect background = new Rect();
        background.set(0, 0, canvas.getWidth(),canvas.getHeight() );
        canvas.drawRect(background, back);

        Paint newPaint = new Paint();
        newPaint.setColor(Color.BLUE);
        newPaint.setTextSize(60);
        canvas.drawText("Beta v2", 10, 60, newPaint);

        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, paint);

        what();

        red = new Paint();
        red.setColor(Color.BLUE);
        red.setTextSize(150);
        canvas.drawText("" + addPoints, 500, 1350, red);
    }

    @SuppressWarnings("deprecation")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN){
            int x = (int) event.getX();
            int y = (int) event.getY();
                if(isInsideCircle(x, y) ==  true){
                    //Do your things here
                        if(lastColor == redColor){
                            //saveScore();
                            howManyPoints = addPoints;
                            if(howManyPoints > highestScore){
                                highestScore = howManyPoints;
                            }
                            handler.removeCallbacks(updateCircle);
                            lastColor = redColor;
                            addPoints = 0;
                            Intent i = new Intent(this.getContext(), YouFailed.class);
                            this.getContext().startActivity(i);
                        } 
                        if(lastColor == greenColor){
                            addPoints++;
                            isTouched = true;
                        }
                }else  {

                }
        }
        return false;
}

public void saveScore() {
        a.add("" + addPoints);
        //if(Integer.parseInt(savedScores[1]) < addPoints){
            //savedScores[2] = savedScores[1];
            //int x = Integer.parseInt(savedScores[1]);
            //x = addPoints;
        //}
    }

public boolean isInsideCircle(int x, int y){
  if ((((x - randomWidth)*(x - randomWidth)) + ((y - randomHeight)*(y - randomHeight))) < ((radius)*(radius))){
    return true;
  }
  return false;    
}
}

1 个答案:

答案 0 :(得分:1)

只需添加一个boolean hasBeenClicked,它是false,当用户触摸它时变为true