Android簡單猜撲克牌

老夫正在課堂上昏昏欲睡,突然鄰座同學推了我一把問我這鬼玩意怎么寫??

那就寫在這里給你們看好咯:

1.首先,作業要求實現撲克牌的按壓效果,這里要先在drawable文件夾下新建一個on_click的xml文件,用于保存按壓效果,代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:drawable="@drawable/image_back_yes"
        android:state_pressed="true"/>

    <item
        android:drawable="@drawable/image_back"
        android:state_pressed="false"/>

</selector>

其中用state_pressed的true或者false屬性來設置是否為按壓;當屬性為true時表示被按壓,此時加載被按壓時的圖片,若為false則表示正常狀態,即撲克牌的背面;

2.之后在layout中編寫五個撲克牌:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:textSize="20sp"
        android:layout_margin="10sp"
        android:id="@+id/tv1"
        android:text="@string/tv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <RelativeLayout
        android:id="@+id/pukePai"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:layout_margin="10sp"
            android:id="@+id/img_1"
            android:layout_width="100sp"
            android:layout_height="150sp"
            android:src="@drawable/on_click"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:clickable="true"/>

        <ImageView
            android:layout_margin="10sp"
            android:layout_toStartOf="@id/img_1"
            android:layout_below="@+id/img_1"
            android:id="@+id/img_2"
            android:layout_width="100sp"
            android:layout_height="150sp"
            android:src="@drawable/on_click"
            android:clickable="true"/>

        <ImageView
            android:layout_margin="10sp"
            android:id="@+id/img_3"
            android:layout_width="100sp"
            android:layout_height="150sp"
            android:layout_toEndOf="@id/img_2"
            android:layout_below="@id/img_1"
            android:src="@drawable/on_click"
            android:clickable="true"/>

        <ImageView
            android:id="@+id/img_4"
            android:layout_width="100sp"
            android:layout_height="150sp"
            android:layout_toEndOf="@id/img_3"
            android:layout_below="@id/img_1"
            android:src="@drawable/on_click"
            android:layout_margin="10sp"
            android:clickable="true"/>

        <ImageView
            android:layout_margin="10sp"
            android:id="@+id/img_5"
            android:layout_width="100sp"
            android:layout_height="150sp"
            android:layout_below="@id/img_3"
            android:layout_toEndOf="@id/img_2"
            android:src="@drawable/on_click"
            android:clickable="true"/>
    </RelativeLayout>

</LinearLayout>

注意這里使用的是ImageView,設置圖片時由于給定的圖片素材大小不一樣,于是沒有使用wrap_content屬性,而是指定了空間的長與寬,背景設置使用android:Src語句,即“android:src="@drawable/on_click",此處不直接引用單一圖片,而是引用剛才的on_click的xml文件,使ImageView能達到按壓顯示不同圖片效果。

(Tip:由于版本問題,部分編譯器可能無法支持“ android:layout_toEndOf”以及“ android:layout_toStartOf”屬性。此時只需要將End替換為Right,Start替換為Left即可。即“ android:layout_toEndOf”替換為“android:layout_toRightOf”)。

3.主代碼MainActivity:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView[] views = new ImageView[5];//保存界面上的五個ImageView控件
    private int[] viewsId = {R.id.img_1,R.id.img_2,R.id.img_3,R.id.img_4,R.id.img_5};
    private int[] imgs = {R.mipmap.image_a,R.mipmap.image_j,R.mipmap.image_q,R.mipmap.image_k,R.mipmap.image_joke};
    int j = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pukepai);

        for (int i = 0; i < 5; i++) {
            views[i] = (ImageView) findViewById(viewsId[i]);
            views[i].setOnClickListener(this);
        }

        //打亂明牌數組,直接在此處引用Random方法即可。
        Random();

    }

    @Override
    public void onClick(View v) {
        for (int i = 0; i < 5; i++) {
            if (v.getId() == views[i].getId()){
                //顯示明牌
                views[i].setImageResource(imgs[i]);
                views[i].setEnabled(false);
                if (imgs[i] == R.mipmap.image_a){
                    Ban();
                    Toast.makeText(this, "你猜對了,游戲結束,點擊空白處重新開始游戲", Toast.LENGTH_SHORT).show();
                }else {
                    j--;
                    if (j >= 1){
                        Toast.makeText(this, "你猜錯了,還剩"+j+"次機會!", Toast.LENGTH_SHORT).show();
                    }
                    if (j == 0){
                        Ban();
                        Toast.makeText(this, "Sorry,三次均未猜中,此次游戲結束,點擊空白處重新開始游戲,下次加油哦!",
                                Toast.LENGTH_LONG).show();
                    }
                }
            }
        }
    }

    /**
     * 初始化視圖,實現五張撲克牌背部
     */
    private void initViews(){

        j =3;
        for (int i = 0; i < 5; i++) {
            views[i] = (ImageView) findViewById(viewsId[i]);
            //此處必須引用寫入了按壓邏輯的xml頁面,直接引用背部頁面將沒有按壓效果
            views[i].setImageResource(R.drawable.on_click);
            views[i].setEnabled(true);
        }
        Random();
    }

    /**
     * 獲取隨機數
     */
    private void Random(){
        for (int i = 0; i < imgs.length; i++) {
            int temp = imgs[i];
            int s = (int) (Math.random()*5);
            imgs[i] = imgs[s];
            imgs[s] = temp;
        }
    }

    /**
     * 點擊除了5個ImageViews以外的地方重新開始
     * @param event
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        initViews();
        return true;
    }

    /**
     * 當出發三次錯誤或者點擊正確時,游戲結束,取消所有ImageView的點擊事件;建議自己寫個for循環
     */
    private void Ban(){
        views[0].setEnabled(false);
        views[1].setEnabled(false);
        views[2].setEnabled(false);
        views[3].setEnabled(false);
        views[4].setEnabled(false);
    }
}

至此一個簡單的猜撲克牌作業就完成了。
附上源碼文件地址:https://gitee.com/bbchond/CaiPuKePai

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,560評論 25 708
  • afinalAfinal是一個android的ioc,orm框架 https://github.com/yangf...
    passiontim閱讀 15,569評論 2 45
  • 有多少人是因為愛而結婚的?又有多少人,是因為到了該結婚的年齡,找了個條件合適的人就把婚結了?到底什么年齡是該結婚...
    洋漾y閱讀 357評論 3 1
  • 正念-即心觀心。對當下意念及動作保有覺知,時刻觀照自己,體會當下發生的一卻,應對任何可能發生的狀況。 之前時常感到...
    東涂西抹閱讀 1,082評論 1 1
  • 我相信,一千顆星星 有一千雙眼睛 我只能俗氣的說,像鉆石 我相信,一萬次黑夜 有一萬次告白 我只能暗自期待,你會來...
    4c9d0f2fc8c3閱讀 268評論 0 0