一、功能与要求
实现功能:设计一个猜猜鸡蛋在哪只鞋子里游戏。在UI上放置三只鞋子,单击其中的任意一只鞋子,将打开鞋子显示里面是否有鸡蛋,如果猜中,设置该图片为半透明显示,并提示信息“猜对了”,如果猜错,提示信息为“再玩一次?”。
指标要求:实现UI布局;业务功能应实现鸡蛋随机显示在某一只鞋子里。
提交作品:项目设计报告。
二、方案设计
(一)UI布局设计
图1 猜鸡蛋游戏界面
整体界面采用LinearLayout(线性布局)嵌套TableLayout(表格布局)的布局方式。
界面中包含的控件有:
TextView:用于显示游戏的提示文本 “猜猜鸡蛋在哪只鞋子里?”。
TableLayout内包含一个TableRow,TableRow中有三个ImageView,分别代表三只鞋子的图像展示区域。
Button:用于触发 “再玩一次” 的操作按钮。
布局设计如下所示:
一、功能与要求
实现功能:设计一个猜猜鸡蛋在哪只鞋子里游戏。在UI上放置三只鞋子,单击其中的任意一只鞋子,将打开鞋子显示里面是否有鸡蛋,如果猜中,设置该图片为半透明显示,并提示信息“猜对了”,如果猜错,提示信息为“再玩一次?”。
指标要求:实现UI布局;业务功能应实现鸡蛋随机显示在某一只鞋子里。
提交作品:项目设计报告。
二、方案设计
(一)UI布局设计
图1 猜鸡蛋游戏界面
整体界面采用LinearLayout(线性布局)嵌套TableLayout(表格布局)的布局方式。
界面中包含的控件有:
TextView:用于显示游戏的提示文本 “猜猜鸡蛋在哪只鞋子里?”。
TableLayout内包含一个TableRow,TableRow中有三个ImageView,分别代表三只鞋子的图像展示区域。
Button:用于触发 “再玩一次” 的操作按钮。
布局设计如下所示:
图2 猜鸡蛋游戏布局设计
(二)业务流程图
当玩家开始游戏时,屏幕上将显示 3 只鞋子,单击其中的任意一只鞋子,程序判断该鞋子中是否有鸡蛋,并且打开鞋子显示结果,此时可以通过单击“再玩一次”按钮重新开始游戏。具体的流程如下图所示:
图3 业务流程图
(三)项目实现步骤
步骤1:搭建开发环境。
步骤2:准备资源。
步骤3:UI设计与开发。
步骤4:编写程序实现游戏规则。
三、项目实现
(一)UI布局
1.项目UI布局如下:
2.项目UI布局代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/a"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/txt_img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:gravity="center"
android:text="猜猜鸡蛋在哪只鞋子里?"
android:textColor="#190202"
android:textSize="30sp"
android:textStyle="bold" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/img1"
android:layout_width="wrap_content"
android:layout_height="302dp"
android:layout_weight="1"
app:srcCompat="@drawable/shoe_sorry" />
<ImageView
android:id="@+id/img2"
android:layout_width="wrap_content"
android:layout_height="311dp"
android:layout_weight="1"
app:srcCompat="@drawable/shoe_sorry" />
<ImageView
android:id="@+id/img3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@drawable/shoe_sorry" />
</TableRow>
</TableLayout>
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="再玩一次"
android:textSize="24sp" />
</LinearLayout>
(二)后台代码:
项目后台代码如下:
package com.example.caijidan2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private TextView txt_img;
private ImageView img1, img2, img3;
private Button btn;
private boolean isAnyShoeClicked;
// 将 list 定义为类的成员变量
private List<Integer> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt_img = findViewById(R.id.txt_img);
img1 = findViewById(R.id.img1);
img2 = findViewById(R.id.img2);
img3 = findViewById(R.id.img3);
btn = findViewById(R.id.btn);
// 初始化 list
list = new ArrayList<>();
list.add(R.drawable.shoe_ok);
list.add(R.drawable.shoe_sorry);
list.add(R.drawable.shoe_sorry);
Collections.shuffle(list);
img1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!isAnyShoeClicked) {
handleShoeClick(0);
isAnyShoeClicked = true;
}
}
});
img2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!isAnyShoeClicked) {
handleShoeClick(1);
isAnyShoeClicked = true;
}
}
});
img3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!isAnyShoeClicked) {
handleShoeClick(2);
isAnyShoeClicked = true;
}
}
});
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
resetGame();
}
});
}
private void handleShoeClick(int index) {
img1.setImageDrawable(getResources().getDrawable(list.get(0)));
img2.setImageDrawable(getResources().getDrawable(list.get(1)));
img3.setImageDrawable(getResources().getDrawable(list.get(2)));
if (list.get(index) == R.drawable.shoe_ok) {
setAlphaForImages(index);
txt_img.setText("运气真好!");
} else {
txt_img.setText("再玩一次?");
}
}
private void setAlphaForImages(int correctIndex) {
for (int i = 0; i < 3; i++) {
if (i == correctIndex) {
if (i == 0) img1.setAlpha(0.5f);
else if (i == 1) img2.setAlpha(0.5f);
else img3.setAlpha(0.5f);
} else {
if (i == 0) img1.setAlpha(1.0f);
else if (i == 1) img2.setAlpha(1.0f);
else img3.setAlpha(1.0f);
}
}
}
private void resetGame() {
txt_img.setText("猜猜鸡蛋在哪只鞋子里?");
Collections.shuffle(list);
img1.setImageDrawable(getResources().getDrawable(R.drawable.shoe_default));
img2.setImageDrawable(getResources().getDrawable(R.drawable.shoe_default));
img3.setImageDrawable(getResources().getDrawable(R.drawable.shoe_default));
img1.setAlpha(1.0f);
img2.setAlpha(1.0f);
img3.setAlpha(1.0f);
isAnyShoeClicked = false;
}
}
三、项目总结
通过该项目的设计,掌握了 LinearLayout 和 TableLayout 的实际应用场景与组合方式。明白了如何利用 LinearLayout 的垂直排列特性来构建层次分明的界面结构,使不同功能的组件有序分布。同时,认识到 TableLayout 在处理多个并列且需要整齐排列元素时的高效性,学会了通过 TableRow 和 layout_weight 来实现元素的平均分配和整齐布局,提升了对 Android 布局体系的整体认知。学会了如何从视觉角度出发,设计出吸引人且易于理解的界面。例如,通过设置合适的 TextView 样式,如文字大小、颜色和字体风格,能够突出游戏主题,增强界面的可读性和吸引力。同时,在处理 ImageView 的图片资源时,意识到了图片质量、尺寸适配以及视觉协调性的重要性,为今后设计美观、适配性强的 UI 界面积累了经验。掌握了如何为控件添加点击事件监听器来实现交互功能。当用户点击 ImageView 时判断是否猜对以及给出相应提示的逻辑,锻炼了事件驱动编程的思维方式。同时,对于 “再玩一次” 按钮的功能实现,学会了如何重置游戏状态或者重新触发相关操作,提升了对应用程序交互逻辑设计的能力。改进随机数生成算法,确保鸡蛋在各个鞋子中的出现概率更加均匀。可以使用更复杂的随机算法或者引入时间戳等因素来增加随机性。同时,添加游戏难度设置功能,例如设置简单、中等、困难模式,在不同模式下调整鞋子数量、随机算法的复杂度或者提示信息的多少等。用户可以在游戏开始前选择适合自己的难度级别,提高游戏的趣味性和挑战性,满足不同玩家的需求。