1. 程式人生 > >【Android開發】範例4-猜猜寶石放在哪個箱子裡

【Android開發】範例4-猜猜寶石放在哪個箱子裡

實現"猜猜寶石放在哪個箱子"的小遊戲:主介面中有三個箱子,單擊其中任意一個箱子,將開啟箱子,顯示裡面是否有寶石,並且將沒有被單擊的箱子設為半透明顯示,被單擊的箱子正常顯示,同時根據單擊的箱子是否有寶石顯示對應的結果。如果單擊的箱子沒有寶石,將顯示"很抱歉,猜錯了,要不要再來一次?"的提示文字,如果猜對了,就會將所有箱子透明化,並顯示"恭喜您,猜對了,祝您幸福!"

效果圖:

使用者未選擇箱子的時候:

使用者選中帶寶石的箱子的效果:


使用者沒有選中帶寶石的箱子的效果:


強制橫屏方法:
在AndroidManifest.xml的配置檔案裡面的<activity
裡面加入:
android:screenOrientation="landscape" //橫屏
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"//去掉狀態列及標題欄

具體實現方法:
res/layout/main.xml:
表格佈局:共分上下中三層,分別放置遊戲資訊,遊戲的三個箱子,"再來一次"的按鈕
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:id="@+id/tableLayout1"
	android:background="@drawable/background">
	<TableRow android:id="@+id/tableRow1"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:gravity="center"
	    android:layout_weight="2">
	    <TextView android:text="@string/title"
	        android:padding="10px"
	        android:gravity="center"
	        android:textSize="35px"
	        android:textColor="#FFFFFF"
	        android:id="@+id/textView1"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"/>
	</TableRow>
	<TableRow android:id="@+id/tableRow2"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:gravity="center"
	    android:layout_weight="1">
	    <LinearLayout android:orientation="horizontal"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content">
	        <ImageView android:id="@+id/imageview1"
	            android:src="@drawable/box_default"
	            android:paddingLeft="30px"
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content"/>
	        <ImageView android:id="@+id/imageview2"
	            android:src="@drawable/box_default"
	            android:paddingLeft="30px"
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content"/>
	        <ImageView android:id="@+id/imageview3"
	            android:src="@drawable/box_default"
	            android:paddingLeft="20px"
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content"/>
	    </LinearLayout>
	</TableRow>
	<LinearLayout android:orientation="horizontal"
	    	android:layout_weight="1"
	    	android:gravity="center_horizontal"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content">
	    <Button android:text="再玩一次"
	        android:id="@+id/button1"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"/>
	</LinearLayout>
</TableLayout>

MainActivity:

package com.example.test;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


public class MainActivity extends Activity {
	//定義一個儲存全部圖片id的陣列
	int[] imageIds=new int[]{R.drawable.box_default,R.drawable.box_default,R.drawable.box_ok};
	private ImageView image1;
	private ImageView image2;
	private ImageView image3;
	private TextView result;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		image1=(ImageView)findViewById(R.id.imageview1);//獲取ImageView1元件
		image2=(ImageView)findViewById(R.id.imageview2);//獲取ImageView2元件
		image3=(ImageView)findViewById(R.id.imageview3);//獲取ImageView3元件
		result=(TextView)findViewById(R.id.textView1);////獲取TextView1元件
		reset();//將箱子的順序打亂
		
		//為第一個箱子新增單擊事件監聽
		image1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				isRight(v,0);//判斷結果
				
			}
		});
		//為第二個箱子新增單擊事件監聽
				image2.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						isRight(v,1);//判斷結果
						
					}
				});
		//為第三個箱子新增單擊事件監聽
		image3.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				isRight(v,2);//判斷結果
				
			}
		});
		
		//獲取“再玩一次”按鈕
		Button button=(Button)findViewById(R.id.button1);
		//為“再玩一次”按鈕新增事件監聽器
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				reset();
				result.setText(R.string.title);//將標題恢復為預設值
				//圖片透明度回覆
				image1.setAlpha(255);
				image2.setAlpha(255);
				image3.setAlpha(255);
				//設定圖片能夠點選
				image1.setClickable(true);
				image2.setClickable(true);
				image3.setClickable(true);
				image1.setImageDrawable(getResources().getDrawable(R.drawable.box_default));
				image2.setImageDrawable(getResources().getDrawable(R.drawable.box_default));
				image3.setImageDrawable(getResources().getDrawable(R.drawable.box_default));
			}
		});
	}
	
	/*
	 * 判斷猜出的結果
	 * param v
	 * param index
	 * */
	private void isRight(View v, int index) {
		//使用隨機陣列中圖片資源ID設定每個ImageView
		image1.setImageDrawable(getResources().getDrawable(imageIds[0]));
		image2.setImageDrawable(getResources().getDrawable(imageIds[1]));
		image3.setImageDrawable(getResources().getDrawable(imageIds[2]));
		//為每個imageView設定半透明效果
		/*Alpha通道是一個8位的灰度通道,該通道用256級灰度
		 * 來記錄影象中的透明度資訊,定義透明、不透明和半透明區域*/
		image1.setAlpha(100);
		image2.setAlpha(100);
		image3.setAlpha(100);
		ImageView v1=(ImageView)v;
		v1.setAlpha(255);
		//設定圖片不再能夠點選
		image1.setClickable(false);
		image2.setClickable(false);
		image3.setClickable(false);
		if(imageIds[index]==R.drawable.box_ok){
			result.setText("恭喜您,猜對了,祝您幸福!");
		}else{
			result.setText("很抱歉,猜錯了,要不要再來一次?");
		}
	}


	//使用隨機數指定寶石所在的箱子
	private void reset(){
		for (int i = 0; i <3; i++) {
			int temp=imageIds[i];
			int index=(int)(Math.random()*2);
			imageIds[i]=imageIds[index];
			imageIds[index]=temp;
		}
	}
}