效果图:
一、案例要求。
- 了解实现的具体的流程
- 怎样进行游戏界面的布局
- ImageView组件的基本应用
- 怎样实现随机指定鸡蛋所在的鞋子
- 怎样设置ImageView组件的透明度
二、功能结构。
【猜猜鸡蛋在哪只鞋子里】——>【显示3只鞋子】——>【点击鞋子显示结果】——>【再玩一次】
当我们单击其中的任意一只鞋子,将打开鞋子,显示里面是否有鸡蛋,并且将没有被单击的鞋子设置为半透明显示,而单击的鞋子则正常显示,同时根据单击的鞋子里是否有鸡蛋显示对应的结果。例如,单击中间的鞋子,如果鸡蛋在这只鞋子里,将显示提示信息——【恭喜你猜中了】,否则,将显示——【很遗憾,猜错了】的提示信息。另外,当我们再点击再玩一次按钮时,可以重新进入游戏。
三、具体实现。
布局文件代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/background"
tools:context="cn.edu.hznu.ex3_2.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:textSize="28sp"
android:gravity="center"
android:textColor="#FF0015FF"
android:text="@string/title"
android:id="@+id/textView"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:onClick="judge"
android:layout_height="match_parent"
android:src="@drawable/shoe_default"
android:id="@+id/shoe1"/>
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:onClick="judge"
android:layout_height="match_parent"
android:src="@drawable/shoe_default"
android:id="@+id/shoe2"/>
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:onClick="judge"
android:layout_height="match_parent"
android:src="@drawable/shoe_default"
android:id="@+id/shoe3"/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="24sp"
android:text="再玩一局"
android:id="@+id/replay"/>
</LinearLayout>
实现代码:
package cn.edu.hznu.ex3_2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView textView;
private int[] imagesId=new int[3];
private int position;
private ImageView[] imageViews=new ImageView[3];
private Button replay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(TextView)findViewById(R.id.textView);
imageViews[0]=(ImageView)findViewById(R.id.shoe1);
imageViews[1]=(ImageView)findViewById(R.id.shoe2);
imageViews[2]=(ImageView)findViewById(R.id.shoe3);
replay=(Button)findViewById(R.id.replay);
init();
replay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
textView.setText(R.string.title); //又回到原来
for(int i=0;i<3;i++){ //显示原来的鞋子
imageViews[i].setImageResource(R.drawable.shoe_default);
imageViews[i].setAlpha(1f);
imageViews[i].setEnabled(true);
}
init();
}
});
}
private void init() {
position=(int) (Math.random()*3);
for(int i=0;i<3;i++){
if(i==position){
imagesId[i]=R.drawable.shoe_ok;
}else {
imagesId[i]=R.drawable.shoe_sorry;
}
}
}
public void judge(View v){
//v代表点击的这个控件对象
if(v==imageViews[position]){
textView.setText("恭喜你猜中了!");
}else{
textView.setText("很遗憾,猜错了!");
}
for(int i=0;i<3;i++){
imageViews[i].setImageResource(imagesId[i]);
imageViews[i].setAlpha(0.5f);//控件的透明度
imageViews[i].setEnabled(false);
}
v.setAlpha(1f);
}
}
2020-3-19