当前位置:   article > 正文

android开发案例,开箱子游戏_android开发小项目实例

android开发小项目实例

1. 项目结构

1.1 代码结构

在这里插入图片描述

1.2 布局与图片素材

在这里插入图片描述

2. 项目资源配置

2.1 清单文件

AndroidManifest.xm

2.1.1 权限设置

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  • 1
  • 2
  • 3

2.1.2 Application 设置

<application
    android:allowBackup="true"
    android:dataExtractionRules="@xml/data_extraction_rules"
    android:fullBackupContent="@xml/backup_rules"
    android:icon="@drawable/ic_launcu_chest"
    android:label="@string/app_name"
    android:roundIcon="@drawable/ic_launcu_chest"
    android:supportsRtl="true"
    android:theme="@style/Theme.Chests"
    tools:targetApi="31">
</application>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

2.1.3 Activity 设置

<activity
    android:name=".activities.PackageDetailActivity"
    android:exported="false">
    <meta-data
        android:name="android.app.lib_name"
        android:value="" />
</activity>
<activity
    android:name=".activities.MainActivity"
    android:exported="false">
    <meta-data
        android:name="android.app.lib_name"
        android:value="" />
</activity>
<activity
    android:name=".activities.LogActivity"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

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

    <meta-data
        android:name="android.app.lib_name"
        android:value="" />
</activity>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

2.1.4 主题设置

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.Chests" parent="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>
</resources>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

2.2 常量配置

2.2.1 基础类型常量

<resources>
    <string name="app_name">开箱子</string>
    <string name="R">R</string>
    <string name="SR">SR</string>
    <string name="SSR">SSR</string>
    <string name="SSS">SSS</string>
    <string name="title_activity_login">LoginActivity</string>
    <string name="prompt_email">Email</string>
    <string name="prompt_password">Password</string>
    <string name="action_sign_in">Sign in or register</string>
    <string name="action_sign_in_short">Sign in</string>
    <string name="welcome">"Welcome !"</string>
    <string name="invalid_username">Not a valid username</string>
    <string name="invalid_password">Password must be >5 characters</string>
    <string name="login_failed">"Login failed"</string>
    <integer name="default_diamond">20000</integer>
    <integer name="default_gold_coin">2000</integer>
    <string name="default_uname">123456</string>
    <string name="default_pwd">123456</string>
    <integer name="open_diamond_min">1</integer>
    <integer name="open_diamond_max">50</integer>
</resources>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

2.2.2 颜色常量

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="purple_200">#FFBB86FC</color>
    <color name="purple_500">#FF6200EE</color>
    <color name="purple_700">#FF3700B3</color>
    <color name="teal_200">#FF03DAC5</color>
    <color name="teal_700">#FF018786</color>
    <color name="black">#FF000000</color>
    <color name="white">#FFFFFFFF</color>
    <color name="red">#FFFF0000</color>
    <color name="btn_click">#FFCE47</color>
    <color name="btn_default">#EBF1FF</color>
    <color name="open_record_back">#FD393B</color>
    <color name="orange">#ffffdd</color>
</resources>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

2.2.3 图片常量

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="img1_2">https://pic.imgdb.cn/item/651ae011c458853aef1c5a52.jpg</string>
    <string name="img2_2">https://pic.imgdb.cn/item/651ae011c458853aef1c5a5d.jpg</string>
    <string name="img3_3">https://pic.imgdb.cn/item/651ae011c458853aef1c5a6d.jpg</string>
    <string name="img4_4">https://pic.imgdb.cn/item/651ae012c458853aef1c5a81.jpg</string>
    <string name="img5_3">https://pic.imgdb.cn/item/651ae012c458853aef1c5aa5.jpg</string>
    <string name="img6_4">https://pic.imgdb.cn/item/651ae041c458853aef1c5fee.jpg</string>
    <string name="img7_4">https://pic.imgdb.cn/item/651ae042c458853aef1c5ffa.jpg</string>
    <string name="img8_2">https://pic.imgdb.cn/item/651ae042c458853aef1c6012.jpg</string>
    <string name="img9_3">https://pic.imgdb.cn/item/651ae042c458853aef1c6020.jpg</string>
    <string name="img10_2">https://pic.imgdb.cn/item/651ae042c458853aef1c6039.jpg</string>
    <string name="img11_3">https://pic.imgdb.cn/item/651ae0e7c458853aef1c7374.jpg</string>
    <string name="img12_3">https://pic.imgdb.cn/item/651ae0e7c458853aef1c7385.jpg</string>
    <string name="img13_1">https://pic.imgdb.cn/item/651ae0e8c458853aef1c7394.jpg</string>
    <string name="img14_1">https://pic.imgdb.cn/item/651ae0e8c458853aef1c73b3.jpg</string>
    <string name="img15_4">https://pic.imgdb.cn/item/651ae0e8c458853aef1c73d4.jpg</string>
    <string name="img16_2">https://pic.imgdb.cn/item/651ae115c458853aef1c7998.jpg</string>
    <string name="img17_1">https://pic.imgdb.cn/item/651ae115c458853aef1c79a6.jpg</string>
    <string name="img18_2">https://pic.imgdb.cn/item/651ae115c458853aef1c79cf.jpg</string>
    <string name="img19_3">https://pic.imgdb.cn/item/651ae115c458853aef1c79df.jpg</string>
    <string name="img20_1">https://pic.imgdb.cn/item/651ae116c458853aef1c79fe.jpg</string>
    <string name="img21_4">https://pic.imgdb.cn/item/651ae153c458853aef1c816f.jpg</string>
    <string name="img22_3">https://pic.imgdb.cn/item/651ae153c458853aef1c817e.jpg</string>
    <string name="img23_4">https://pic.imgdb.cn/item/651ae153c458853aef1c818d.jpg</string>
    <string name="img24_2">https://pic.imgdb.cn/item/651ae153c458853aef1c81a1.jpg</string>
    <string name="img25_1">https://pic.imgdb.cn/item/651ae154c458853aef1c81c6.jpg</string>
    <string name="img26_3">https://pic.imgdb.cn/item/651ae184c458853aef1c8787.jpg</string>
    <string name="img27_1">https://pic.imgdb.cn/item/651ae185c458853aef1c8797.jpg</string>
    <string name="img28_4">https://pic.imgdb.cn/item/651ae185c458853aef1c87ab.jpg</string>
    <string name="img29_4">https://pic.imgdb.cn/item/651ae185c458853aef1c87c2.jpg</string>
    <string name="img30_4">https://pic.imgdb.cn/item/651ae185c458853aef1c87e0.jpg</string>
    <string name="img31_4">https://pic.imgdb.cn/item/651ae1bcc458853aef1c8cbf.jpg</string>
    <string name="img32_4">https://pic.imgdb.cn/item/651ae1bcc458853aef1c8ce1.jpg</string>
    <string name="img33_1">https://pic.imgdb.cn/item/651ae1bcc458853aef1c8d08.jpg</string>
    <string name="img34_1">https://pic.imgdb.cn/item/651ae1bdc458853aef1c8d3c.jpg</string>
    <string name="img35_1">https://pic.imgdb.cn/item/651ae1bdc458853aef1c8d5a.jpg</string>
    <string name="img36_4">https://pic.imgdb.cn/item/651ae1e9c458853aef1c945d.jpg</string>
    <string name="img37_1">https://pic.imgdb.cn/item/651ae1eac458853aef1c946d.jpg</string>
    <string name="img38_3">https://pic.imgdb.cn/item/651ae1eac458853aef1c947f.jpg</string>
    <string name="img39_1">https://pic.imgdb.cn/item/651ae1eac458853aef1c948d.jpg</string>
    <string name="img40_2">https://pic.imgdb.cn/item/651ae1eac458853aef1c94a2.jpg</string>
    <string name="img41_1">https://pic.imgdb.cn/item/651ae222c458853aef1ca07d.jpg</string>
    <string name="img42_4">https://pic.imgdb.cn/item/651ae222c458853aef1ca08a.jpg</string>
    <string name="img43_4">https://pic.imgdb.cn/item/651ae223c458853aef1ca099.jpg</string>
    <string name="img44_2">https://pic.imgdb.cn/item/651ae223c458853aef1ca0a9.jpg</string>
    <string name="img45_2">https://pic.imgdb.cn/item/651ae223c458853aef1ca0c1.jpg</string>
    <string name="img46_1">https://pic.imgdb.cn/item/651ae27fc458853aef1caa8c.jpg</string>
    <string name="img47_3">https://pic.imgdb.cn/item/651ae280c458853aef1caa9a.jpg</string>
    <string name="img48_3">https://pic.imgdb.cn/item/651ae280c458853aef1caaae.jpg</string>
    <string name="img49_4">https://pic.imgdb.cn/item/651ae280c458853aef1caac3.jpg</string>
    <string name="img50_4">https://pic.imgdb.cn/item/651ae281c458853aef1caaea.jpg</string>
    <string name="img51_1">https://pic.imgdb.cn/item/651ae2d2c458853aef1cb473.jpg</string>
    <string name="img52_3">https://pic.imgdb.cn/item/651ae2d3c458853aef1cb482.jpg</string>
    <string name="img53_3">https://pic.imgdb.cn/item/651ae2d3c458853aef1cb493.jpg</string>
    <string name="img54_4">https://pic.imgdb.cn/item/651ae2d3c458853aef1cb4a2.jpg</string>
    <string name="img55_2">https://pic.imgdb.cn/item/651ae2d3c458853aef1cb4b8.jpg</string>
    <string name="img56_2">https://pic.imgdb.cn/item/651ae305c458853aef1cbac8.jpg</string>
    <string name="img57_2">https://pic.imgdb.cn/item/651ae305c458853aef1cbad6.jpg</string>
    <string name="img58_1">https://pic.imgdb.cn/item/651ae305c458853aef1cbae5.jpg</string>
    <string name="img59_3">https://pic.imgdb.cn/item/651ae305c458853aef1cbafa.jpg</string>
    <string name="img60_3">https://pic.imgdb.cn/item/651ae305c458853aef1cbb10.jpg</string>
    <string name="img61_2">https://pic.imgdb.cn/item/651ae334c458853aef1cc0b1.jpg</string>
    <string name="img62_1">https://pic.imgdb.cn/item/651ae334c458853aef1cc0d1.jpg</string>
    <string name="img63_1">https://pic.imgdb.cn/item/651ae334c458853aef1cc0d1.jpg</string>
