简单的GridLayout比例动画

时间:2018-10-03 18:50:21

标签: java android

我试图将GridLayout的X和Y的比例从0缩放到100。我希望我的网格看起来是“不可见的”,并且我希望通过更改其比例使它变得很大。但是,每次尝试运行代码时,都会出现异常:

   gameGrid = (GridLayout) findViewById(R.id.gameView);
    ScaleAnimation animation = new ScaleAnimation(0, 1, 0, 1,
                                                   Animation.RELATIVE_TO_SELF, 0.5f,
                                                   Animation.RELATIVE_TO_SELF, 0.5f);
    gameGrid.setLayoutAnimation(new GridLayoutAnimationController(animation));

对象不为null,但出现此异常:

Attempt to invoke virtual method 'void android.widget.GridLayout.setLayoutAnimation(android.view.animation.LayoutAnimationController)' on a null object reference

我该如何解决?看起来很简单,请问我在这里想念什么?

我的XML文件:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:gravity="center"
    android:layoutDirection="ltr"
    android:orientation="vertical"
    android:padding="16dp">


  <GridLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:columnCount="7"
      android:orientation="vertical"
      android:rowCount="7"
      android:visibility="visible"
      android:id="@+id/GameGrid">

    <TableRow>

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B1"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B2"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B3"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B4"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B5"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B6"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B7"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />
    </TableRow>

    <TableRow>

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B8"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B9"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B10"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B11"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B12"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B13"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B14"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

    </TableRow>

    <TableRow>

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B15"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B16"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B17"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B18"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B19"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B20"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B21"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

    </TableRow>

    <TableRow>

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B22"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B23"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B24"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B25"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B26"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B27"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B28"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

    </TableRow>

    <TableRow>

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B29"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B30"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B31"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B32"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B33"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B34"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B35"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

    </TableRow>

    <TableRow>

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B36"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B37"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B38"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B39"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B40"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B41"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B42"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

    </TableRow>

    <TableRow>

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B43"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B44"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B45"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B46"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B47"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B48"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id="@+id/B49"
          android:layout_margin="3dp"
          android:layout_weight="1"
          android:background="@drawable/knight3" />

    </TableRow>
  </GridLayout>
</LinearLayout>

2 个答案:

答案 0 :(得分:0)

  

对象不为null,但出现此异常:

您的GridView ID是:

android:id="@+id/GameGrid"

您正在执行此操作:

gameGrid = (GridLayout) findViewById(R.id.gameView);

应该是这个

gameGrid = (GridLayout) findViewById(R.id.GameGrid);

对我来说,也尝试使用setAnimation()代替setLayoutAnimation()

答案 1 :(得分:0)

好,我是这样解决的:

   gameGrid = (GridLayout) findViewById(R.id.GameGrid);
    ScaleAnimation animation = new ScaleAnimation(0, 1, 0, 1,
                                                   Animation.RELATIVE_TO_SELF, 0.5f,
                                                   Animation.RELATIVE_TO_SELF, 0.5f);
    animation.setDuration(1000);
    gameGrid.setAnimation(animation);
    gameGrid.animate();