Android翻译动画在布局和视图下进行

时间:2013-06-12 12:18:22

标签: android android-layout android-animation translate-animation

我知道有关于这个主题的问题,但没有帮助我。 我正在制作纸牌游戏(通常称为浓度记忆游戏),每当选择两张相同的牌时,我希望他们使用进入顶部TranslateAnimation 即可。问题是,视图属于其他布局和视图......

这是功能:

    private void matchAnimation(int card1ID, int card2ID ,boolean isPlayer1) {

    getNameLocations();
    int x;
    int[] card1Loc = new int[2]; 
    int[] card2Loc = new int[2]; 
    ImageView card1 = (ImageView)findViewById(card1ID);
    ImageView card2 = (ImageView)findViewById(card2ID);

    if (player)
        x = playerOneLocation[0];
    else 
        x = playerTwoLocation[0];

    card1.getLocationOnScreen(card1Loc);
    card2.getLocationOnScreen(card2Loc);

    Animation animationMove = 
        new TranslateAnimation(0, x-card1Loc[0], 0,
                            playerOneLocation[1]-card1Loc[1]);
    animationMove.setZAdjustment(Animation.ZORDER_TOP);
    animationMove.setDuration(1000);
    animationMove.setFillAfter(true);
    card1.startAnimation(animationMove);
    card2.startAnimation(animationMove);
    card1.setVisibility(View.VISIBLE);
}

这是XML:

<RelativeLayout 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:background="#BDE1FF"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".GamePlay" >

<TableLayout
    android:id="@+id/tableLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="@dimen/activity_vertical_margin"
    android:layout_centerHorizontal="true" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp" >

        <ImageView
            android:id="@+id/c1"
            android:onClick="cardPicked"
            android:src="@drawable/icon"
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:contentDescription="@string/card_1" />

        <ImageView
            android:id="@+id/c2"
            android:onClick="cardPicked"
            android:src="@drawable/icon"
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:contentDescription="@string/card_2"
             />

        <ImageView
            android:id="@+id/c3"
            android:onClick="cardPicked"
            android:src="@drawable/icon"
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:contentDescription="@string/card_3" 
            />

        <ImageView
            android:id="@+id/c4"
            android:onClick="cardPicked"
            android:src="@drawable/icon" 
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:contentDescription="@string/card_4"
            />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp">

        <ImageView
            android:id="@+id/c5"
            android:onClick="cardPicked"
            android:src="@drawable/icon" 
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:contentDescription="@string/card_5"
            />

        <ImageView
            android:id="@+id/c6"
            android:onClick="cardPicked"
            android:src="@drawable/icon"
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:contentDescription="@string/card_6"
             />

        <ImageView
            android:id="@+id/c7"
            android:onClick="cardPicked"
            android:src="@drawable/icon" 
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:contentDescription="@string/card_7"
            />

        <ImageView
            android:id="@+id/c8"
            android:onClick="cardPicked"
            android:src="@drawable/icon"
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:contentDescription="@string/card_8" 
            />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp">

        <ImageView
            android:id="@+id/c9"
            android:onClick="cardPicked"
            android:src="@drawable/icon" 
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:contentDescription="@string/card_9"
             />

        <ImageView
            android:id="@+id/c10"
            android:onClick="cardPicked"
            android:src="@drawable/icon"
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:contentDescription="@string/card_10" 
            />

        <ImageView
            android:id="@+id/c11"
            android:onClick="cardPicked"
            android:src="@drawable/icon"
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:contentDescription="@string/card_11" />

        <ImageView
            android:id="@+id/c12"
            android:onClick="cardPicked"
            android:src="@drawable/icon"
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:contentDescription="@string/card_12" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp" >

        <ImageView
            android:id="@+id/c13"
            android:onClick="cardPicked"
            android:src="@drawable/icon"
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:contentDescription="@string/card_13"
             />

        <ImageView
            android:id="@+id/c14"
            android:onClick="cardPicked"
            android:src="@drawable/icon"
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:contentDescription="@string/card_14" />

        <ImageView
            android:id="@+id/c15"
            android:onClick="cardPicked"
            android:src="@drawable/icon"
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:contentDescription="@string/card_15" />

        <ImageView
            android:id="@+id/c16"
            android:onClick="cardPicked"
            android:src="@drawable/icon"
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:contentDescription="@string/card_16"
             />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp" >

        <ImageView
            android:id="@+id/c17"
            android:onClick="cardPicked"
            android:src="@drawable/icon"
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:contentDescription="@string/card_17" />

        <ImageView
            android:id="@+id/c18"
            android:onClick="cardPicked"
            android:src="@drawable/icon"
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:contentDescription="@string/card_18" />

        <ImageView
            android:id="@+id/c19"
            android:onClick="cardPicked"
            android:src="@drawable/icon"
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:contentDescription="@string/card_19" />

        <ImageView
            android:id="@+id/c20"
            android:onClick="cardPicked"
            android:src="@drawable/icon"
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:contentDescription="@string/card_20" />
    </TableRow>


</TableLayout>


   <TextView
       android:id="@+id/playername1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentRight="true"
       android:layout_alignParentTop="true"
       android:layout_marginTop="4dp"
       android:layout_marginRight="12dp"
       android:text="@string/placeholder"
       android:textAppearance="?android:attr/textAppearanceLarge" />

   <TextView
       android:id="@+id/playername2"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentLeft="true"
       android:layout_alignParentTop="true"
       android:layout_marginTop="4dp"
       android:layout_marginLeft="12dp"
       android:text="@string/placeholder"
       android:textAppearance="?android:attr/textAppearanceLarge" />

   <TextView
       android:id="@+id/playerscore2"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignBaseline="@+id/playerscore1"
       android:layout_alignBottom="@+id/playerscore1"
       android:layout_alignLeft="@+id/tableLayout1"
       android:text="@string/initial_score"
       android:textAppearance="?android:attr/textAppearanceMedium"
       android:textStyle="bold" />

   <ImageView
       android:id="@+id/pointer"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignTop="@+id/playername1"
       android:layout_centerHorizontal="true"
       android:contentDescription="@string/smurf_pointing"
       android:src="@drawable/smurf" />

   <TextView
       android:id="@+id/playerscore1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignRight="@+id/tableLayout1"
       android:layout_below="@+id/playername1"
       android:text="@string/initial_score"
       android:textAppearance="?android:attr/textAppearanceMedium"
       android:textStyle="bold" />

希望有人可以提供帮助!感谢。

1 个答案:

答案 0 :(得分:4)

android:clipChildren="false"设置为所有容器布局。

另请参阅我的问题:Animation: Move TextView into another container

然而,我注意到这导致Android 2.3版中的布局出现问题。 所以我现在要做的就是在所有动画发生的地方创建一个自己的ViewOverlay。

根据您希望支持的版本,还有View.getViewOverlay()