Android实现真心话大冒险App

Android实现真心话大冒险App,第1张

1.运行效果


点击启动转盘,音乐播放,转盘转动

点击抽奖

点击设置,可以设置参数


2.项目情况

前端界面activity_main.xml代码


<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">

    <Button
        android:id="@+id/btn_startTurntable"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="92dp"
        android:layout_marginTop="34dp"
        android:onClick="startTurntable"
        android:text="启动转盘"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn_lottery"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="34dp"
        android:layout_marginEnd="82dp"
        android:onClick="lottery"
        android:text="抽奖"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/imageView_turntable"
        android:layout_width="0dp"
        android:layout_height="345dp"
        android:layout_marginStart="50dp"
        android:layout_marginTop="264dp"
        android:layout_marginEnd="50dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/pan0" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="112dp"
        android:text="恭喜抽中:"
        android:textSize="30sp"
        app:layout_constraintBaseline_toBaselineOf="@+id/textView_present"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/textView_present"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginTop="56dp"
        android:textColor="#DA1414"
        android:textSize="30sp"
        app:layout_constraintStart_toEndOf="@+id/textView"
        app:layout_constraintTop_toBottomOf="@+id/btn_lottery" />
androidx.constraintlayout.widget.ConstraintLayout>

MainActivity代码

package com.zhumin.project9;



import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Random;

public class MainActivity extends AppCompatActivity {
    private ImageView imageView_turntable ;
    private Button btn_startTurntable,btn_lottery;
    private TextView textView_present;
    private Runnable_startTurntableThread runnable_startTurntableThread;// 转盘Runnable接口
    private  Runnable_PlayMusic runnable_playMusic;
    private  SharedPreferences sharedPreferences =null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    // 初始化控件
    private void initView() {
        // 初始化控件
        imageView_turntable = findViewById(R.id.imageView_turntable);
        btn_lottery = findViewById(R.id.btn_lottery);
        btn_startTurntable = findViewById(R.id.btn_startTurntable);
        textView_present = findViewById(R.id.textView_present);
        // 初始化按钮状态
        btn_startTurntable.setEnabled(true);
        btn_lottery.setEnabled(false);
        textView_present.setEnabled(false);
        // 初始化数据sharedPreferences
        sharedPreferences = MainActivity.this.getSharedPreferences("data",MainActivity.this.MODE_PRIVATE);
    }

    // 启动转盘
    public void startTurntable(View view) throws InterruptedException {
        btn_startTurntable.setEnabled(false);
        btn_lottery.setEnabled(true);
        // 创建一个线程用于播放音乐
        runnable_playMusic = new Runnable_PlayMusic();
        runnable_playMusic.setContext(this);
        runnable_playMusic.setTage(true);
        Thread playMusic =new Thread(runnable_playMusic);

        // 创建一个线程用于抽奖
        runnable_startTurntableThread = new Runnable_startTurntableThread();
        runnable_startTurntableThread.setTage(true);
        runnable_startTurntableThread.setView(view);
        runnable_startTurntableThread.setImageView(imageView_turntable);
        if (sharedPreferences != null) {
            runnable_startTurntableThread.setSpeed(sharedPreferences.getInt("speed", 0));
        }
        Thread startTurntableThread = new Thread(runnable_startTurntableThread);




        // 开启线程
        playMusic.start();
        startTurntableThread.start();



    }

    // 抽奖
    public void lottery(View view) {
        btn_startTurntable.setEnabled(true);
        btn_lottery.setEnabled(false);
        runnable_startTurntableThread.setTage(false);

        // 开启线程,去关闭音乐
        runnable_playMusic.setTage(false);
        Thread playMusic =new Thread(runnable_playMusic);
        playMusic.start();

        // 反馈获奖情况
        String [] present={"真心话","大冒险","无惊无险"};
        int chance1=3,chance2=3,chance3=4;//0~2 3~5 6~9
        // 判断数据SharedPreferences是否有数据
        if (sharedPreferences != null) {
            chance1 = sharedPreferences.getInt("chance1",0);
            chance2 = sharedPreferences.getInt("chance2", 0);
            chance3 = sharedPreferences.getInt("chance3", 0);
        }
        int i = new Random().nextInt(chance1+chance2+chance3);
        if (i < chance1) {
            textView_present.setText(present[0]);
        } else if (i < (chance1+chance2)){
            textView_present.setText(present[1]);
        } else if (i <= (chance1 + chance2 + chance3)) {
            textView_present.setText(present[2]);
        }


    }

