前言
最近没什么事情,在微信上看见看图猜成语的游戏,于是自己也写了一个来玩。源码放在了文章底部。
先来看下效果图
Gif的画质就将就这样吧
我们先来分析一下
我们主要的就是这3个地方
1、就是一个imageview
2、我们用动态的方式添加多个Textview
3、下面就是4个TextView
先来看下布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<ImageView
android:id="@+id/question_img"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<LinearLayout
android:id="@+id/liner"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_margin="12dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="题目:" />
<TextView
android:id="@+id/one"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:background="#90CDD5"
android:gravity="center" />
<TextView
android:id="@+id/two"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:background="#90CDD5"
android:gravity="center" />
<TextView
android:id="@+id/three"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:background="#90CDD5"
android:gravity="center" />
<TextView
android:id="@+id/four"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:background="#90CDD5"
android:gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/judge"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_margin="12dp"
android:gravity="center" />
<TextView
android:id="@+id/next"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_margin="12dp"
android:background="#00BCD4"
android:gravity="center"
android:text="答题"
android:visibility="visible" />
<TextView
android:id="@+id/number"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_margin="12dp"
android:gravity="center"
android:text="分数:0" />
</LinearLayout>
<TextView
android:id="@+id/tishi"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_margin="10dp"
android:background="#FF9800"
android:gravity="center"
android:text="提示"
android:textColor="#fff" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
第二我们就需要动态添加很多个Textview来显示字
大概就是这个样子 我们先要弄3个LinerLayout 出来在里面在分别装4个Textview具体代码我贴在下面
private void addTextiew() {
//获取屏幕的宽度高度
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight() - PxtoDpUntils.dp2pxInt(context, 30);
//先循环4次弄出LinearLayout
for (int i = 0; i < 4; i++) {
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
//添加进去
liner.addView(linearLayout);
//设置4个LinearLyout 的高度宽度
LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) linearLayout.getLayoutParams();
linearParams.width = width;
linearParams.height = height / 16;
linearLayout.setLayoutParams(linearParams);
//再循环出每个LinerLayout 里面的4个Textview
for (int j = 0; j < 4; j++) {
TextView textView = new TextView(context);
textView.setBackgroundColor(0xff8BCB8E);
textView.setTextSize(12);
textView.setGravity(Gravity.CENTER);
textView.setTextColor(0xffffffff);
//给每一个Textview加上tag一会方便来确认是那个Textview
textView.setTag(i + "" + j);
//把Textview添加到LinerLayout里面去
linearLayout.addView(textView);
//再用一个 Textview的集合吧16个Textview装起来方便使用
textViewsList.add(textView);
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) textView.getLayoutParams();
lp.width = width / 4;
lp.height = LinearLayout.LayoutParams.MATCH_PARENT;
lp.setMargins(3, 3, 3, 3);
textView.setLayoutParams(lp);
//这里是每个textView的点击事件
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
}
}
接下来就是把字放在每个Textview里面去 有12个错误的字4个正确的字是答案
主要代码
我先写了一个汉字的数组
public static String textList[] = {"哈","亿","个","么",
"墨","喜","留","样","哦","是","许","吃","测","级","发","飞","好","和 ","对","吧","不","包","你","一","儿","二","请","看"};
在上面的汉字里面随机抽12个字出来
int min = 0;
int max = TextListUntils.textList.length;
Random random = new Random();
for (int i = 0; i < 12; i++) {
int num = random.nextInt(max) % (max - min + 1) + min;
nowTextList.add(TextListUntils.textList[num]);
}
这个是正确的答案的集合
public static String okList[] = {"琴棋书画","过河拆桥","画蛇添足","倾国倾城","鹤立鸡群","手起刀落","老马识途","卧薪尝胆","老当益壮"};
第一关我们就应该取出正确的集合里面的第一个例如 琴棋书画 4个字放到上面的12个字的集合里面去
String str = TextListUntils.okList[level_index];
char[] chars = str.toCharArray();
for (int i = 0; i < chars.length; i++) {
nowTextList.add(chars[i] + "");
}
这样就出现了16个字,但是答案的4个字使我们最后添加进去所以答案总是在最后,所以我们需要打乱这个集合
if (nowTextList.size() == 16) {
//这个方法就是打乱集合里面的数据的方法
Collections.shuffle(nowTextList);
for (int i = 0; i < textViewsList.size(); i++) {
textViewsList.get(i).setText(nowTextList.get(i));
}
}
汉字我们已经添加完成了接下里我们要点击每个汉字组要代码贴下
for (int k = 0; k < questionList.size(); k++) {
if (questionList.get(k).getTag() == null) {
questionList.get(k).setTag(textView.getTag());
questionList.get(k).setText(textView.getText().toString());
break;
}
}
textView.setText("");
我把下面的4个填写正确答案Textview 装在了一个Textview的集合里面 questionList ,先便利一下面的集合,下面4个我是没有设置tag的所以判断tag只要为空就表示上面没有字。然后在他上面的tag和汉字赋值给下面,这里一定要break跳出循环并且把上面的textView赋值成“”。
这样我们就可以把 上面的字拿到下面去了。
接下来我们要把下面的字给还到上面去,而且还的位置还要还到原来的位置上去,这里我们开始把字点击下来的时候把tag也给了下来,所以还回去就很简单了只需要找对相应的tag就行了,下面是代码
for (int i = 0; i < textViewsList.size(); i++) {
if (textView.getTag().equals(textViewsList.get(i).getTag())) {
textViewsList.get(i).setText(textView.getText().toString());
}
}
textView.setText("");
textView.setTag(null);
这样基本我们就可以哪儿来的字我们就还回到哪儿去基本大体的功能就已经实现了,还有一个就是提示的功能我把代码贴出来
tishi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (num == 0) {
MyToast.addToast(context, "你没得分数 提示不了");
return;
}
if (num < level_num) {
MyToast.addToast(context, "分数不够了");
return;
}
tishiList.clear();
olderList.clear();
if (num >= 0) {
//可以提示
//当前关卡随便拿一个字出来提示
String str = TextListUntils.okList[level_index];
char[] chars = str.toCharArray();
for (int i = 0; i < chars.length; i++) {
tishiList.add(chars[i] + "");
olderList.add(chars[i] + "");
}
//先看下面的是否有字
for (int i = questionList.size() - 1; i >= 0; i--) {
if (questionList.get(i).getTag() != null) {
tishiList.remove(i);
}
}
if (tishiList.size() == 0) {
MyToast.addToast(context, "选择已经满了没有可以提示的地方了");
return;
}
int min = 0;
int max = tishiList.size();
Random random = new Random();
int nums = random.nextInt(max) % (max - min + 1) + min;
String randomText = tishiList.get(nums);
for (int i = 0; i < olderList.size(); i++) {
if (olderList.get(i).equals(randomText)) {
//先把 上面的字移除
for (int j = 0; j < nowTextList.size(); j++) {
if (nowTextList.get(j).equals(randomText)) {
textViewsList.get(j).setText("");
questionList.get(i).setText(randomText);
questionList.get(i).setTag(textViewsList.get(j).getTag());
break;
}
}
break;
}
}
num = num - level_num;
number.setText("分数:" + num);
//判断成功
if (one.getTag() != null && two.getTag() != null && three.getTag() != null && four.getTag() != null) {
isgo = true;
success();
} else {
isgo = false;
}
} else {
//不可以提示
MyToast.addToast(context, "分数不够了");
}
}
});
**
源码点我
**