Я хочу разработать игру, в которой вы должны держать палец на экране, пока «вода» не поднимется до 3/4 высоты экрана (в моем случае между 1544 и 1623 эта область отмечена разметкой кадра) , Как сделать так, чтобы расположение макета рамки и значения максимума и минимума зависели от размера экрана?
public class GameActivity extends AppCompatActivity {
Button button_game;
TextView water;
Handler handler = new Handler();
boolean pushingDown = false;
int wincounter;
int maximum = 1623;
int minimum = 1544;
FrameLayout frameLayout;
int height;
@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
frameLayout = findViewById(R.id.frameLayout);
button_game = findViewById(R.id.button_game);
water = findViewById(R.id.water);
button_game.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
pushingDown = true;
handler.post(repeater);
}
if (event.getAction() == MotionEvent.ACTION_UP) {
pushingDown = false;
ViewGroup.LayoutParams params = water.getLayoutParams();
if (params.height > minimum && params.height <= maximum) {
wincounter = 1;
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("wincounter", wincounter);
startActivity(intent);
}
params.height = 1;
}
return GameActivity.super.onTouchEvent(event);
}
});
}
Runnable repeater = new Runnable() {
@Override
public void run() {
ViewGroup.LayoutParams params = water.getLayoutParams();
if (params.height > maximum) {
Intent intent = new Intent(getApplicationContext(), LoseActivity.class);
startActivity(intent);
} else if (pushingDown) {
params.height = params.height + 2;
water.setLayoutParams(params);
handler.post(this);
//repeating while touched
}
}
};
}