    // 菜单在Activity中进行显示
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.app_menu, menu);
        return true;
    }

    // 处理点击事件
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.app_setting:
                appSetting();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    // d出自定义对话框
    private void appSetting() {
        MyDialog myDialog = new MyDialog(MainActivity.this,R.style.MyDialog);
        // 确定点击事件
        myDialog.setOnYesOnclickListener(() -> {
           // view.post(()->{
                EditText chance1 = myDialog.getChance1();
                EditText chance2 = myDialog.getChance2();
                EditText chance3 = myDialog.getChance3();
                EditText speed = myDialog.getSpeed();
                if (chance1 == null || chance2 == null || chance3 == null || speed == null) {
                    Toast.makeText(MainActivity.this, "参数设置不能为空!", Toast.LENGTH_SHORT).show();
                    return;
                }

            if (chance1.getText().toString().isEmpty() || chance2.getText().toString().isEmpty() || chance3.getText().toString().isEmpty() || speed.getText().toString().isEmpty()) {
                Toast.makeText(MainActivity.this, "参数设置不能为空!", Toast.LENGTH_SHORT).show();
                return;
            }
                Integer c1 = Integer.valueOf(chance1.getText().toString());
                Integer c2 = Integer.valueOf(chance2.getText().toString());
                Integer c3 = Integer.valueOf(chance3.getText().toString());
                Integer sp = Integer.valueOf(speed.getText().toString());
                // 数据验证
                if (!(c1 >0 && c1<=10 || c2 >0 && c2<=10 ||c3 >0 && c3<=10))
                {
                    Toast.makeText(MainActivity.this, "真心话,大冒险,无惊无险的概率必须在0-10闭区间", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (!(sp <= 1000 && sp >= 1)) {
                    Toast.makeText(MainActivity.this, "转盘速率必须在1-1000的闭区间内", Toast.LENGTH_SHORT).show();
                    return;
                }
                // 将数据存储到SharedPreferences中
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putInt("chance1", c1);
                editor.putInt("chance2", c2);
                editor.putInt("chance3", c3);
                editor.putInt("speed", sp);
                editor.commit();
                myDialog.dismiss();
           // });
        });

        // 取消点击事件
        myDialog.setOnNoOnclickListener(()->{
            myDialog.dismiss();
        });
        myDialog.show();


    }


}

class Runnable_startTurntableThread implements Runnable{

    private int count = 0;
    public boolean tage = false;
    private View view;
    private ImageView imageView;
    private int speed =100;



    @Override
    public void run() {
        // 键盘开始旋转
        while (tage) {
            if (count > 22){
                count = 0;
            }
            count += 2;
            // 开始旋转
           view.post(()->{
                switch (count){
                    case  0:imageView.setImageResource(R.drawable.pan0) ;break;
                    case 2:imageView.setImageResource(R.drawable.pan2);break;
                    case 4:imageView.setImageResource(R.drawable.pan4);break;
                    case 6:imageView.setImageResource(R.drawable.pan6);break;
                    case 8:imageView.setImageResource(R.drawable.pan8);break;
                    case 10:imageView.setImageResource(R.drawable.pan10);break;
                    case  12:imageView.setImageResource(R.drawable.pan12) ;break;
                    case 14:imageView.setImageResource(R.drawable.pan14);break;
                    case 16:imageView.setImageResource(R.drawable.pan16);break;
                    case 18:imageView.setImageResource(R.drawable.pan18);break;
                    case 20:imageView.setImageResource(R.drawable.pan20);break;
                    case 22:imageView.setImageResource(R.drawable.pan22);break;
                    case 24:imageView.setImageResource(R.drawable.pan24);break;
                }
           });
            try {
                Thread.sleep(speed);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void setTage(boolean tage) {
        this.tage = tage;
    }

    public void setView(View view) {
        this.view = view;
    }

    public void setImageView(ImageView imageView) {
        this.imageView = imageView;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }
}

class Runnable_PlayMusic implements  Runnable{
    private boolean tage =false,state;
    MediaPlayer mediaPlayer;
    Context context;

    public void setTage(boolean tage) {
        this.tage = tage;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    @Override
    public void run() {
            if (tage) {
                mediaPlayer = MediaPlayer.create(context, R.raw.mymusic);
                mediaPlayer.start(); // no need to call prepare(); create() does that for you
            } else {
                mediaPlayer.stop();
                // 进行释放
                mediaPlayer.release();
                mediaPlayer = null;
            }
    }
}
3.项目源码

微信公众号搜索程序员孤夜(或扫描下方二维码),后台回复 安卓源码 获取项目总文件(projects),在总文件下的Project9就是本篇博文对应的源码!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/924017.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存