</resources>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66

2.3 图形资源

2.3.1 ic_border_clicked.xml

在这里插入图片描述

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke
        android:width="1dp"
        android:color="#555555" />
    <solid android:color="@color/btn_click" />
    <corners
        android:radius="5dp" />
</shape>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

2.3.2 ic_border_default

在这里插入图片描述

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke
        android:width="1dp"
        android:color="#555555" />
    <solid android:color="@color/btn_default" />
    <corners
        android:radius="5dp" />
</shape>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

2.3.3 ic_open_chest_five

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/ic_open_five_active" />
    <item android:drawable="@drawable/ic_open_five" />
</selector>
  • 1
  • 2
  • 3
  • 4
  • 5

2.3.4 ic_open_chest_once

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/ic_open_once_activate" />
    <item android:drawable="@drawable/ic_open_once" />
</selector>
  • 1
  • 2
  • 3
  • 4
  • 5

2.3.5 ic_title_back.xml

在这里插入图片描述

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke
        android:width="1dp"
        android:color="#aaaaff" />
    <solid android:color="#aaaaff" />
    <corners
        android:radius="1dp" />
</shape>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

2.3.6 tv_selt.xml

在这里插入图片描述

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/ic_border_clicked" />
    <item android:drawable="@drawable/ic_border_default" />
</selector>
  • 1
  • 2
  • 3
  • 4

3. 后端代码

3.1 页面代码(activites)

3.1.1 LogActivity.java

public class LogActivity extends AppCompatActivity {

    private ChestDatabaseHelper helper;
    private TextView tv_listen_download;
    private EditText ei_uname;
    private EditText ei_upwd;
    private Button btn_login;
    private CheckBox cbx_rember_pwd;
//    private static HashMap<Integer, ImageURL> allImageUrls;


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

        tv_listen_download = findViewById(R.id.tv_listen_download);
        addTextChangeListener();

        helper = ChestDatabaseHelper.getInstance(this);
        helper.openWriteLink();
        helper.openReadLink();

        ei_uname = findViewById(R.id.ei_uname);
        ei_upwd = findViewById(R.id.ei_upwd);
        cbx_rember_pwd = findViewById(R.id.cbx_rember_pwd);

        userLogin();
//        helper.clearImageUrlDatas();
        helper.clearUserInfos();
        cbx_rember_pwd.setChecked(SharedUtil.getInstance(this).readBoolean("rempwd", false));

