就是图片这样,滑动又是有分数变化的。
代码:package com.example.administrator.game2048;import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.GridLayout;import java.util.ArrayList;
import java.util.List;public class GameView extends GridLayout{
    public GameView(Context context, AttributeSet attrs,int defStyle) {
        super(context, attrs, defStyle);
        initGameView();
    }
    public GameView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initGameView();
    }    public GameView(Context context) {
        super(context);
        initGameView();
    }
    //游戏初始化方法
    private void initGameView(){
        setColumnCount(4);
        //将面板设置成4列
        setBackgroundColor(0xffbbada0);
        System.out.println("initGameView");        setOnTouchListener(new View.OnTouchListener() {
            /*
             * startX:手机刚开始在屏幕上的X坐标
             * startY:手机刚开始在屏幕上的Y坐标
             * offsetX,offsetY,分别是手指在屏幕上的X,Y上的偏移量
             */
            private float startX,startY,offsetX,offsetY;
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        startX = event.getX();
                        startY = event.getY();
                        break;
                    case MotionEvent.ACTION_UP:
                        offsetX = event.getX() - startX;
                        offsetY = event.getY() - startY;                        if(Math.abs(offsetX) > Math.abs(offsetY)){
                            if(offsetX < -5){
                                swipeLeft();
                                System.out.println("Left");                            }else if(offsetX > 5){
                                swipeRight();
                                System.out.println("Right");
                            }                        } else{
                            if(offsetY < -5){
                                swipeUp();
                                System.out.println("Up");
                            }else if(offsetY > 5){
                                swipeDown();
                                System.out.println("Down");
                            }
                        }
                        break;
                }
                return true;
            }
        });
    }
    //当布局的尺寸发生变化时,会执行该方法
    protected void onSizeChanged(
            int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        int cardWidth = (Math.min(w, h)-10)/4;
        addCards(cardWidth, cardWidth);
        startGame();
    }
    //添加卡片的方法
    private void addCards( int cardWidth,int cardHeight){
        Card c;
        //遍历每一张卡片
        for (int y = 0; y < 4; y++) {
            for (int x = 0; x < 4; x++) {
                c = new Card(getContext());
                c.setNum(0);
                addView(c, cardWidth, cardHeight);
                cardsMap[x][y] = c;
            }
        }
    }
    //开始游戏的方法
    public void startGame(){
        MainActivity.getMainActivity().clearScore();
        for (int y = 0; y < 4; y++) {
            for (int x = 0; x < 4; x++) {
                cardsMap[x][y].setNum(0);
                emptyPoint.add(new Point(x,y));
            }
        }
        addRandomNum();
        addRandomNum();
    }
    //添加随机数
    private void addRandomNum() {
        emptyPoint.clear();
        for (int y = 0; y < 4; y++) {
            for (int x = 0; x < 4; x++) {
                if (cardsMap[x][y].getNum()<=0) {
                    emptyPoint.add(new Point(x,y));                }
            }
        }
        Point p = emptyPoint
                .remove((int) (Math.random() * emptyPoint.size()));
        cardsMap[p.x][p.y].setNum(Math.random() > 0.1 ? 2 : 4);
    }
    //向左滑动
    public void swipeLeft(){
        boolean meger = false;
        for (int y = 0; y < 4; y++) {
            for (int x = 0; x < 4; x++) {
                for (int x1 = x+1; x1 < 4; x1++) {
                    if(cardsMap[x1][y].getNum()>0){
                        if(cardsMap[x][y].getNum()<=0){
                            /*
                             *  将下标为(x,y)所在位置的卡片上的数字
                             * 设置为,坐标为(x1,y)所在位置的卡片上的值;
                             * 第二步,将坐标(x1,y)所在位置的卡片上的数字
                             * 设置为0
                             *    (即:变成空卡片)
                             */
                            cardsMap[x][y].setNum(
                                    cardsMap[x1][y].getNum());
                            cardsMap[x1][y].setNum(0);
                            x--;
                            meger =true;
                            break;
                        }else if(cardsMap[x][y].equals(cardsMap[x1][y])){
                            cardsMap[x][y].setNum(cardsMap[x][y].getNum()*2);
                            cardsMap[x1][y].setNum(0);
                            MainActivity.getMainActivity().
                                    addScore(cardsMap[x][y].getNum());
                            meger = true;
                        }
                        break;
                    }
                }
            }
        }
        if(meger){
            addRandomNum();
            checkComplete();
        }
    }
    //向右滑动
    public void swipeRight(){
        boolean meger = false;
        for (int y = 0; y < 4; y++) {
            for (int x = 3; x >=0; x--) {
                for (int x1 = x-1; x1 >= 0; x1--) {
                    if(cardsMap[x1][y].getNum()>0){
                        if(cardsMap[x][y].getNum()<=0){
                            /*
                             *  将下标为(x,y)所在位置的卡片上的数字
                             * 设置为,坐标为(x1,y)所在位置的卡片上的值;
                             * 第二步,将坐标(x1,y)所在位置的卡片上的数字
                             * 设置为0
                             *    (即:变成空卡片)
                             */
                            cardsMap[x][y].setNum(
                                    cardsMap[x1][y].getNum());
                            cardsMap[x1][y].setNum(0);
                            x++;
                            meger =true;
                            break;
                        }else if(cardsMap[x][y].equals(cardsMap[x1][y])){
                            cardsMap[x][y].setNum(cardsMap[x][y].getNum()*2);
                            cardsMap[x1][y].setNum(0);
                            MainActivity.getMainActivity().
                                    addScore(cardsMap[x][y].getNum());
                            meger =true;
                        }break;
                    }
                }
            }
        }
        if(meger){
            addRandomNum();
            checkComplete();
        }
    }
    //向上滑动
    public void swipeUp(){
        boolean meger = false;
        for (int x= 0; x< 4; x++) {
            for (int y = 0; y < 4; y++) {
                for (int y1 = y+1; y1 < 4; y1++) {
                    if(cardsMap[x][y1].getNum()>0){
                        if(cardsMap[x][y].getNum()<=0){
                            /*
                             *  将下标为(x,y)所在位置的卡片上的数字
                             * 设置为,坐标为(x1,y)所在位置的卡片上的值;
                             * 第二步,将坐标(x1,y)所在位置的卡片上的数字
                             * 设置为0
                             *    (即:变成空卡片)
                             */
                            cardsMap[x][y].setNum(
                                    cardsMap[x][y1].getNum());
                            cardsMap[x][y1].setNum(0);
                            y--;
                            meger =true;
                            break;
                        }else if(cardsMap[x][y].equals(cardsMap[x][y1])){
                            cardsMap[x][y].setNum(cardsMap[x][y].getNum()*2);
                            cardsMap[x][y1].setNum(0);
                            MainActivity.getMainActivity().
                                    addScore(cardsMap[x][y].getNum());
                            meger =true;
                        }
                        break;
                    }
                }
            }
        }
        if(meger){
            addRandomNum();
            checkComplete();
        }
    }
    //向下滑动
    public void swipeDown(){
        boolean meger = false;
        for (int x = 0; x< 4; x++) {
            for (int y = 3; y>= 0;y--) {
                for (int y1 = y-1; y1 >=0; y1--) {
                    if(cardsMap[x][y1].getNum()>0){
                        if(cardsMap[x][y].getNum()<=0){
                            /*
                             *  将下标为(x,y)所在位置的卡片上的数字
                             * 设置为,坐标为(x1,y)所在位置的卡片上的值;
                             * 第二步,将坐标(x1,y)所在位置的卡片上的数字
                             * 设置为0
                             *    (即:变成空卡片)
                             */
                            cardsMap[x][y].setNum(
                                    cardsMap[x][y1].getNum());
                            cardsMap[x][y1].setNum(0);
                            y++;
                            meger =true;
                            break;
                        }else if(cardsMap[x][y].equals(cardsMap[x][y1])){
                            cardsMap[x][y].setNum(cardsMap[x][y].getNum()*2);
                            cardsMap[x][y1].setNum(0);
                            MainActivity.getMainActivity().
                                    addScore(cardsMap[x][y].getNum());
                            meger =true;
                        }
                        break;