效果圖:
一、案例要求。
- 瞭解實現的具體的流程
- 怎樣進行遊戲界面的佈局
- 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