Недавно начал заниматься в Android Studio, постоянно обращаюсь для решения вопросов на этот ресурс, однако сейчас я ответа - не нашел.
Задача Общая - сделать небольшую игру на одной активности с тремя "сосудами" которые необходимо привести в определенное положение. Игра считается пройденной, после того как все сосуды имеют относительно одинаковый размер около зеленой линии, не меньше и не больше заданных значений. Проиграна если один из сосудов достигает красной линии.
Задача 1 - сделать постоянную проверку условий if после нажатий на разные кнопки (для выведения сообщения, прохождения игры.)
Задача 2 - сделать вносимые размеры для view элементов в dp, а не px. Так как появляются проблемы при различных размерах экрана в эмуляторе (очень не красиво).
Проблема 1 - к сожалению, я вынужден был поставить условие в каждую кнопку, что смотрится весьма не красиво, возможно, что условие необходимо установить в какой-то из жизненных циклов активности.
Проблема 2 - Все цифровые значения которые я задаю для изменения высоты "сосудов" видимо учитываются как px, хотя как мне кажется лучше использовать dp.
Игра работает, но весьма безобразно, уверен что можно сделать красивее и правильнее. Код прилагаю: Разметка:
<TextView
android:id="@+id/textViewNameLayout"
android:textSize="@dimen/basic_textsize"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginTop="10dp"
android:text=""
android:textColor="#ffee72"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_constraintLeft_creator="1"
app:layout_constraintHorizontal_bias="0.0"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
app:layout_constraintRight_toRightOf="parent" />
\\ имя на резметке
<TextView
android:id="@+id/textViewCommentary"
android:textSize="@dimen/basic_textsize"
android:layout_width="0dp"
android:layout_height="@dimen/size_commentary"
android:text=""
android:textColor="#44ff76"
android:layout_marginLeft="10dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="10dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
tools:layout_constraintRight_creator="1"
tools:layout_constraintLeft_creator="1"
app:layout_constraintVertical_chainStyle="spread_inside"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintBottom_toTopOf="@+id/linearLayout1" />
\\ комментарий на разметке
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="0dp"
android:layout_height="@dimen/button_size"
android:orientation="horizontal"
android:layout_marginRight="10dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="10dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/linearLayout2"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp">
<TextView
android:id="@+id/textViewButton1"
android:layout_width="match_parent"
android:layout_height="@dimen/button_size"
android:layout_weight="1"
android:layout_gravity="fill_horizontal"
android:layout_marginRight="4dp"
android:background="@drawable/rectangle_yellow"
android:gravity="center"
android:text="@string/textViewBackButton"
android:textColor="#ffee72"
android:textSize="@dimen/basic_textsize"
android:onClick="onClickButton1"
/>
<TextView
android:id="@+id/textViewButton2"
android:layout_width="match_parent"
android:layout_height="@dimen/button_size"
android:layout_weight="1"
android:layout_gravity="fill_horizontal"
android:layout_marginLeft="4dp"
android:background="@drawable/rectangle_yellow"
android:gravity="center"
android:text="1"
android:textColor="#ffee72"
android:textSize="@dimen/basic_textsize"
android:onClick="onClickButton2"
/>
</LinearLayout>
\\ кнопки 1 и 2
<LinearLayout
android:layout_width="0dp"
android:layout_height="@dimen/button_size"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:id="@+id/linearLayout2">
<TextView
android:id="@+id/textViewButton3"
android:layout_marginRight="4dp"
android:layout_width="match_parent"
android:layout_height="@dimen/button_size"
android:layout_weight="1"
android:gravity="center"
android:layout_gravity="fill_horizontal"
android:background="@drawable/rectangle_yellow"
android:onClick="onClickButton3"
android:text="2"
android:textColor="#ffee72"
android:textSize="@dimen/basic_textsize"
/>
<TextView
android:id="@+id/textViewButton4"
android:layout_width="match_parent"
android:layout_height="@dimen/button_size"
android:layout_weight="1"
android:gravity="center"
android:layout_gravity="fill_horizontal"
android:background="@drawable/rectangle_yellow"
android:text="3"
android:textColor="#ffee72"
android:textSize="@dimen/basic_textsize"
android:layout_marginLeft="4dp"
android:onClick="onClickButton4"
/>
</LinearLayout> \\ кнопки 3 и 4
<TextView
android:id="@+id/View2"
android:background="@drawable/rectangle_yellow"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text=""
android:layout_marginBottom="40dp"
app:layout_constraintBottom_toTopOf="@+id/textViewCommentary"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent" />
<TextView
android:id="@+id/View3"
android:background="@drawable/rectangle_yellow"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text=""
android:layout_marginBottom="40dp"
app:layout_constraintBottom_toTopOf="@+id/textViewCommentary"
app:layout_constraintLeft_toRightOf="@+id/View2"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent" />
<TextView
android:id="@+id/View1"
android:background="@drawable/rectangle_yellow"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text=""
android:layout_marginBottom="40dp"
app:layout_constraintBottom_toTopOf="@+id/textViewCommentary"
app:layout_constraintRight_toLeftOf="@+id/View2"
android:layout_marginRight="8dp"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="0.5" />
<View
android:id="@+id/viewline1"
android:layout_width="0dp"
android:background="@drawable/line_green"
android:layout_height="3dp"
app:layout_constraintBottom_toBottomOf="@+id/View2"
android:layout_marginBottom="600px"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="0.0" />
<View
android:id="@+id/viewline2"
android:layout_width="0dp"
android:layout_height="3dp"
android:layout_marginBottom="700px"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:background="@drawable/line_red"
app:layout_constraintBottom_toBottomOf="@+id/View2"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<View
android:id="@+id/viewline3"
android:layout_width="0dp"
android:layout_height="3dp"
android:layout_marginBottom="100px"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:background="@drawable/line_red"
app:layout_constraintBottom_toBottomOf="@+id/View2"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<TextView
android:id="@+id/textViewblock1"
android:background="@drawable/rectangle_yellow"
android:layout_width="80dp"
android:gravity="center"
android:layout_height="0dp"
android:text="@string/block1"
android:layout_marginTop="0dp"
app:layout_constraintTop_toBottomOf="@+id/View1"
android:layout_marginRight="8dp"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/textViewblock2"
app:layout_constraintBottom_toBottomOf="@+id/textViewblock2"
app:layout_constraintHorizontal_bias="0.5"
android:layout_marginBottom="0dp"
app:layout_constraintVertical_bias="0.5" />
<TextView
android:id="@+id/textViewblock2"
android:background="@drawable/rectangle_yellow"
android:layout_width="80dp"
android:gravity="center"
android:layout_height="0dp"
android:text="@string/block2"
android:layout_marginBottom="0dp"
app:layout_constraintBottom_toTopOf="@+id/textViewCommentary"
android:layout_marginTop="0dp"
app:layout_constraintTop_toBottomOf="@+id/View2"
app:layout_constraintVertical_bias="0.0"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent" />
<TextView
android:id="@+id/textViewblock3"
android:background="@drawable/rectangle_yellow"
android:layout_width="80dp"
android:layout_height="0dp"
android:gravity="center"
android:text="@string/block3"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="0dp"
app:layout_constraintTop_toBottomOf="@+id/View3"
app:layout_constraintLeft_toRightOf="@+id/textViewblock2"
app:layout_constraintBottom_toBottomOf="@+id/textViewblock2"
android:layout_marginBottom="0dp"
app:layout_constraintHorizontal_bias="0.506"
app:layout_constraintVertical_bias="0.0" />
Активити: import android.content.Intent; import android.graphics.Typeface; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.Html; import android.view.View; import android.widget.TextView;
import java.security.SecureRandom;
public class test_Activity extends AppCompatActivity {
TextView PixelText;
TextView View1;
TextView View2;
TextView View3;
private int a, b, c;
private int x, y, z;
private int result_x = 0;
private int result_y = 0;
private int result_z = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_layout);
PixelText = (TextView) findViewById(R.id.textViewNameLayout);
Typeface CustomPixelText = Typeface.createFromAsset(getAssets(),"pixel.ttf");
PixelText.setTypeface(CustomPixelText);
PixelText = (TextView) findViewById(R.id.textViewCommentary);
PixelText.setTypeface(CustomPixelText);
PixelText = (TextView) findViewById(R.id.textViewButton3);
PixelText.setTypeface(CustomPixelText);
PixelText = (TextView) findViewById(R.id.textViewButton4);
PixelText.setTypeface(CustomPixelText);
PixelText = (TextView) findViewById(R.id.textViewblock1);
PixelText.setTypeface(CustomPixelText);
PixelText = (TextView) findViewById(R.id.textViewblock2);
PixelText.setTypeface(CustomPixelText);
PixelText = (TextView) findViewById(R.id.textViewblock3);
PixelText.setTypeface(CustomPixelText);
View1 = (TextView)findViewById(R.id.View1);
View2 = (TextView)findViewById(R.id.View2);
View3 = (TextView)findViewById(R.id.View3);
SecureRandom random1 = new SecureRandom();
x = random1.nextInt(39) + 1;
SecureRandom random2 = new SecureRandom();
y = random2.nextInt(39) + 1;
SecureRandom random3 = new SecureRandom();
z = random3.nextInt(39) + 1;
View1.setHeight(x * 10);
View2.setHeight(y * 10);
View3.setHeight(z * 10);
TextView txt1 = (TextView)findViewById(R.id.textViewblock1);
txt1.setText(Html.fromHtml(getString(R.string.block1)));
TextView txt2 = (TextView)findViewById(R.id.textViewblock2);
txt2.setText(Html.fromHtml(getString(R.string.block2)));
TextView txt3 = (TextView)findViewById(R.id.textViewblock3);
txt3.setText(Html.fromHtml(getString(R.string.block3)));
}
public void onClickButton1 (View view) {
Intent intent = new Intent(test_Activity.this, Control_Point_Activity_act1.class);
startActivity(intent);
}
public void onClickButton2 (View view) {
a++;
if (a == 1) {
result_x = x++ * 10;
result_y = y-- * 10;
result_z = z++ * 10;
View1.setHeight(result_x);
View2.setHeight(result_y);
View3.setHeight(result_z);
a = 0;
if (View1.getHeight() <= 220 & View2.getHeight() <= 220 & View3.getHeight() <= 220 & View1.getHeight() >= 180 & View2.getHeight() >= 180 & View3.getHeight() >= 180) {
TextView tvb3 = (TextView) findViewById(R.id.textViewCommentary);
tvb3.setText("все получилось");
} else {
TextView tvb3 = (TextView) findViewById(R.id.textViewCommentary);
tvb3.setText("");
}
if (View1.getHeight() <= 220 & View1.getHeight() >= 180) {
TextView txt1 = (TextView) findViewById(R.id.textViewblock1);
txt1.setText(Html.fromHtml(getString(R.string.blockOK)));
} else {
TextView txt1 = (TextView) findViewById(R.id.textViewblock1);
txt1.setText(Html.fromHtml(getString(R.string.block1)));
}
}
}
public void onClickButton3 (View view) {
b ++;
if (b == 1){
result_x = x -- * 10;
//result_y = y -- * 10;
result_z = z ++ * 10;
View1.setHeight(result_x);
//View2.setHeight(result_y);
View3.setHeight(result_z);
b = 0;
if (View1.getHeight() <= 220 & View2.getHeight() <= 220 & View3.getHeight() <= 220 & View1.getHeight() >= 180 & View2.getHeight() >= 180 & View3.getHeight() >= 180) {
TextView tvb3 = (TextView) findViewById(R.id.textViewCommentary);
tvb3.setText("все получилось");
} else {
TextView tvb3 = (TextView) findViewById(R.id.textViewCommentary);
tvb3.setText("");
}
if (View2.getHeight() <= 220 & View2.getHeight() >= 180) {
TextView txt1 = (TextView)findViewById(R.id.textViewblock2);
txt1.setText(Html.fromHtml(getString(R.string.blockOK)));
} else {
TextView txt1 = (TextView)findViewById(R.id.textViewblock2);
txt1.setText(Html.fromHtml(getString(R.string.block2)));
}
}
}
public void onClickButton4 (View view) {
c ++;
if (c == 1){
result_x = x ++ * 10;
result_y = y ++ * 10;
result_z = z -- * 10;
View1.setHeight(result_x);
View2.setHeight(result_y);
View3.setHeight(result_z);
c = 0;
if (View1.getHeight() <= 220 & View2.getHeight() <= 220 & View3.getHeight() <= 220 & View1.getHeight() >= 180 & View2.getHeight() >= 180 & View3.getHeight() >= 180) {
TextView tvb3 = (TextView) findViewById(R.id.textViewCommentary);
tvb3.setText("все получилось");
} else {
TextView tvb3 = (TextView) findViewById(R.id.textViewCommentary);
tvb3.setText("");
}
if (View3.getHeight() <= 220 & View3.getHeight() >= 180) {
TextView txt1 = (TextView) findViewById(R.id.textViewblock3);
txt1.setText(Html.fromHtml(getString(R.string.blockOK)));
} else {
TextView txt1 = (TextView)findViewById(R.id.textViewblock3);
txt1.setText(Html.fromHtml(getString(R.string.block3)));
}
}
}