Игра “Рулетка” : Android Studio уроки

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {
private TextView tvResult;
private ImageView rul;
private Random random;
private int old_deegre = 0;
private int deegre = 0;
private static final float FACTOR = 4.86f;
private String[] numbers = {"32 RED","15 BLACK","19 RED","4 BLACK",
"21 RED","2 BLACK","25 RED","17 BLACK", "34 RED",
"6 BLACK","27 RED","13 BLACK","36 RED","11 BLACK","30 RED",
"8 BLACK","23 RED","10 BLACK","5 RED","24 BLACK","16 RED","33 BLACK",
"1 RED","20 BLACK","14 RED","31 BLACK","9 RED","22 BLACK","18 RED",
"29 BLACK","7 RED","28 BLACK","12 RED","35 BLACK","3 RED","26 BLACK","0"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}

public void onClickStart(View view)
{
old_deegre = deegre % 360;
deegre = random.nextInt(3600) + 720;
RotateAnimation rotate = new RotateAnimation(old_deegre,deegre,RotateAnimation.RELATIVE_TO_SELF,
0.5f,RotateAnimation.RELATIVE_TO_SELF,0.5f);
rotate.setDuration(3600);
rotate.setFillAfter(true);
rotate.setInterpolator(new DecelerateInterpolator());
rotate.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
tvResult.setText("");
}

@Override
public void onAnimationEnd(Animation animation) {
tvResult.setText(getResult(360 - (deegre % 360)));
}

@Override
public void onAnimationRepeat(Animation animation) {

}
});
rul.startAnimation(rotate);

}
private void init()
{
tvResult = findViewById(R.id.tvResult);
rul = findViewById(R.id.rul);
random = new Random();

}
private String getResult(int deegre)
{
String text = "";

int factor_x = 1;
int factor_y = 3;
for(int i = 0;i < 37; i++){
if(deegre >= (FACTOR * factor_x) && deegre < (FACTOR * factor_y))
{
text = numbers[i];
}
factor_x += 2;
factor_y += 2;
}
if(deegre >= (FACTOR * 73) && deegre < 360 || deegre >= 0 && deegre < (FACTOR * 1)) text = numbers[numbers.length - 1];

return text;
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/tvResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textColor="@android:color/background_dark"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.116" />

<ImageView
android:id="@+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="@+id/tvResult"
app:layout_constraintStart_toStartOf="@+id/tvResult"
app:layout_constraintTop_toBottomOf="@+id/tvResult"
app:srcCompat="@drawable/flecha" />

<ImageView
android:id="@+id/rul"
android:layout_width="380dp"
android:layout_height="380dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView"
app:srcCompat="@drawable/rouletteive" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="32dp"
android:onClick="onClickStart"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

styles.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowFullscreen">true</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>

</style>

</resources>

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.neco_desarrollo.winerrul">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *