多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > 綜合技術 > Android技術――高級UI:視圖拖拽(下)

Android技術――高級UI:視圖拖拽(下)

來源:程序員人生   發布時間:2015-04-11 08:57:21 閱讀次數:2544次

3、用視圖拖拽+GridLayout實現簡單移圖游戲

這只實現了簡單的最核心的UI,沒有寫判贏邏輯。

源代碼參見:https://github.com/YongYuIT/YituGame

1、/YituGame/res/layout/activity_game_main_line.xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:id="@+id/rel_root"
    android:padding="5dp" >


    <GridLayout
        android:id="@+id/grl_root"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:columnCount="2"
        android:rowCount="5" >


        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_columnSpan="2"
            android:background="@drawable/final_img" />


        <Space
            android:layout_height="20dp"
            android:layout_columnSpan="2" />


        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_columnSpan="2"
            android:orientation="horizontal" >


            <LinearLayout
                android:id="@+id/lil_0_0"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="horizontal" >


                <ImageView
                    android:id="@+id/img_0_0"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/img_1" />
            </LinearLayout>


            <LinearLayout
                android:id="@+id/lil_0_1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="horizontal" >


            </LinearLayout>
        </LinearLayout>


        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_columnSpan="2"
            android:orientation="horizontal" >


            <LinearLayout
                android:id="@+id/lil_1_0"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="horizontal" >


                <ImageView
                    android:id="@+id/img_1_0"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/img_2" />
            </LinearLayout>


            <LinearLayout
                android:id="@+id/lil_1_1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="horizontal" >


                <ImageView
                    android:id="@+id/img_1_1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/img_4" />
            </LinearLayout>
        </LinearLayout>


        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_columnSpan="2"
            android:orientation="horizontal" >


            <LinearLayout
                android:id="@+id/lil_2_0"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="horizontal" >


                <ImageView
                    android:id="@+id/img_2_0"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/img_5" />
            </LinearLayout>


            <LinearLayout
                android:id="@+id/lil_2_1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="horizontal" >


                <ImageView
                    android:id="@+id/img_2_1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/img_3" />
            </LinearLayout>
        </LinearLayout>
    </GridLayout>


</RelativeLayout>

2、/YituGame/src/com/thinking/yitugame/Game_Main.java文件

package com.thinking.yitugame;


import com.thinking.basicService.LogService;


import android.app.Activity;
import android.content.ClipData;
import android.os.Bundle;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.View.DragShadowBuilder;
import android.view.View.OnDragListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;


public class Game_Main extends Activity
{


