gpt4 book ai didi

android - onTouchEvent() 方法被调用超过 7 次

转载 作者:行者123 更新时间:2023-11-30 05:05:17 24 4
gpt4 key购买 nike

刚接触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);


}
}

提前致谢。

最佳答案

如果您使用 onTouchEvent() 方法来拦截 Activity 或 View 中的触摸事件。当触摸事件的压力、大小、位置等发生变化时,甚至当新手指放在触摸屏上时,都会多次调用此方法。

关于android - onTouchEvent() 方法被调用超过 7 次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54671113/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com