onTouchEvent()方法被调用了7次以上

时间:2019-02-13 13:15:35

标签: android

刚刚进入Android开发。.请忍受我。

我创建了一个重写的onTouchEvent函数。每次我触摸应用程序内的某个位置时,我希望它运行两次该功能。而是,函数运行随机次数。有时是7,有时是12。我只是想知道这是否正常?

如果有人好奇,这是我的代码。


package com.jojocoder.subhunter;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.view.Display;
import android.util.Log;
import android.widget.ImageView;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    int numberHorizontalPixels;
    int numberVerticalPixels;
    int blockSize;
    int gridWidth = 40;
    int gridHeight;
    float horizantalTouched = -100;
    float verticalTouched = -100;
    int subHorizaltalPosition;
    int subVerticalPosition;
    boolean hit = false;
    int shotsTaken;
    int distanceFromSub;
    boolean debugging = true;
    int testcount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);

        numberHorizontalPixels = size.x;
        numberVerticalPixels = size.y;
        blockSize = numberHorizontalPixels / gridWidth;
        gridHeight = numberVerticalPixels / blockSize;

        Log.d("Debugging","Running OnCreate func");
        newGame();
        draw();
    }

    public void newGame() {
        Log.d("Debugging", "Running newGame func");

    }

    void draw() {
        Log.d("Debugging", "Running draw func");
        printDebuggingText();

    }

    @Override
    public boolean onTouchEvent(MotionEvent motionEvent) {
        testcount ++;
        Log.d("Debugging", "Running OnTouchEvent func");
        takeShot();

        return true;
    }

    void takeShot() {
        Log.d("Debugging", "Running takeShot func");
        draw();

    }

    void boom() {

    }

    void printDebuggingText() {

        Log.d("numberHorizantalPixels", "" + numberHorizontalPixels);
        Log.d("numberVerticalPixels", "" + numberVerticalPixels);
        Log.d("blockSize", "" + blockSize);
        Log.d("gridWidth", "" + gridWidth);
        Log.d("gridHeight", "" + gridHeight);
        Log.d("horizantalTouched", "" + horizantalTouched);
        Log.d("verticalTouhced", "" + verticalTouched);
        Log.d("subHorizantalPosition", "" + subHorizaltalPosition);
        Log.d("subVerticalPosition", "" + subVerticalPosition);
        Log.d("hit", "" + hit);
        Log.d("shotsTaken", "" + shotsTaken);
        Log.d("debugging", "" + debugging);
        Log.d("distanceFronSub", "" + distanceFromSub);
        Log.d("testcount", "" + testcount);


    }
}

谢谢。

1 个答案:

答案 0 :(得分:0)

如果您使用onTouchEvent()方法来拦截“活动”或“视图”中的触摸事件。当触摸事件的压力,大小,位置等发生变化时,甚至当新的手指放在触摸屏上时,这种方法都会被多次调用。