    LinearLayout   lil_0_0;
    LinearLayout   lil_0_1;
    LinearLayout   lil_1_0;
    LinearLayout   lil_1_1;
    LinearLayout   lil_2_0;
    LinearLayout   lil_2_1;
    RelativeLayout rel_root;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        LogService.writeLog("running!");
        super.onCreate(savedInstanceState);


        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);


        setContentView(R.layout.activity_game_main_line);


        ImageView img_0_0 = (ImageView) findViewById(R.id.img_0_0);
        ImageView img_1_0 = (ImageView) findViewById(R.id.img_1_0);
        ImageView img_1_1 = (ImageView) findViewById(R.id.img_1_1);
        ImageView img_2_0 = (ImageView) findViewById(R.id.img_2_0);
        ImageView img_2_1 = (ImageView) findViewById(R.id.img_2_1);


        lil_0_0 = (LinearLayout) findViewById(R.id.lil_0_0);
        lil_0_1 = (LinearLayout) findViewById(R.id.lil_0_1);
        lil_1_0 = (LinearLayout) findViewById(R.id.lil_1_0);
        lil_1_1 = (LinearLayout) findViewById(R.id.lil_1_1);
        lil_2_0 = (LinearLayout) findViewById(R.id.lil_2_0);
        lil_2_1 = (LinearLayout) findViewById(R.id.lil_2_1);
        rel_root = (RelativeLayout) findViewById(R.id.rel_root);


        img_0_0.setOnTouchListener(new ImgOnTouchListener());
        img_1_0.setOnTouchListener(new ImgOnTouchListener());
        img_1_1.setOnTouchListener(new ImgOnTouchListener());
        img_2_0.setOnTouchListener(new ImgOnTouchListener());
        img_2_1.setOnTouchListener(new ImgOnTouchListener());


        lil_0_0.setOnDragListener(new MyDragListener());
        lil_0_1.setOnDragListener(new MyDragListener());
        lil_1_0.setOnDragListener(new MyDragListener());
        lil_1_1.setOnDragListener(new MyDragListener());
        lil_2_0.setOnDragListener(new MyDragListener());
        lil_2_1.setOnDragListener(new MyDragListener());
        rel_root.setOnDragListener(new MyDragListener());
    }


    private int[] getXYById(int id)
    {
        if (id == lil_0_0.getId())
        {
            return new int[] { 0, 0 };
        }
        if (id == lil_0_1.getId())
        {
            return new int[] { 0, 1 };
        }
        if (id == lil_1_0.getId())
        {
            return new int[] { 1, 0 };
        }
        if (id == lil_1_1.getId())
        {
            return new int[] { 1, 1 };
        }
        if (id == lil_2_0.getId())
        {
            return new int[] { 2, 0 };
        }
        if (id == lil_2_1.getId())
        {
            return new int[] { 2, 1 };
        } else
        {
            return new int[] { ⑴, ⑴ };
        }
    }
    
    private class ImgOnTouchListener implements OnTouchListener
    {
        @Override
        public boolean onTouch(View v, MotionEvent event)
        {
            LogService.writeLog("onTouch running!");
            ClipData data = ClipData.newPlainText("", "");
            DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
            v.startDrag(data, shadowBuilder, v, 0);
            return true;
        }
    }


    class MyDragListener implements OnDragListener
    {
        @Override
        public boolean onDrag(View v, DragEvent event)
        {
            int action = event.getAction();
            switch (event.getAction())
            {
                case DragEvent.ACTION_DRAG_STARTED:
                    break;
                case DragEvent.ACTION_DRAG_ENTERED:
                    break;
                case DragEvent.ACTION_DRAG_EXITED:
                    break;
                case DragEvent.ACTION_DROP:
                    View view = (View) event.getLocalState();
                    ViewGroup owner = (ViewGroup) view.getParent();
                    LinearLayout container;
                    try
                    {
                        container = (LinearLayout) v;
                    } catch (Exception e)
                    {
                        view.setVisibility(View.VISIBLE);
                        break;
                    }


                    LogService.writeLog("owner:" + getXYById(owner.getId())[0]
                            + " " + getXYById(owner.getId())[1] + " container:"
                            + getXYById(container.getId())[0] + " "
                            + getXYById(container.getId())[1]);


                    LogService.writeLog("getChildCount"
                            + container.getChildCount());


                    if ((getXYById(owner.getId())[0] == getXYById(container
                            .getId())[0] || getXYById(owner.getId())[1] == getXYById(container
                            .getId())[1])
                            && container.getChildCount() == 0)
                    {
                        owner.removeView(view);
                        container.addView(view);
                    }
                    view.setVisibility(View.VISIBLE);
                    break;
                case DragEvent.ACTION_DRAG_ENDED:
                default:
                    break;
            }
            return true;
        }
    }
}

真機運行效果:


生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 黄色一级a毛片 | 亚洲国产日韩欧美 | 在线观看男女激情小视频 | 亚洲影院在线 | 久久 在线播放 | www.在线视频 | 亚洲高清免费 | 春意影院午夜免费入口 | 亚洲精品国产v片在线观看 亚洲精品国产啊女成拍色拍 | 欧美性猛交xxxxx按摩欧美 | 欧美成人免费网在线观看 | 亚洲欧美一区二区三区 | 亚洲综合国产一区在线 | 亚洲六区 | 激性欧美激情在线播放16页 | 国产v片在线观看 | www.日本精品 | 久久中精品中文 | 最近高清中文字幕在线国语5 | 亚洲春色小说 | 在线观看欧美亚洲 | 8av国产精品爽爽ⅴa在线观看 | 拍拍拍在线观看视频免费 | 日韩欧美在线观看视频 | 极品丝袜高跟91极品系列 | 日本特级全黄一级毛片 | 亚洲欧美高清视频 | 琪琪在线观看 | 日本一区二区三区在线观看视频 | 成人免费淫片免费观看 | 亚洲第一免费网站 | 亚洲精品在线第一页 | 中文字幕第30页 | 色琪琪永久远网址 | 欧美日韩一二三四区 | 3344成年站福利在线视频免费 | 四虎东方va私人影库在线观看 | 欧美不卡视频在线 | 中文字幕第23页 | 国产在线观看精品一区二区三区91 | www天堂在线 |