こんにちはコミュニティ私はアンドロイドとアンドロイドのスタジオで作業を始めました。 私はこのすべてに新しいですが、私は試しています。異なる場所にあるボタン
ここに問題があります 私は同じサイズ(70x70dp)の6つのボタンを持っています 私はこの6つのボタンを画面の別の場所に生成するが同じではないようにします。
私はすでにそれを作りましたが、ほとんどの時、いくつかのボタンは他のボタンよりも上になります。 私は何とかこれを制限したい。
私はしばらく試してみましたが、いくつかのおしゃれな小切手とすべての組み合わせは試しましたが、これまでのところ何もできませんでした。
private void changeButtonPlace(Button button) {
Random r = new Random();
int newXloc = r.nextInt(getScreenWidth() - (2 * button.getLayoutParams().width));
int newYloc = r.nextInt(getScreenHeight() - (3 * button.getLayoutParams().height));
if (button == _button) {
while (Math.abs(newXloc - _button1.getX()) < 10 || Math.abs(newXloc - _button2.getX()) < 10
|| Math.abs(newXloc - _button3.getX()) < 10 || Math.abs(newXloc - _button4.getX()) < 10
|| Math.abs(newXloc - _button5.getX()) < 10){
Log.d("myTag", "KSANA UPOLOGISA");
newXloc = r.nextInt(getScreenWidth() - (2 * button.getLayoutParams().width));
}
while (Math.abs(newYloc - _button1.getY()) < 10 || Math.abs(newYloc - _button2.getY()) < 10
|| Math.abs(newYloc - _button3.getY()) < 10 || Math.abs(newYloc - _button4.getY()) < 10
|| Math.abs(newYloc - _button5.getY()) < 10){
newYloc = r.nextInt(getScreenWidth() - (2 * button.getLayoutParams().height));
}
} else if (button == _button1) {
while (Math.abs(newXloc - _button.getX()) < 10 || Math.abs(newXloc - _button2.getX()) < 10
|| Math.abs(newXloc - _button3.getX()) < 10 || Math.abs(newXloc - _button4.getX()) < 10
|| Math.abs(newXloc - _button5.getX()) < 10){
newXloc = r.nextInt(getScreenWidth() - (2 * button.getLayoutParams().width));
}
while (Math.abs(newYloc - _button1.getY()) < 10 || Math.abs(newYloc - _button2.getY()) < 10
|| Math.abs(newYloc - _button3.getY()) < 10 || Math.abs(newYloc - _button4.getY()) < 10
|| Math.abs(newYloc - _button5.getY()) < 10){
newYloc = r.nextInt(getScreenWidth() - (2 * button.getLayoutParams().height));
}
} else if (button == _button2) {
while (Math.abs(newXloc - _button.getX()) < 10 || Math.abs(newXloc - _button1.getX()) < 10
|| Math.abs(newXloc - _button3.getX()) < 10 || Math.abs(newXloc - _button4.getX()) < 10
|| Math.abs(newXloc - _button5.getX()) < 10){
newXloc = r.nextInt(getScreenWidth() - (2 * button.getLayoutParams().width));
}
while (Math.abs(newYloc - _button1.getY()) < 10 || Math.abs(newYloc - _button2.getY()) < 10
|| Math.abs(newYloc - _button3.getY()) < 10 || Math.abs(newYloc - _button4.getY()) < 10
|| Math.abs(newYloc - _button5.getY()) < 10){
newYloc = r.nextInt(getScreenWidth() - (2 * button.getLayoutParams().height));
}
} else if (button == _button3) {
while (Math.abs(newXloc - _button.getX()) < 10 || Math.abs(newXloc - _button1.getX()) < 10
|| Math.abs(newXloc - _button2.getX()) < 10 || Math.abs(newXloc - _button4.getX()) < 10
|| Math.abs(newXloc - _button5.getX()) < 10){
newXloc = r.nextInt(getScreenWidth() - (2 * button.getLayoutParams().width));
}
while (Math.abs(newYloc - _button1.getY()) < 10 || Math.abs(newYloc - _button2.getY()) < 10
|| Math.abs(newYloc - _button2.getY()) < 10 || Math.abs(newYloc - _button4.getY()) < 10
|| Math.abs(newYloc - _button5.getY()) < 10){
newYloc = r.nextInt(getScreenWidth() - (2 * button.getLayoutParams().height));
}
} else if (button == _button4) {
while (Math.abs(newXloc - _button.getX()) < 10 || Math.abs(newXloc - _button1.getX()) < 10
|| Math.abs(newXloc - _button2.getX()) < 10 || Math.abs(newXloc - _button3.getX()) < 10
|| Math.abs(newXloc - _button5.getX()) < 10){
newXloc = r.nextInt(getScreenWidth() - (2 * button.getLayoutParams().width));
}
while (Math.abs(newYloc - _button1.getY()) < 10 || Math.abs(newYloc - _button2.getY()) < 10
|| Math.abs(newYloc - _button2.getY()) < 10 || Math.abs(newYloc - _button3.getY()) < 10
|| Math.abs(newYloc - _button5.getY()) < 10){
newYloc = r.nextInt(getScreenWidth() - (2 * button.getLayoutParams().height));
}
} else if (button == _button5) {
while (Math.abs(newXloc - _button.getX()) < 10 || Math.abs(newXloc - _button1.getX()) < 10
|| Math.abs(newXloc - _button2.getX()) < 10 || Math.abs(newXloc - _button4.getX()) < 10
|| Math.abs(newXloc - _button3.getX()) < 10){
newXloc = r.nextInt(getScreenWidth() - (2 * button.getLayoutParams().width));
}
while (Math.abs(newYloc - _button1.getY()) < 10 || Math.abs(newYloc - _button2.getY()) < 10
|| Math.abs(newYloc - _button2.getY()) < 10 || Math.abs(newYloc - _button4.getY()) < 10
|| Math.abs(newYloc - _button3.getY()) < 10){
newYloc = r.nextInt(getScreenWidth() - (2 * button.getLayoutParams().height));
}
}
button.setX(newXloc);
button.setY(newYloc);
}
多分私の思考は正しくないが、主に私が欲しいのは、6つのボタンが別々の場所にあることです。
これはあまりにも複雑すぎます。とにかく、既存のボタンの座標を保存して、座標軸が交差している限り、それを再登録してみてください。 – m02ph3u5
あなたの素早い返信をありがとう、おそらく私はそれぞれの配列にX、Yのlocsを交差して格納することができますし、あなたは毎回同じようにチェックしたが、私が間違っていない場合、ボタンの位置は特に1コーディネートたとえば、コードが501,501を生成する場合は500,500になりますが、これも問題になります。 – usrnvm43894
多分、私は両方の次元のためにプラス70dp毎回チェックする必要があります – usrnvm43894