        DefaultResource.init(this, tv_listen_download);
        ei_uname.setText(SharedUtil.getInstance(this).readString("uname", DefaultResource.userInfo.uname));
        if (cbx_rember_pwd.isChecked()) {
            ei_upwd.setText(SharedUtil.getInstance(this).readString("upwd", ""));
        }
        helper.addUserInfo();
        btn_login.setEnabled(true);
        btn_login.setText("登录");

//        findViewById(R.id.btn_login).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//            }
//        });
    }

    private void userLogin() {
        btn_login = findViewById(R.id.btn_login);

        btn_login.setEnabled(false);
        btn_login.setText("初始化中..");

        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserInfo userInfo = new UserInfo();
                userInfo.upwd = ei_upwd.getText().toString();
                userInfo.uname = ei_uname.getText().toString();
                SharedUtil.getInstance(LogActivity.this).writeString("uname", userInfo.uname);

                if (cbx_rember_pwd.isChecked()) {
                    SharedUtil.getInstance(LogActivity.this).writeString("upwd", userInfo.upwd);
                    SharedUtil.getInstance(LogActivity.this).writeBoolean("rempwd", true);
                } else {
                    SharedUtil.getInstance(LogActivity.this).writeString("upwd", "");
                    SharedUtil.getInstance(LogActivity.this).writeBoolean("rempwd", false);
                }

                if (helper.userLogin(userInfo)) {
                    Intent intent = new Intent(LogActivity.this, MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(intent);
                } else {
                    ToastUtil.show(LogActivity.this, "用户名或密码错误!");
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        helper.closeLink();
    }

    private void addTextChangeListener(){
        tv_listen_download.addTextChangedListener(new TextWatcher() {


            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                helper.insertDefaultValues();
//                List<ImageURL> imageURLS = helper.getAllImageUrls();
//                for (int i = 0; i < imageURLS.size(); i++) {
//                    allImageUrls.put(imageURLS.get(i).id, imageURLS.get(i));
//                }

                ToastUtil.show(LogActivity.this, "初始化成功!");
//                btn_login.setEnabled(true);
//                btn_login.setText("登录");
            }
        });
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118

3.1.2 MainActivity.java

public class MainActivity extends AppCompatActivity {

    private Button btn_open_chest;
    private Button btn_package;
    private Button btn_record;
    private ImageView iv_open_once;
    private ImageView iv_open_five;
    private LinearLayout ll_open_chest;
    private LinearLayout ll_package;
    private LinearLayout ll_record;
    private TextView tv_gold_coin;
    private TextView tv_diamond;
    private TextView tv_open_records;

    private int open_times = 0;
    private ChestDatabaseHelper helper;
    private ScrollView sv_open_records;
    private List<PackageInfo> allPackageInfos;
    private static HashMap<Integer, ImageURL> allImageUrls;
    private ScrollView ss_package;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        helper = ChestDatabaseHelper.getInstance(this);

        allImageUrls = new HashMap<>();

        ll_init();
        btn_init();
        iv_btn_init();
        tv_init();
        show_userinfo();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void refreshPackageInfos() {
        ll_package.removeAllViews();
        allPackageInfos = helper.getAllPackageInfos();

        if (allPackageInfos == null) {
            return;
        }

        if (allPackageInfos.size() == 0) {
            return;
        }

        for (PackageInfo packageInfo : allPackageInfos) {
            ImageURL imageURL = helper.getImageUrlById(String.valueOf(packageInfo.imageUrlId));
            allImageUrls.put(packageInfo.imageUrlId, imageURL);

            View view = LayoutInflater.from(this).inflate(R.layout.activity_package_item, null);
            ImageView iv_thumb = view.findViewById(R.id.iv_thumb);
            TextView tv_name = view.findViewById(R.id.tv_name);
            TextView tv_desc = view.findViewById(R.id.tv_desc);
            TextView tv_count = view.findViewById(R.id.tv_count);
            TextView tv_price = view.findViewById(R.id.tv_price);
            TextView tv_sum = view.findViewById(R.id.tv_sum);

            iv_thumb.setImageURI(Uri.parse(imageURL.path));
            tv_name.setText("待描述");
            tv_desc.setText(imageURL.simple_name);
            tv_count.setText(String.valueOf(packageInfo.count));
            tv_price.setText(String.valueOf((int)imageURL.diamond_cost));
            tv_sum.setText(String.valueOf((int)(packageInfo.count * imageURL.diamond_cost)));

            view.setOnLongClickListener(v -> {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setMessage("是否从从背包中卖出" + imageURL.simple_name + "? ");
                builder.setPositiveButton("是", (dialog, which) -> {
                    DefaultResource.userInfo.diamond += packageInfo.count * imageURL.diamond_cost;
                    show_userinfo();
                    helper.updateUserInfo(DefaultResource.userInfo);

                    ll_package.removeView(v);
                    helper.deletePackageInfo(packageInfo);
                });

                builder.setNegativeButton("否", null);
                builder.create().show();
                return true;
            });

            view.setOnClickListener(v -> {
                Intent intent = new Intent(MainActivity.this, PackageDetailActivity.class);
                intent.putExtra("imageUriId", imageURL.id);
                startActivity(intent);
            });

            ll_package.addView(view);
        }
    }

    private void tv_init() {
        tv_gold_coin = findViewById(R.id.tv_gold_coin);
        tv_diamond = findViewById(R.id.tv_diamond);
        tv_open_records = findViewById(R.id.tv_open_records);
        sv_open_records = findViewById(R.id.sv_open_records);
    }

    private void show_userinfo() {
        tv_gold_coin.setText(MathUtils.formatNumber((int) DefaultResource.userInfo.gold_coin));
        tv_diamond.setText(MathUtils.formatNumber((int) DefaultResource.userInfo.diamond));
    }

    private void ll_init() {
        ll_open_chest = findViewById(R.id.ll_open_chest);
        ll_package = findViewById(R.id.ll_package);
        ll_record = findViewById(R.id.ll_record);
        ss_package = findViewById(R.id.ss_package);

        ll_open_chest.setVisibility(View.VISIBLE);
        ll_package.setVisibility(View.GONE);
        ss_package.setVisibility(View.GONE);
        ll_record.setVisibility(View.GONE);
    }

    private void iv_btn_init() {
        iv_open_once = findViewById(R.id.iv_open_once);
        iv_open_five = findViewById(R.id.iv_open_five);

        Handler handler = new Handler();

        iv_open_once.setOnClickListener(v -> {
            iv_open_once.setImageResource(R.drawable.ic_open_once_activate);

            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    iv_open_once.setImageResource(R.drawable.ic_open_once);
                }
            }, 100);

            open_once_process();
        });

        iv_open_five.setOnClickListener(v -> {
            iv_open_five.setImageResource(R.drawable.ic_open_five_active);
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    iv_open_five.setImageResource(R.drawable.ic_open_five);
                    for (int i = 0; i < 5; i++) {
                        if (!open_once_process()) {
                            break;
                        }
                    }
                }
            }, 100);
        });
    }

    private void btn_init() {
        btn_open_chest = findViewById(R.id.btn_open_chest);
        btn_package = findViewById(R.id.btn_package);
        btn_record = findViewById(R.id.btn_record);

        btn_open_chest.setBackground(getDrawable(R.drawable.ic_border_clicked));
        btn_package.setBackground(getDrawable(R.drawable.ic_border_default));
        btn_record.setBackground(getDrawable(R.drawable.ic_border_default));

        btn_open_chest.setOnClickListener(v -> {
            btn_open_chest.setBackground(getDrawable(R.drawable.ic_border_clicked));
            btn_package.setBackground(getDrawable(R.drawable.ic_border_default));
            btn_record.setBackground(getDrawable(R.drawable.ic_border_default));

            ll_open_chest.setVisibility(View.VISIBLE);
            ll_package.setVisibility(View.GONE);
            ss_package.setVisibility(View.GONE);
            ll_record.setVisibility(View.GONE);

            open_times = 0;
            tv_open_records.setText("");
            show_userinfo();
        });

        btn_package.setOnClickListener(v -> {
            btn_open_chest.setBackground(getDrawable(R.drawable.ic_border_default));
            btn_package.setBackground(getDrawable(R.drawable.ic_border_clicked));
            btn_record.setBackground(getDrawable(R.drawable.ic_border_default));

            ll_open_chest.setVisibility(View.GONE);
            ll_package.setVisibility(View.VISIBLE);
            ss_package.setVisibility(View.VISIBLE);
            ll_record.setVisibility(View.GONE);

            refreshPackageInfos();
        });
        btn_record.setOnClickListener(v -> {
            btn_open_chest.setBackground(getDrawable(R.drawable.ic_border_default));
            btn_package.setBackground(getDrawable(R.drawable.ic_border_default));
            btn_record.setBackground(getDrawable(R.drawable.ic_border_clicked));

            ll_open_chest.setVisibility(View.GONE);
            ll_package.setVisibility(View.GONE);
            ss_package.setVisibility(View.GONE);
            ll_record.setVisibility(View.VISIBLE);
        });
    }

    private boolean open_once_process(){
        if (DefaultResource.userInfo.diamond < 50) {
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
//            builder.setMessage("钻石已经花光了,要再充点吗?");
            builder.setMessage("钻石已经花光了,是否充个648?");
            builder.setPositiveButton("是", (dialog, which) -> {
//                ToastUtil.show(MainActivity.this, "当前版本不支持充值!");
                DefaultResource.userInfo.diamond += 6480;
                helper.updateUserInfo(DefaultResource.userInfo);
                show_userinfo();
            });

            builder.setNegativeButton("否", null);
            builder.create().show();

            return false;
        }

        int roll_count = RandomUtils.getRandomInt(Rules.hit_weights[0][0] - 10000, Rules.hit_weights[3][1] + 10000);
        int level = -1;
        for (int i = 0; i < Rules.hit_weights.length; i++) {
            if (roll_count <= Rules.hit_weights[i][1] && roll_count >= Rules.hit_weights[i][0]) {
                level = i;
                break;
            }
        }

        int id = 0;
        int url_id = RandomUtils.getRandomInt(Rules.chestIds[id][0], Rules.chestIds[id][1]);

        String info = "";
        String before_info = "roll_count:" + roll_count + ",";
        before_info = "恭喜你";

        int diamond_num = RandomUtils.getRandomInt(
                getResources().getInteger(R.integer.open_diamond_min),
                getResources().getInteger(R.integer.open_diamond_max));

        if (level == -1) {
            info = before_info + "抽到钻石:" + diamond_num;
//            ToastUtil.show(this, info);
        } else {
            ImageURL imageURL = helper.getImageUrlByIdAndCount(level, url_id);
            id = level - 1;

            if (imageURL == null) {
                info = before_info + "抽到钻石:" + diamond_num;
                level = -1;
                id = 0;
            } else {
                if (imageURL.path == null) {
                    info = before_info + "抽到钻石:" + diamond_num;
                    level = -1;
                    id = 0;
                } else {
                    if (imageURL.path.isEmpty()) {
                        info = before_info + "抽到钻石:" + diamond_num;
                        level = -1;
                        id = 0;
                    } else {
                        diamond_num = 0;
                        PackageInfo packageInfo = new PackageInfo();
                        packageInfo.imageUrlId = imageURL.id;
                        packageInfo.count = 1;
                        helper.updatePackageInfo(packageInfo);
//                        Drawable drawable = Drawable.createFromPath(imageURL.path);
//                        tv_open_records.setBackground(drawable);
                    }
                }
            }
//            Log.d(null, imageURL.url + "\n" + imageURL.path);
        }

        if (level == 1) {
            info =before_info + "抽到SSS_" + url_id;
            ToastUtil.show(this, info);
        }
        if (level == 2) {
            info = before_info + "抽到SSR_" + url_id;
//            ToastUtil.show(this, info);
        }
        if (level == 3) {
            info =before_info + "抽到SR_" + url_id;
//            ToastUtil.show(this, info);
        }
        if (level == 4) {
            info = before_info + "抽到R_" + url_id;
//            ToastUtil.show(this, info);
        }

        open_times++;

//        if (open_times == DefaultResource.MAX_SCRENN_LENGTH) {
//            open_times = 0;
//            tv_open_records.setText("");
//        }

//        tv_open_records.append(open_times + ": " + info + "\n\n");
        tv_open_records.append(info + "\n\n");

        DefaultResource.userInfo.diamond -= 50;
        DefaultResource.userInfo.diamond += diamond_num;
        show_userinfo();

        int childHeight = tv_open_records.getHeight();
        sv_open_records.scrollTo(0, childHeight);

        helper.updateUserInfo(DefaultResource.userInfo);
        return true;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317

3.1.3 PackageDetailActivity.java

public class PackageDetailActivity extends AppCompatActivity implements View.OnClickListener {

//    private TextView tv_title;
    private TextView tv_count;
    private TextView tv_goods_price;
    private TextView tv_goods_desc;
    private ImageView iv_goods_pic;
    private ChestDatabaseHelper mDBHelper;
    private int mImageUrlId;

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

//        tv_title = findViewById(R.id.tv_title);
        tv_count = findViewById(R.id.tv_count);
        tv_goods_price = findViewById(R.id.tv_goods_price);
        iv_goods_pic = findViewById(R.id.iv_goods_pic);
        tv_goods_desc = findViewById(R.id.tv_goods_desc);

        findViewById(R.id.iv_back).setOnClickListener(this);
//        findViewById(R.id.iv_cart).setOnClickListener(this);
        findViewById(R.id.btn_add_cart).setOnClickListener(this);

//        tv_count.setText(String.valueOf(MyApplication.getInstance().goodsCount));

        mDBHelper = ChestDatabaseHelper.getInstance(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        showDetail();
    }

    private void showDetail() {
        mImageUrlId = getIntent().getIntExtra("imageUriId", 0);
        if (mImageUrlId > 0) {
            ImageURL imageURL = mDBHelper.getImageUrlById(String.valueOf(mImageUrlId));
//            tv_title.setText(imageURL.simple_name);
            tv_goods_desc.setText("描述:说点啥吧");
            tv_goods_price.setText(String.valueOf((int)imageURL.diamond_cost));
            iv_goods_pic.setImageURI(Uri.parse(imageURL.path));
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
//            case R.id.iv_cart:
//                Intent intent = new Intent(this, ShoppingCartActivity.class);
//                startActivity(intent);
//                break;
            case R.id.btn_add_cart:
//                addToCart(mGoodsId);
                ToastUtil.show(this, "谢谢点赞!");
                break;
        }
    }

    private void addToCart(int goodsId) {
//        int count = ++MyApplication.getInstance().goodsCount;
//        tv_count.setText(String.valueOf(count));
//        mDBHelper.insertChartInfo(goodsId);
//        ToastUtil.show(this, "成功添加至购物车");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71

3.2 常量代码(constant)

3.2.1 DefaultResource .java

public class DefaultResource {
    public static List<ImageURL> imageURLS;
    public static UserInfo userInfo;
    public static final int MAX_SCRENN_LENGTH = 100;
    public static void init(Context ctx, TextView textView){
        imageURLS = new ArrayList<>();
        //  初始化默认字符串资源
        if (!ImageUrls.init_state) {
            ImageUrls.inti(ctx);
        }

        //  尝试下载资源
        new ImageDownloadTask(ctx, textView, ImageUrls.urls_str_level1, 1).execute();
        new ImageDownloadTask(ctx, textView, ImageUrls.urls_str_level2, 2).execute();
        new ImageDownloadTask(ctx, textView, ImageUrls.urls_str_level3, 3).execute();
        new ImageDownloadTask(ctx, textView, ImageUrls.urls_str_level4, 4).execute();

        userInfo = new UserInfo();
        userInfo.diamond = ctx.getResources().getInteger(R.integer.default_diamond);
        userInfo.gold_coin = ctx.getResources().getInteger(R.integer.default_gold_coin);

        userInfo.uname = ctx.getString(R.string.default_uname);
        userInfo.upwd = ctx.getString(R.string.default_pwd);
        userInfo.lucky_scale = (float) RandomUtils.getRandomDouble(Rules.userLuckyRange[0], Rules.userLuckyRange[1]);
        userInfo.id = 0;

        int idx1 = 0;
        int idx2 = idx1 + ImageUrls.IMG_URLS_LEVEL1.length;
        int idx3 = idx2 + ImageUrls.IMG_URLS_LEVEL2.length;
        int idx4 = idx3 + ImageUrls.IMG_URLS_LEVEL3.length;
        int idx5 = idx4 + ImageUrls.IMG_URLS_LEVEL4.length;

        Rules.chestIds = new int[][] {
                {idx1, idx2},
                {idx2, idx3},
                {idx3, idx4},
                {idx4, idx5}
        };
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

3.2.2 ImageUrls .java

public class ImageUrls {
    public static final int[] IMG_URLS_LEVEL1 = {
            R.string.img13_1, R.string.img14_1, R.string.img17_1, R.string.img20_1,
            R.string.img25_1, R.string.img27_1, R.string.img33_1, R.string.img34_1,
            R.string.img35_1, R.string.img37_1, R.string.img39_1, R.string.img41_1,
            R.string.img46_1, R.string.img51_1, R.string.img58_1, R.string.img62_1,
            R.string.img63_1
    };
    public static final int[] IMG_URLS_LEVEL2 = {
            R.string.img1_2, R.string.img2_2, R.string.img8_2, R.string.img10_2, R.string.img16_2,
            R.string.img18_2, R.string.img24_2, R.string.img40_2, R.string.img44_2, R.string.img45_2,
            R.string.img55_2, R.string.img56_2, R.string.img57_2, R.string.img61_2
    };

    public static final int[] IMG_URLS_LEVEL3 = {
            R.string.img3_3, R.string.img5_3, R.string.img9_3, R.string.img11_3, R.string.img12_3,R.string.img19_3,
            R.string.img22_3, R.string.img26_3, R.string.img38_3, R.string.img47_3, R.string.img48_3, R.string.img52_3,
            R.string.img53_3, R.string.img59_3, R.string.img60_3
    };

    public static final int[] IMG_URLS_LEVEL4 = {
            R.string.img4_4, R.string.img6_4, R.string.img7_4, R.string.img15_4, R.string.img21_4, R.string.img23_4,
            R.string.img28_4, R.string.img29_4, R.string.img30_4, R.string.img31_4, R.string.img32_4, R.string.img36_4,
            R.string.img42_4, R.string.img43_4, R.string.img49_4, R.string.img50_4, R.string.img54_4
    };

    public static String[] urls_str_level1;
    public static String[] urls_str_level2;
    public static String[] urls_str_level3;
    public static String[] urls_str_level4;

    public static boolean init_state = false;

    public static void inti(Context ctx){
        urls_str_level1 = new String[IMG_URLS_LEVEL1.length];
        for (int i = 0; i < IMG_URLS_LEVEL1.length; i++) {
            urls_str_level1[i] = ctx.getString(IMG_URLS_LEVEL1[i]);
        }

        urls_str_level2 = new String[IMG_URLS_LEVEL2.length];
        for (int i = 0; i < IMG_URLS_LEVEL2.length; i++) {
            urls_str_level2[i] = ctx.getString(IMG_URLS_LEVEL2[i]);
        }

        urls_str_level3 = new String[IMG_URLS_LEVEL3.length];
        for (int i = 0; i < IMG_URLS_LEVEL3.length; i++) {
            urls_str_level3[i] = ctx.getString(IMG_URLS_LEVEL3[i]);
        }

        urls_str_level4 = new String[IMG_URLS_LEVEL4.length];
        for (int i = 0; i < IMG_URLS_LEVEL4.length; i++) {
            urls_str_level4[i] = ctx.getString(IMG_URLS_LEVEL4[i]);
        }

        init_state = true;
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58

3.2.3 Rules .java

public class Rules {
    //  每个等级映射的字符串
    public static int[] level_ids = {
            R.string.SSS, R.string.SSR,
            R.string.SR, R.string.R};

    public static double[] min_costs = {
            60000, 10000, 1000, 200
    };

    public static double[] max_costs = {
            100000, 50000, 5000, 1000
    };

    public static double[] userLuckyRange = {0.001, 10};

    //  每个等级抽到物品的权重
    public static int[][] hit_weights = {
            {1000, 2000}, {2000, 200000}, {200000, 1000000}, {1000000, 10000000}
    };

    public static int[][] chestIds;

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

3.3 数据操作(Dao)

3.3.1 ImageURLDao .java

public class ImageURLDao extends SQLiteOpenHelper {
    private String tableName;

    private static ImageURLDao mDao;

    public static ImageURLDao getInstance(@Nullable Context context, @Nullable String dbname, String tableName, int version) {
        if (mDao == null) {
            mDao = new ImageURLDao(context, dbname, tableName, version);
        }
        return mDao;
    }

    public ImageURLDao(@Nullable Context context, @Nullable String dbname, String tableName, int version) {
        super(context, dbname, null, version);
        this.tableName = tableName;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "CREATE TABLE IF NOT EXISTS " + tableName +
                " (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
                " url VARCHAR NOT NULL," +
                " path VARCHAR NOT NULL," +
                " simple_name VARCHAR NOT NULL," +
                " complex_name VARCHAR NOT NULL," +
                " level_int INTEGER NOT NULL," +
                " level_id INTEGER NOT NULL," +
                " diamond_cost INTEGER NOT NULL" +
                ");";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//        if (oldVersion < 2) {
//            // 在第二个版本中添加新表
//            db.execSQL("CREATE TABLE new_table (id INTEGER PRIMARY KEY, name TEXT)");
//        }
//        if (oldVersion < 3) {
//            // 在第三个版本中更新现有表的列
//            db.execSQL("ALTER TABLE old_table ADD COLUMN new_column TEXT");
//        }
    }

    public void clear(SQLiteDatabase wdb) {
        wdb.delete(tableName, null, null);
    }

    public boolean isPrimaryKeyExist(SQLiteDatabase rdb, String id) {
        boolean exist = false;
        Cursor cursor = rdb.rawQuery("SELECT * FROM " + this.tableName + " WHERE id=?", new String[]{id});
        if (cursor != null) {
            exist = cursor.getCount() > 0;
            cursor.close();
        }
        return exist;
    }

    public void insertDefaultImageUrls(SQLiteDatabase wdb, List<ImageURL> list) {
        onCreate(wdb);
        try {
            wdb.beginTransaction();
            for (ImageURL imageURL : list) {
                if (!isPrimaryKeyExist(wdb, String.valueOf(imageURL.id))) { // 判断主键是否已经存在
                    ContentValues values = new ContentValues();
                    values.put("id", imageURL.id);
                    values.put("url", imageURL.url);
                    values.put("path", imageURL.path);
                    values.put("simple_name", imageURL.simple_name);
                    values.put("complex_name", imageURL.complex_name);
                    values.put("level_int", imageURL.level_int);
                    values.put("level_id", imageURL.level_id);
                    values.put("diamond_cost", imageURL.diamond_cost);
                    wdb.insert(this.tableName, null, values);
                }
            }
            wdb.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            wdb.endTransaction();
        }
    }

    public List<ImageURL> getAllImageUrls(SQLiteDatabase rdb) {
        List<ImageURL> resultList = new ArrayList<>();
        String[] columns = {"id", "url", "path", "simple_name", "complex_name", "level_int", "level_id", "diamond_cost"};
        Cursor cursor = rdb.query(this.tableName, columns, null, null, null, null, null);
        while (cursor.moveToNext()) {
            int id = cursor.getInt(0);
            String url = cursor.getString(1);
            String path = cursor.getString(2);
            String simpleName = cursor.getString(3);
            String complexName = cursor.getString(4);
            int levelInt = cursor.getInt(5);
            int leveId = cursor.getInt(6);
            int diamondCost = cursor.getInt(7);
            ImageURL imageUrl = new ImageURL(id, url, path, simpleName, complexName, levelInt, leveId, diamondCost);
            resultList.add(imageUrl);
        }
        cursor.close();
        return resultList;
    }

    public ImageURL searchImageUrlById(SQLiteDatabase rdb, String imageUrlId){
        ImageURL result = null;
        String[] columns = {"id", "url", "path", "simple_name", "complex_name", "level_int", "level_id", "diamond_cost"};
        String selection = "id=?";
        String[] selectionArgs = {imageUrlId};
        Cursor cursor = rdb.query(this.tableName, columns, selection, selectionArgs, null, null, null);
        if (cursor.moveToNext()) {
            int id = cursor.getInt(0);
            String url = cursor.getString(1);
            String path = cursor.getString(2);
            String simpleName = cursor.getString(3);
            String complexName = cursor.getString(4);
            int levelInt = cursor.getInt(5);
            int leveId = cursor.getInt(6);
            int diamondCost = cursor.getInt(7);
            result = new ImageURL(id, url, path, simpleName, complexName, levelInt, leveId, diamondCost);
        }
        cursor.close();
        return result;
    }

    public List<ImageURL> searchByLevelId(SQLiteDatabase rdb, int levelId) {
        List<ImageURL> result = new ArrayList<>();
        String[] columns = {"id", "url", "path", "simple_name", "complex_name", "level_int", "level_id", "diamond_cost"};
        String selection = "level_id = ?";
        String[] selectionArgs = {String.valueOf(levelId)};
        Cursor cursor = rdb.query(this.tableName, columns, selection, selectionArgs, null, null, null);
        while (cursor.moveToNext()) {
            int id = cursor.getInt(0);
            String url = cursor.getString(1);
            String path = cursor.getString(2);
            String simpleName = cursor.getString(3);
            String complexName = cursor.getString(4);
            int levelInt = cursor.getInt(5);
            int leveId = cursor.getInt(6);
            int diamondCost = cursor.getInt(7);
            ImageURL imageURL = new ImageURL(id, url, path, simpleName, complexName, levelInt, leveId, diamondCost);
            result.add(imageURL);
        }
        cursor.close();
        return result;
    }

    public ImageURL getImageUrlByLvelIdAndCount(SQLiteDatabase rdb, int levelId, int count_id){
        List<ImageURL> imageURLS = searchByLevelId(rdb, levelId);
        if (count_id >= imageURLS.size()) {
            return new ImageURL();
        }

        return imageURLS.get(count_id);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156

3.3.2 PackageInfoDao.java

public class PackageInfoDao extends SQLiteOpenHelper {
    private String tableName;
    private static PackageInfoDao mDao = null;

    public static PackageInfoDao getInstance(@Nullable Context context, @Nullable String dbname, String tableName, int version) {
        if (mDao == null) {
            mDao = new PackageInfoDao(context, dbname, tableName, version);
        }
        return mDao;
    }

    public PackageInfoDao(@Nullable Context context, @Nullable String dbname, String tableName, int version) {
        super(context, dbname, null, version);
        this.tableName = tableName;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "CREATE TABLE IF NOT EXISTS " + tableName +
                " (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
                " image_url_id INTEGER NOT NULL," +
                " count INTEGER NOT NULL" +
                ");";
        db.execSQL(sql);
    }

    public boolean isPrimaryKeyExist(SQLiteDatabase rdb, String id) {
        boolean exist = false;
        Cursor cursor = rdb.rawQuery("SELECT * FROM " + this.tableName + " WHERE id=?", new String[]{id});
        if (cursor != null) {
            exist = cursor.getCount() > 0;
            cursor.close();
        }
        return exist;
    }

    public boolean isImageUrlIdExists(SQLiteDatabase rdb, String imageUrlId) {
        boolean exist = false;
        Cursor cursor = rdb.rawQuery("SELECT * FROM " + this.tableName + " WHERE image_url_id=?", new String[]{imageUrlId});
        if (cursor != null) {
            exist = cursor.getCount() > 0;
            cursor.close();
        }
        return exist;
    }

    public PackageInfo getPackageInfoByImageUrlId(SQLiteDatabase rdb, String imageUrlId){
        PackageInfo packageInfo = null;

        Cursor cursor = rdb.rawQuery("SELECT * FROM " + this.tableName + " WHERE image_url_id=?", new String[]{imageUrlId});
        if (cursor != null) {
            if (cursor.moveToNext()) {
                packageInfo = new PackageInfo();
                packageInfo.id = cursor.getInt(0);
                packageInfo.imageUrlId = cursor.getInt(1);
                packageInfo.count = cursor.getInt(2);
            }
            cursor.close();
        }

        return packageInfo;
    }
    public List<PackageInfo> getAllPackageInfo(SQLiteDatabase rdb){
        List<PackageInfo> packageList = new ArrayList<>();

        Cursor cursor = rdb.rawQuery("SELECT * FROM " + this.tableName, null);
        if (cursor != null){
            while(cursor.moveToNext()){
                PackageInfo packageInfo = new PackageInfo();
                packageInfo.id = cursor.getInt(0);
                packageInfo.imageUrlId = cursor.getInt(1);
                packageInfo.count = cursor.getInt(2);
                packageList.add(packageInfo);
            }
            cursor.close();
        }

        return packageList;
    }
    public void insertPackageInfo(SQLiteDatabase wdb, PackageInfo packageInfo) {
        onCreate(wdb);

        try {
            wdb.beginTransaction();
            ContentValues values = new ContentValues();
            values.put("image_url_id", packageInfo.imageUrlId);
            values.put("count", packageInfo.count);
            wdb.insert(this.tableName, null, values);
            wdb.setTransactionSuccessful();
        }catch (Exception e) {
            e.printStackTrace();
        } finally {
            wdb.endTransaction();
        }
    }

    public void updatePackageInfo(SQLiteDatabase wdb, PackageInfo packageInfo) {
        onCreate(wdb);

        PackageInfo packageInfoTemp = getPackageInfoByImageUrlId(wdb, String.valueOf(packageInfo.imageUrlId));
        if(packageInfoTemp == null) {
            insertPackageInfo(wdb, packageInfo);
            return;
        } else {
            packageInfo.id = packageInfoTemp.id;
            packageInfo.count = ++packageInfoTemp.count;
        }

        try {
            wdb.beginTransaction();
            ContentValues values = new ContentValues();
            values.put("image_url_id", packageInfo.imageUrlId);
            values.put("count", packageInfo.count);
            wdb.update(this.tableName, values, "id=?", new String[] {String.valueOf(packageInfo.id)});
            wdb.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            wdb.endTransaction();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public void clear(SQLiteDatabase mWDB) {
        mWDB.delete(tableName, null, null);
    }
    public void deletePackageInfo(SQLiteDatabase mWDB, PackageInfo packageInfo) {
        mWDB.delete(tableName, "id=?", new String[]{String.valueOf(packageInfo.id)});
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134

3.3.3 UserInfoDao.java

public class UserInfoDao extends SQLiteOpenHelper {
    private String tableName;
    private static UserInfoDao mDao = null;

    public static UserInfoDao getInstance(@Nullable Context context, @Nullable String dbname, String tableName, int version) {
        if (mDao == null) {
            mDao = new UserInfoDao(context, dbname, tableName, version);
        }
        return mDao;
    }

    public UserInfoDao(@Nullable Context context, @Nullable String dbname, String tableName, int version) {
        super(context, dbname, null, version);
        this.tableName = tableName;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "CREATE TABLE IF NOT EXISTS " + tableName +
                " (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
                " uname VARCHAR NOT NULL," +
                " upwd VARCHAR NOT NULL," +
                " gold_coin INTEGER NOT NULL," +
                " diamond INTEGER NOT NULL," +
                " lucky_scale FLOAT NOT NULL" +
                ");";
        db.execSQL(sql);
    }

    public boolean isPrimaryKeyExist(SQLiteDatabase rdb, String id) {
        boolean exist = false;
        Cursor cursor = rdb.rawQuery("SELECT * FROM " + this.tableName + " WHERE id=?", new String[]{id});
        if (cursor != null) {
            exist = cursor.getCount() > 0;
            cursor.close();
        }
        return exist;
    }

    public void insertDefaultUserInfo(SQLiteDatabase wdb, UserInfo userInfo) {
        onCreate(wdb);
        try {
            wdb.beginTransaction();
            ContentValues values = new ContentValues();
            if (!isPrimaryKeyExist(wdb, String.valueOf(userInfo.id))) {
                values.put("id", userInfo.id);
                values.put("uname", userInfo.uname);
                values.put("upwd", userInfo.upwd);
                values.put("gold_coin", userInfo.gold_coin);
                values.put("diamond", userInfo.diamond);
                values.put("lucky_scale", userInfo.lucky_scale);
                wdb.insert(this.tableName, null, values);
            }
            wdb.setTransactionSuccessful();
        }catch (Exception e) {
            e.printStackTrace();
        } finally {
            wdb.endTransaction();
        }
    }
    public void updateUserInfo(SQLiteDatabase wdb, UserInfo userInfo) {
        try {
            wdb.beginTransaction();
            ContentValues values = new ContentValues();
            values.put("uname", userInfo.uname);
            values.put("upwd", userInfo.upwd);
            values.put("gold_coin", userInfo.gold_coin);
            values.put("diamond", userInfo.diamond);
            values.put("lucky_scale", userInfo.lucky_scale);
            wdb.update(this.tableName, values, "id=?", new String[] {String.valueOf(userInfo.id)});
            wdb.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            wdb.endTransaction();
        }
    }

    public boolean userLogin(SQLiteDatabase rdb, UserInfo userInfo) {
        Cursor cursor = null;
        try {
            cursor = rdb.query(tableName, null, "uname=? and upwd=?",
                    new String[]{userInfo.uname, userInfo.upwd}, null, null, null);

            if (cursor.moveToNext()) {
                DefaultResource.userInfo.uname = userInfo.uname;
                DefaultResource.userInfo.upwd = userInfo.upwd;
                DefaultResource.userInfo.gold_coin = cursor.getInt(3);
                DefaultResource.userInfo.diamond = cursor.getInt(4);
            }

            return cursor.getCount() > 0;
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public void clear(SQLiteDatabase mWDB) {
        mWDB.delete(tableName, null, null);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108

3.4. 数据库辅助(Database)

ChestDatabaseHelper .java

public class ChestDatabaseHelper extends SQLiteOpenHelper {
    private static String DB_NAME = "chest.db";
    private static int VERSION_ID = 1;
    private Context context;

    public static String TABLE_NAME_IMAGE_URL = "ImageURL";
    public static String TABLE_NAME_USER_INFO = "UserInfo";
    public static String TABLE_NAME_PACKAGE_INFO = "PackageInfo";

    private static ChestDatabaseHelper mHelper;

    private SQLiteDatabase mRDB = null;
    private SQLiteDatabase mWDB = null;
    private ImageURLDao mImageUrlDao;
    private UserInfoDao mUserInfoDao;
    private PackageInfoDao mPackageInfoDao;

    public static ChestDatabaseHelper getInstance(Context context) {
        if (mHelper == null) {
            mHelper = new ChestDatabaseHelper(context);
        }
        return mHelper;
    }

    public ChestDatabaseHelper(@Nullable Context context) {
        super(context, DB_NAME, null, VERSION_ID);
        this.context = context;
    }

    private void prepareDaos(){
        if (mImageUrlDao == null) {
            mImageUrlDao = ImageURLDao.getInstance(this.context, DB_NAME, TABLE_NAME_IMAGE_URL, VERSION_ID);
        }

        if (mUserInfoDao == null) {
            mUserInfoDao = UserInfoDao.getInstance(this.context, DB_NAME, TABLE_NAME_USER_INFO, VERSION_ID);
        }

        if (mPackageInfoDao == null) {
            mPackageInfoDao = PackageInfoDao.getInstance(this.context, DB_NAME, TABLE_NAME_PACKAGE_INFO, VERSION_ID);
        }
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        prepareDaos();
        mImageUrlDao.onCreate(db);
        mUserInfoDao.onCreate(db);
        mPackageInfoDao.onCreate(db);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public void insertDefaultValues(){
        prepareDaos();
        mImageUrlDao.insertDefaultImageUrls(mWDB, DefaultResource.imageURLS);
//        mUserInfoDao.insertDefaultUserInfo(mWDB, DefaultResource.userInfo);
    }

    public List<ImageURL> getAllImageUrls(){
        prepareDaos();
        return mImageUrlDao.getAllImageUrls(mRDB);
    }

    public void deletePackageInfo(PackageInfo packageInfo){
        prepareDaos();
        mPackageInfoDao.deletePackageInfo(mWDB, packageInfo);
    }

    public ImageURL getImageUrlById(String imageUrlId){
        prepareDaos();
        return mImageUrlDao.searchImageUrlById(mRDB, imageUrlId);
    }

    public List<PackageInfo> getAllPackageInfos() {
        prepareDaos();
        return  mPackageInfoDao.getAllPackageInfo(mRDB);
    }

    public void updatePackageInfo(PackageInfo packageInfo) {
        prepareDaos();
        mPackageInfoDao.updatePackageInfo(mRDB, packageInfo);
    }

    public void addUserInfo(){
        prepareDaos();
        mUserInfoDao.insertDefaultUserInfo(mWDB, DefaultResource.userInfo);
    }

    public boolean userLogin(UserInfo userInfo) {
        prepareDaos();
        return mUserInfoDao.userLogin(mRDB, userInfo);
    }

    public ImageURL getImageUrlByIdAndCount(int levelId, int count) {
        prepareDaos();
        return mImageUrlDao.getImageUrlByLvelIdAndCount(mRDB, levelId, count);
    }

    public void updateUserInfo(UserInfo userInfo) {
        prepareDaos();
        mUserInfoDao.updateUserInfo(mRDB, userInfo);
    }

    public void clearImageUrlDatas(){
        prepareDaos();
        mImageUrlDao.clear(mWDB);
    }

    public void clearUserInfos(){
        prepareDaos();
        mUserInfoDao.clear(mWDB);
    }

    public void closeLink() {
        if (mRDB != null && mRDB.isOpen()) {
            mRDB.close();
            mRDB = null;
        }

        if (mWDB != null && mWDB.isOpen()) {
            mWDB.close();
            mWDB = null;
        }
    }

    public SQLiteDatabase openWriteLink() {
        if (mWDB == null || !mWDB.isOpen()) {
            mWDB = mHelper.getWritableDatabase();
        }
        return mWDB;
    }

    public SQLiteDatabase openReadLink() {
        if (mRDB == null || !mRDB.isOpen()) {
            mRDB = mHelper.getReadableDatabase();
        }
        return mRDB;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144

3.5. 实体类(Entity)

3.5.1 ChestRecord.java

//  开箱记录
public class ChestRecord {
    // 主键
    public int id;
    // 抽到的随机点数
    public int roll_count;
    // 抽到的第二级随机点数
    public int rand_second;
    public int imageUrlId;
    public String simple_name;

    public ChestRecord(int id, int roll_count, int rand_second, int imageUrlId, String simple_name) {
        this.id = id;
        this.roll_count = roll_count;
        this.rand_second = rand_second;
        this.imageUrlId = imageUrlId;
        this.simple_name = simple_name;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

3.5.2 ImageURL .java

public class ImageURL {
    //  主键
    public int id;
    //  网络资源路径
    public String url;
    //  本地资源路径
    public String path;
    //  简单名称标识
    public String simple_name;
    //  复杂名称标识
    public String complex_name;
    //  SSR SSS SR R
    public int level_int;
    public int level_id;

    public long diamond_cost;

    public ImageURL(int id, String url, String path, String simple_name, String complex_name,int level_int, int level_id, double diamond_cost) {
        this.id = id;
        this.url = url;
        this.path = path;
        this.simple_name = simple_name;
        this.level_int = level_int;
        this.level_id = level_id;
        this.diamond_cost = (long)diamond_cost;
        this.complex_name = complex_name;
    }

    public ImageURL() {
    }

    @Override
    public String toString() {
        return "ImageURL{" +
                "id=" + id +
                ", url='" + url + '\'' +
                ", path='" + path + '\'' +
                ", simple_name='" + simple_name + '\'' +
                ", complex_name='" + complex_name + '\'' +
                ", level_int=" + level_int +
                ", level_id=" + level_id +
                ", diamond_cost=" + diamond_cost +
                '}';
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

3.5.3 PackageInfo .java

public class PackageInfo {
    //  主键
    public int id;
    public int imageUrlId;
    public int count;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

3.5.4 UserInfo.java

package com.example.chests.entity;

public class UserInfo {
    //  主键
    public int id;
    //  用户名
    public String uname;
    //  密码
    public String upwd;

    //  金币
    public long gold_coin;
    //  钻石
    public long diamond;
    //  幸运因子(0.1-10) X 物品的权重
    public float lucky_scale;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

3.6 工具类(utils)

3.6.1 ImageDownloadTask .java

public class ImageDownloadTask extends AsyncTask<String, Integer, List<String>> {
    private Context context;
    private TextView textView;
    private String[] urls;
    private List<String> paths;
    private int index;
    private int level;
    private int index_before;

    public ImageDownloadTask(Context context, TextView textView, String[] urls, int index) {
        this.context = context;
        this.textView = textView;
        this.urls = urls;
        this.paths = new ArrayList<>();
        this.level = index;

        int idx1 = 0;
        int idx2 = idx1 + ImageUrls.IMG_URLS_LEVEL1.length;
        int idx3 = idx2 + ImageUrls.IMG_URLS_LEVEL2.length;
        int idx4 = idx3 + ImageUrls.IMG_URLS_LEVEL3.length;

        switch (index) {
            case 1:
                this.index = idx1;
                break;
            case 2:
                this.index = idx2;
                break;
            case 3:
                this.index = idx3;
                break;
            case 4:
                this.index = idx4;
                break;
        }
        index_before = this.index;
    }

    @Override
    protected void onPreExecute() {
    }

    public Bitmap scaleBitmap(Bitmap bitmap, float scale) {
        int width = Math.round(bitmap.getWidth() * scale);
        int height = Math.round(bitmap.getHeight() * scale);

        Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap, width, height, false);

        return newBitmap;
    }

    @Override
    protected List<String> doInBackground(String... params) {
        for (String url : urls) {
            try {
                String fileName = "image_" + index + ".jpg";
                File file = new File(context.getFilesDir(), fileName);

                if (file.exists()) {
                    paths.add(file.getAbsolutePath());
                    index++;
                    Log.d("ImageDownloadTask", "文件已存在");
                    Log.d("ImageDownloadTask", context.getFilesDir() + "/" + fileName);
                    continue;
                }

                URL urlConnection = new URL(url);
                HttpURLConnection connection = (HttpURLConnection) urlConnection.openConnection();
                connection.setDoInput(true);
                connection.connect();
                InputStream input = connection.getInputStream();
                Bitmap bitmap = BitmapFactory.decodeStream(input);
                bitmap = scaleBitmap(bitmap, 0.35f);
                if (bitmap != null) {
                    FileOutputStream outputStream = context.openFileOutput(fileName, Context.MODE_PRIVATE);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
                    outputStream.flush();
                    outputStream.close();

                    paths.add(file.getAbsolutePath());
                    index++;
                }
            } catch (Exception e) {
                Log.e("ImageDownloadTask", "图片下载失败", e);
            }
        }
        return paths;
    }

    @Override
    protected void onPostExecute(List<String> result) {
        if (result != null && result.size() == urls.length) {
            int level_id = this.level - 1;
            for (int i = 0; i < urls.length; i++) {
                String level_name = context.getString(Rules.level_ids[level_id]) + "_" + i;

                ImageURL imageURL= new ImageURL(index_before++, urls[i], paths.get(i),
                        level_name, "", Rules.level_ids[level_id], level_id + 1,
                        RandomUtils.getRandomDouble(
                                Rules.min_costs[level_id],
                                Rules.max_costs[level_id])
                        );

                DefaultResource.imageURLS.add(imageURL);
            }

            if (textView != null) {
                if (this.level == 4) {
                    textView.setText("下载成功");
                }
            }
        } else {
            if (textView != null) {
                if (this.level == 4) {
                    textView.setText("下载失败");
                }
            }
        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121

3.6.2 MathUtils .java

public class MathUtils {
    public static String formatNumber(int number) {
        double level1 = 100000000;
        double level2 = 10000;

        if (number >= level1) {
            return String.format("%.2fM", number / level1);
        } else if (number >= level2) {
            return String.format("%.2fw", number / level2);
        } else {
            return String.valueOf(number);
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

3.6.3 RandomUtils .java

package com.example.chests.utils;
import java.util.Random;

public class RandomUtils {
    private static final Random RANDOM = new Random();
    /**
     * 生成指定区间内的整型随机数
     * @param min 最小值(包含)
     * @param max 最大值(包含)
     * @return 生成的随机数
     */
    public static int getRandomInt(int min, int max) {
        return RANDOM.nextInt(max - min + 1) + min;
    }

    /**
     * 生成指定区间内的浮点型随机数
     * @param min 最小值(包含)
     * @param max 最大值(包含)
     * @return 生成的随机数
     */
    public static double getRandomDouble(double min, double max) {
        return min + (max - min) * RANDOM.nextDouble();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

3.6.4 SharedUtil .java

public class SharedUtil {
    private static SharedUtil mUtil;
    private SharedPreferences preferences;

    public static SharedUtil getInstance(Context ctx) {
        if (mUtil == null) {
            mUtil = new SharedUtil();
            mUtil.preferences = ctx.getSharedPreferences("chests", Context.MODE_PRIVATE);
        }
        return mUtil;
    }

    public void writeString(String key, String value) {
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(key, value);
        editor.commit();
    }

    public String readString(String key, String defaultValue) {
        return preferences.getString(key, defaultValue);
    }

    public void writeBoolean(String key, boolean value) {
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    public boolean readBoolean(String key, boolean defaultValue) {
        return preferences.getBoolean(key, defaultValue);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

3.6.5 ToastUtil.java

public class ToastUtil {
    public static void show(Context ctx, String desc) {
        Toast.makeText(ctx, desc, Toast.LENGTH_SHORT).show();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5

4. 前端布局代码

4.1 主要布局

4.1.1 activity_log.xml

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#e5e4e2">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="76dp"
        android:text="开宝箱啦"
        android:textColor="#ffd700"
        android:textSize="30sp"
        android:textStyle="bold"
        android:gravity="center"
        android:layout_marginTop="150dp"
        ></TextView>

    <EditText
        android:id="@+id/ei_uname"
        android:layout_width="411dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:hint="用户名"
        android:layout_marginTop="50dp"
        android:textAlignment="center"
        >

    </EditText>

    <EditText
        android:id="@+id/ei_upwd"
        android:layout_width="411dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:hint="密码"
        android:textAlignment="center"
        android:inputType="textPassword"
        >

    </EditText>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        >
        <Button
            android:id="@+id/btn_login"
            android:layout_width="150dp"
            android:layout_height="59dp"
            android:layout_gravity="center"
            android:text="登陆"
            android:textSize="20sp"
            android:background="#B986F2"
            />
        <CheckBox
            android:id="@+id/cbx_rember_pwd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/btn_login"
            android:text="记住密码"
            android:textSize="20sp"
            android:paddingVertical="10dp"
            >
        </CheckBox>
    </RelativeLayout>

    <TextView
        android:visibility="gone"
        android:id="@+id/tv_listen_download"
        android:layout_width="0dp"
        android:layout_height="0dp">
    </TextView>

</LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76

4.1.2 activity_main.xml

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFEDD"
    android:orientation="vertical"
    >
    <!--    <LinearLayout-->
<!--        android:layout_width="match_parent"-->
<!--        android:layout_height="0dp"-->
<!--        android:layout_weight="10"-->
<!--        >-->

<!--        </ImageView>-->


<!--    </LinearLayout>-->

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible"
        >
        <include layout="@layout/chest_title"
            android:id="@+id/ll_top"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            />

            <LinearLayout
                android:id="@+id/ll_open_chest"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/ll_top"
                android:layout_above="@+id/ll_bottom"
                android:orientation="vertical"
                android:visibility="visible"
                >
                <ImageView
                    android:layout_width="200dp"
                    android:layout_height="200dp"
                    android:layout_gravity="center"
                    android:src="@drawable/ic_chest"
                    >
                </ImageView>
                <ImageView
                    android:id="@+id/iv_open_once"
                    android:layout_width="200dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:src="@drawable/ic_open_chest_once"
                    >
                </ImageView>
                <ImageView
                    android:id="@+id/iv_open_five"
                    android:layout_width="200dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:src="@drawable/ic_btn_open_chest_five"
                    >
                </ImageView>

                <ScrollView
                    android:id="@+id/sv_open_records"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <TextView
                        android:id="@+id/tv_open_records"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@drawable/ic_open_info"
                        android:scaleType="centerCrop"
                        android:textAlignment="center"
                        android:textColor="@color/open_record_back"
                        android:textSize="20sp">

                    </TextView>
                </ScrollView>
            </LinearLayout>

            <ScrollView
                android:id="@+id/ss_package"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/ll_top"
                android:layout_above="@+id/ll_bottom"
                android:visibility="gone"
                >
                <LinearLayout
                    android:id="@+id/ll_package"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    >
                </LinearLayout>
            </ScrollView>
<!--        <LinearLayout-->
<!--            android:id="@+id/ll_package"-->
<!--            android:layout_width="match_parent"-->
<!--            android:layout_height="wrap_content"-->
<!--            android:layout_below="@+id/ll_top"-->
<!--            android:layout_above="@+id/ll_bottom"-->
<!--            android:orientation="vertical"-->
<!--            android:visibility="visible"-->
<!--            >-->
<!--        </LinearLayout>-->
            <LinearLayout
                android:id="@+id/ll_record"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="gone"
                >
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:textSize="50dp"
                    android:text="RECORD"
                    android:gravity="center"
                    >
                </TextView>
            </LinearLayout>

        <include layout="@layout/activity_bottom"
            android:id="@+id/ll_bottom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            >
        </include>
    </RelativeLayout>
</LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134

4.2 主要布局

4.2.1 activity_bottom.xml

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:orientation="vertical"
    android:background="#e5e4e2"
    >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
            <Button
                android:id="@+id/btn_open_chest"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="开箱子"
                android:gravity="center"
                android:textSize="20sp"
                android:textColor="@color/black"
                android:background="@drawable/tv_selt"
                >
            </Button>
            <Button
                android:id="@+id/btn_package"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="背包"
                android:gravity="center"
                android:textSize="20sp"
                android:textColor="@color/black"
                android:background="@drawable/tv_selt"

                >
            </Button>
            <Button
                android:id="@+id/btn_record"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="记录"
                android:gravity="center"
                android:textSize="20sp"
                android:textColor="@color/black"
                android:background="@drawable/tv_selt"
                >
            </Button>
        </LinearLayout>
</LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50

4.2.2 activity_package_detail.xml

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/orange"
    android:orientation="vertical"
    >
    <include layout="@layout/activity_package_detaild_title" />
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <ImageView
                android:id="@+id/iv_goods_pic"
                android:layout_width="match_parent"
                android:layout_height="350dp"
                android:scaleType="fitCenter"
                android:src="@drawable/ic_title_back" />
            <TextView
                android:id="@+id/tv_goods_price"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="5dp"
                android:textColor="@color/red"
                android:textSize="22sp"
                android:text="1990" />
            <TextView
                android:id="@+id/tv_goods_desc"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="5dp"
                android:textSize="15sp"
                android:textColor="@color/black"
                android:text="小米 MI10 8GB+128GB 钛银黑 5G手机 游戏拍照手机"/>
            <Button
                android:id="@+id/btn_add_cart"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="点赞点赞"
                android:textColor="@color/black"
                android:textSize="17sp" />
        </LinearLayout>
    </ScrollView>
</LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

4.2.3 activity_package_detaild_title.xml

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="@drawable/ic_title_back"
    android:layout_height="60dp"
    >
        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/iv_back"
            android:textSize="25sp"
            android:padding="10dp"
            android:textColor="@color/black"
            android:text="物品详情"
            >
        </TextView>

    <ImageView
        android:id="@+id/iv_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:padding="10dp"
        android:src="@drawable/ic_back"
        android:scaleType="fitCenter"
        >
    </ImageView>
<!--    <TextView-->
<!--        android:id="@+id/tv_gold_coin"-->
<!--        android:layout_width="wrap_content"-->
<!--        android:layout_height="wrap_content"-->
<!--        android:layout_toRightOf="@id/tv_gold_coin_title"-->
<!--        android:textSize="25sp"-->
<!--        android:padding="10dp"-->
<!--        android:textColor="@color/black"-->
<!--        android:text="999w"-->
<!--        >-->
<!--    </TextView>-->

<!--    <ImageView-->
<!--        android:id="@+id/tv_diamond_title"-->
<!--        android:layout_width="wrap_content"-->
<!--        android:layout_height="wrap_content"-->
<!--        android:layout_centerInParent="true"-->
<!--        android:layout_toLeftOf="@+id/tv_diamond"-->
<!--        android:padding="10dp"-->
<!--        android:src="@drawable/ic_diamond"-->
<!--        >-->
<!--    </ImageView>-->
<!--    <TextView-->
<!--        android:id="@+id/tv_diamond"-->
<!--        android:layout_width="wrap_content"-->
<!--        android:layout_height="wrap_content"-->
<!--        android:gravity="center"-->
<!--        android:layout_alignParentRight="true"-->
<!--        android:padding="12dp"-->
<!--        android:text="10000"-->
<!--        android:textColor="@color/black"-->
<!--        android:textSize="25sp">-->
<!--    </TextView>-->
</RelativeLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63

4.2.4 activity_package_item.xml

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:orientation="horizontal"
    >

    <ImageView
        android:id="@+id/iv_thumb"
        android:layout_width="85dp"
        android:layout_height="85dp"
        android:scaleType="fitCenter"
        android:src="@color/open_record_back"
        />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:orientation="vertical"
        >
        <TextView
            android:id="@+id/tv_name"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2"
            android:gravity="left|center"
            android:textColor="@color/black"
            android:textSize="17sp"
            android:text="小米手机" />
        <TextView
            android:id="@+id/tv_desc"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="3"
            android:gravity="left|center"
            android:textColor="@color/black"
            android:textSize="12sp"
            android:text="小米 MI10 8GB+128GB 钛银黑 5G 手机 游戏拍照手机"
            />
    </LinearLayout>
    <TextView
        android:id="@+id/tv_count"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:textColor="@color/black"
        android:textSize="17sp"
        android:text="2"
        />
    <TextView
        android:id="@+id/tv_price"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="right|center"
        android:textColor="@color/black"
        android:textSize="15sp"
        android:text="1000"
        />
    <TextView
        android:id="@+id/tv_sum"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1.2"
        android:gravity="right|center"
        android:textColor="@color/red"
        android:textSize="17sp"
        android:text="2000"
        />
</LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74

4.2.5 activity_record_item.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</androidx.constraintlayout.widget.ConstraintLayout>
  • 1
  • 2
  • 3
  • 4
  • 5

4.2.6 chest_title.xml

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="@drawable/ic_title_back"
    android:layout_height="60dp"
    >
<!--    <TextView-->
<!--        android:id="@+id/tv_title"-->
<!--        android:layout_width="wrap_content"-->
<!--        android:layout_height="wrap_content"-->
<!--        android:layout_alignParentLeft="true"-->
<!--        android:padding="7dp"-->
<!--        android:textSize="30sp"-->
<!--        android:textColor="#FD393B"-->
<!--        android:text="开箱子"-->
<!--        >-->
<!--    </TextView>-->
    <ImageView
        android:id="@+id/tv_gold_coin_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:padding="10dp"
        android:src="@drawable/ic_gold_coin"
        android:scaleType="fitCenter"
        >
    </ImageView>
    <TextView
        android:id="@+id/tv_gold_coin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/tv_gold_coin_title"
        android:textSize="25sp"
        android:padding="10dp"
        android:textColor="@color/black"
        android:text="999w"
        >
    </TextView>

    <ImageView
        android:id="@+id/tv_diamond_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_toLeftOf="@+id/tv_diamond"
        android:padding="10dp"
        android:src="@drawable/ic_diamond"
        >
    </ImageView>
    <TextView
        android:id="@+id/tv_diamond"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_alignParentRight="true"
        android:padding="12dp"
        android:text="10000"
        android:textColor="@color/black"
        android:textSize="25sp">
    </TextView>
</RelativeLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62

5. 结果演示

5.1 登录界面

在这里插入图片描述

5.2 主界面

在这里插入图片描述

5.3 背包界面

在这里插入图片描述

5.4 详情页

在这里插入图片描述

声明:

本游戏项目示例仅供学习和研究使用,不得用于商业用途或其他非法用途。本项目中所有素材、代码和文档均为原创或合法获取,并严格遵守相关法律法规和协议。使用者应当承担使用本项目可能产生的一切责任和风险,包括但不限于数据损失、程序错误、安全问题等。本项目的所有权归原作者所有,未经允许不得进行修改、复制、传播或用于其他用途,违者必将承担相应法律责任。使用本项目即表示您已经完全认同并遵守此声明。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/613693
推荐阅读
相关标签