简体   繁体   中英

Simple GridLayout scale animation

I am trying to scale X and Y of a GridLayout from 0 to 100. I want my grid to look "invisible" and than I want it to be very big by changing it's scale. However I get an exception every time I try to run my code:

   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));

The object is not null, but I get this exception:

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

How should I solve this? It's looks rather simple, what am I missing here please?

My XML file:

    <?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>

The object is not null, but I get this exception:

Your GridView id is :

android:id="@+id/GameGrid"

And you are doing this :

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

It should be this one

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

Also try to use setAnimation() instead of setLayoutAnimation() for me is working.

OK, I solved it like this:

   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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM