复读鸽 2019-06-21 09:42
浏览 438

网格布局在安卓5.1版本下显示,在7.1或其他版本下不显示的问题

图片说明图片说明

代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal"
    android:background="@drawable/pic3"
    android:orientation="vertical"
    android:paddingBottom="20dp"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:paddingTop="20dp"
    tools:context=".MainActivity">

    <com.hnkjxy.a2048.MainView
        android:id="@+id/gameView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:layout_weight="1">

    </com.hnkjxy.a2048.MainView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:gravity="center"
        android:orientation="horizontal"
        >

        <Button
            android:id="@+id/btnrestart"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:background="@drawable/btn_style"
            android:onClick="onClick"
            android:text="restart" />

        <Button
            android:id="@+id/btnquit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/btn_style"
            android:text="quit"
            android:onClick="onClick"/>

        <Button
            android:id="@+id/btnhelp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/btn_style"
            android:text="help!"
            android:onClick="onClick"/>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffff"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Your Score:" />

        <TextView
            android:id="@+id/tv_currentscore"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:paddingLeft="20dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Top Score:" />

        <TextView
            android:id="@+id/tv_topscore"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:paddingLeft="20dp" />
    </LinearLayout>
</LinearLayout>

public class MainView extends GridLayout{
    public MainView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initGameView();
    }
    public MainView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initGameView();
    }

    public MainView(Context context) {
        super(context);
        initGameView();
    }


    //游戏初始化方法
    private void initGameView(){
        setColumnCount(4);
        //将面板设置成4列
        System.out.println("initGameView");

        setOnTouchListener(new 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 cardWith,int cardHeight){
        Count c;
        //遍历每一张卡片
        for (int y = 0; y < 4; y++) {
            for (int x = 0; x < 4; x++) {
                c = new Count(getContext());
                c.setNum(2);//给每一张卡片赋值
                addView(c,cardWith,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){
                            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){
                            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){
                            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 checkComplete(){
        boolean complete = true;//记录游戏的状态
        ALL:
        for (int y = 0; y <4; y++) {
            for (int x = 0; x <4; x++) {
                if(cardsMap[x][y].getNum() == 0 ||
                        (x>0 && cardsMap[x][y].equals(cardsMap[x-1][y])) ||
                        (x<3 && cardsMap[x][y].equals(cardsMap[x+1][y])) ||
                        (y>0 && cardsMap[x][y].equals(cardsMap[x][y-1])) ||
                        (y<3 && cardsMap[x][y].equals(cardsMap[x][y+1]))){
                    complete =false;
                    break ALL;
                    //break 用于跳出循环,但是只能跳出一层循环
                    //break ALL:可跳出所有的循环
                }
            }
        }
        if (complete){
            new AlertDialog.Builder(getContext())
                    .setTitle("hello!")
                    .setMessage("GAME OVER")
                    .setPositiveButton("RESTART", new DialogInterface.OnClickListener() {

                                public void onClick(DialogInterface dialog,
                                                    int which) {
                                    startGame();
                                }
                            })
                    .setNegativeButton("QUIT", new DialogInterface.OnClickListener() {

                                public void onClick(DialogInterface dialog,
                                                    int which) {
                                    MainActivity.getMainActivity().finish();
                                }
                            }).show();
        }
    }
    //存放卡片的二维数组,便于对卡片进行操作
    private Count[][] cardsMap = new Count[4][4];
    private List<Point> emptyPoint = new ArrayList<Point>();
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 易语言把MYSQL数据库中的数据添加至组合框
    • ¥20 求数据集和代码#有偿答复
    • ¥15 关于下拉菜单选项关联的问题
    • ¥20 java-OJ-健康体检
    • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
    • ¥15 使用phpstudy在云服务器上搭建个人网站
    • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
    • ¥15 vue3+express部署到nginx
    • ¥20 搭建pt1000三线制高精度测温电路
    • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况