Я новичок в Android программировании (и программировании в целом), я пытаюсь создать приложение, которое может отслеживать события на поле для регби.
Приложение отображает изображение BitMap поля для регби и пользователь нажимает на поле, чтобы указать начальное и конечное местоположение действия на поле, такого как пас, игрок А передает игроку В. Получив координаты этих точек, можно будет проанализировать данные на более позднем этапе. чтобы помочь с коучингом et c.
Я написал подпрограмму, которая фиксирует первую и вторую точки, но она выглядит довольно неуклюжей, и мне интересно, есть ли какие-либо встроенные методы или другие подходы к программированию это может обеспечить более элегантное решение.
Я использую Android Studio 3.6.1 с java в качестве языка кода. Буду очень признателен за любую помощь или совет.
myPitch.setOnTouchListener (new View.OnTouchListener () {// myPitch - это растровое изображение поля регби, целью которого является захват событий на поле для регби по типу события: Pass, Kick, Ball Carry и др. c. // каждое из этих действий будет иметь начальную и конечную точки, цель метода onTouch, описанного ниже, состоит в том, чтобы зафиксировать местоположение первого касания на экран, а затем второе касание // это начальная и конечная точки движения шара от начальной к конечной точке.
Pass myPass = new Pass();
// myPass is an object that capture the number of the player that passed the ball and also captures the coordinates
// of where the pass was made to where it was received.
// myPass also records whether or not the pass resulted in retained possession or lost possession
@Override
public boolean onTouch(View v, MotionEvent event) {
myPass.setPlayer(player);
// player is a field within the MainActivity class, it is set when the user clicks on a player button,
// the action buttons (such as Pass) are only enabled afetr a player has been selected.
// there is a button for each player 1 to 15.
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// capture the ACTION_DOWN event on the myPitch image
Log.d(TAG, "onTouch: " + event.getX() + ", " + event.getY());
if (numClicks.equals("Waiting_1st")) {
// numClicks is a field in the MainActivity class and is set to "WaitingFirst" when it is declared
// there have been no clicks and so what we are capturing now is the 'startPoint' of the myPass event
myPass.setStartPointX((int) event.getX());
myPass.setStartPointY((int) event.getY());
// Log.d (TAG, "onTouch:" + myPass .isPoint_Start ());
}
if (numClicks.equals("Waiting_2nd")) {
// there has been at least one click but the en point has not been recorded
myPass.setEndPointX((int) event.getX());
myPass.setEndPointY((int) event.getY());
// Log.d (TAG, «Второй щелчок»: «+ myPass.getStartPointX () +», «+ myPass.getStartPointY () +», «+ myPass» .getEndPointX () + "," + myPass.getEndPointY ());}
if (numClicks.equals("Waiting_1st")) {
//to get here we must have captured at least 1 onTouch event and updated the myPass object with that info
numClicks = "Waiting_2nd";
// I need to set numClicks to "Waiting2nd" so that the next onTouch event can be captured.
} else {
numClicks = "Waiting_1st";
// reset the value of numClicks after the second onTouch event has been captured.
}
}
return true;
}
});
Приветствия
Луч